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 +5 -1
|
@@ -306,6 +306,10 @@
|
|
| 306 |
|
| 307 |
}, 3000);
|
| 308 |
}
|
| 309 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
</body>
|
| 311 |
</html>
|
|
|
|
| 306 |
|
| 307 |
}, 3000);
|
| 308 |
}
|
| 309 |
+
// Initialize with your actual Azure credentials
|
| 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 |
+
</script>
|
| 314 |
</body>
|
| 315 |
</html>
|