Spaces:
Running
function encodeImageToBase64(filePath) {
Browse filesconst buffer = fs.readFileSync(filePath);
const ext = filePath.split('.').pop();
return `data:image/${ext};base64,${buffer.toString('base64')}`;
}
// الدالة الرئيسية لإرسال POST والحصول على النتيجة
async function analyzeMedia(userText) {
const media_url = encodeImageToBase64(IMAGE_PATH);
// بناء chat prompt كنص فقط (لأن نموذج النصوص لا يدعم image_url مباشرة)
const chat_prompt = [
{ role: "system", content: "تحليل الوسائط الإعلامية" },
{ role: "user", content: `${userText}\nالصورة: ${media_url}` }
];
try {
const response = await fetch(AZURE_ENDPOINT, {
method: "POST",
headers: {
"Content-Type": "application/json",
"api-key": AZURE_API_KEY
},
body: JSON.stringify({
model: DEPLOYMENT,
messages: chat_prompt,
max_completion_tokens: 1024,
stream: false
})
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Azure API error ${response.status}: ${text}`);
}
const data = await response.json();
// استرجاع المحتوى النهائي مباشرة
const content = data.choices?.[0]?.message?.content || "";
return content;
} catch (err) {
console.error("Error:", err);
return null;
}تحليل يكون ناتج من الداله
}
- index.html +24 -1
|
@@ -310,6 +310,29 @@
|
|
| 310 |
const AZURE_ENDPOINT = "https://your-resource-name.openai.azure.com/openai/deployments/your-deployment-name/chat/completions?api-version=2023-05-15";
|
| 311 |
const AZURE_API_KEY = "your-api-key-here";
|
| 312 |
const DEPLOYMENT = "your-deployment-name";
|
| 313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
</body>
|
| 315 |
</html>
|
|
|
|
| 310 |
const AZURE_ENDPOINT = "https://your-resource-name.openai.azure.com/openai/deployments/your-deployment-name/chat/completions?api-version=2023-05-15";
|
| 311 |
const AZURE_API_KEY = "your-api-key-here";
|
| 312 |
const DEPLOYMENT = "your-deployment-name";
|
| 313 |
+
|
| 314 |
+
// Helper function to format API response
|
| 315 |
+
function parseAnalysisResponse(text) {
|
| 316 |
+
try {
|
| 317 |
+
// Try to parse as JSON first
|
| 318 |
+
return JSON.parse(text);
|
| 319 |
+
} catch (e) {
|
| 320 |
+
// Fallback to extracting sections from plain text
|
| 321 |
+
const summaryMatch = text.match(/ملخص:(.*?)(?=الكلمات المفتاحية|$)/s);
|
| 322 |
+
const keywordsMatch = text.match(/الكلمات المفتاحية:(.*?)(?=التقرير التفصيلي|$)/s);
|
| 323 |
+
const reportMatch = text.match(/التقرير التفصيلي:(.*)/s);
|
| 324 |
+
|
| 325 |
+
return {
|
| 326 |
+
summary: summaryMatch ? summaryMatch[1].trim() : "لا يوجد ملخص متاح",
|
| 327 |
+
keywords: keywordsMatch ?
|
| 328 |
+
keywordsMatch[1].trim().split(',').map(k => k.trim()) :
|
| 329 |
+
["لا يوجد كلمات مفتاحية"],
|
| 330 |
+
detailed_report: reportMatch ?
|
| 331 |
+
`<p>${reportMatch[1].trim().replace(/\n/g, '</p><p>')}</p>` :
|
| 332 |
+
"<p>لا يوجد تقرير متاح</p>"
|
| 333 |
+
};
|
| 334 |
+
}
|
| 335 |
+
}
|
| 336 |
+
</script>
|
| 337 |
</body>
|
| 338 |
</html>
|