Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,68 +13,50 @@ def analyze_sentiment(text):
|
|
| 13 |
if not api_key:
|
| 14 |
return "خطأ: لم يتم العثور على مفتاح API Key.", "❌"
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
# لاحظ: قمنا بتغيير اسم النموذج هنا بناءً على القائمة التي ظهرت لك
|
| 18 |
model_name = "gemini-2.5-flash"
|
| 19 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/{model_name}:generateContent?key={api_key}"
|
| 20 |
|
| 21 |
-
# 3. تجهيز الرسالة (Prompt) بتعليمات دقيقة
|
| 22 |
prompt_text = f"""
|
| 23 |
-
أنت نظام ذكي لتحليل المشاعر (Sentiment Analysis) للنصوص
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
-
|
| 29 |
-
|
| 30 |
-
- confidence: (نسبة مئوية تقديرية)
|
| 31 |
-
|
| 32 |
-
إذا لم تستطع الرد بـ JSON، اكتب النتيجة نصياً بشكل مختصر.
|
| 33 |
"""
|
| 34 |
|
| 35 |
payload = {
|
| 36 |
-
"contents": [{
|
| 37 |
-
"parts": [{"text": prompt_text}]
|
| 38 |
-
}]
|
| 39 |
}
|
| 40 |
-
|
| 41 |
headers = {'Content-Type': 'application/json'}
|
| 42 |
|
| 43 |
try:
|
| 44 |
-
# 4. الاتصال المباشر
|
| 45 |
response = requests.post(url, headers=headers, data=json.dumps(payload))
|
| 46 |
|
| 47 |
if response.status_code != 200:
|
| 48 |
-
return f"خطأ
|
| 49 |
|
| 50 |
data = response.json()
|
| 51 |
-
|
| 52 |
-
# استخراج الرد
|
| 53 |
try:
|
| 54 |
result_text = data['candidates'][0]['content']['parts'][0]['text']
|
| 55 |
-
|
| 56 |
-
# تنظيف الرد
|
| 57 |
clean_result = result_text.replace("```json", "").replace("```", "").strip()
|
| 58 |
|
| 59 |
-
# تحديد الإيموجي واللون
|
| 60 |
emoji = "🤔"
|
| 61 |
-
if "إيجابي" in clean_result:
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
emoji = "😡 سلبي"
|
| 65 |
-
else:
|
| 66 |
-
emoji = "😐 محايد"
|
| 67 |
|
| 68 |
return clean_result, emoji
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
return f"فشل استخراج الرد: {str(e)} \nالرد الخام: {str(data)}", "⚠️"
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
-
return f"
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {direction: rtl; text-align: right;}") as demo:
|
| 78 |
|
| 79 |
with gr.Row():
|
| 80 |
gr.Markdown("# 📊 تطبيق انطباع (Entibaa)")
|
|
@@ -84,7 +66,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {direction: rtl; t
|
|
| 84 |
with gr.Row():
|
| 85 |
input_text = gr.Textbox(
|
| 86 |
label="أدخل التعليق هنا لتحليله",
|
| 87 |
-
placeholder="
|
| 88 |
lines=3,
|
| 89 |
text_align="right"
|
| 90 |
)
|
|
@@ -92,9 +74,21 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {direction: rtl; t
|
|
| 92 |
analyze_btn = gr.Button("تحليل الانطباع 🚀", variant="primary")
|
| 93 |
|
| 94 |
with gr.Row():
|
| 95 |
-
output_text = gr.Textbox(label="
|
| 96 |
-
sentiment_badge = gr.Label(label="النتيجة
|
| 97 |
|
| 98 |
analyze_btn.click(analyze_sentiment, inputs=input_text, outputs=[output_text, sentiment_badge])
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
demo.launch()
|
|
|
|
| 13 |
if not api_key:
|
| 14 |
return "خطأ: لم يتم العثور على مفتاح API Key.", "❌"
|
| 15 |
|
| 16 |
+
# استخدام Gemini 2.5 Flash
|
|
|
|
| 17 |
model_name = "gemini-2.5-flash"
|
| 18 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/{model_name}:generateContent?key={api_key}"
|
| 19 |
|
|
|
|
| 20 |
prompt_text = f"""
|
| 21 |
+
أنت نظام ذكي لتحليل المشاعر (Sentiment Analysis) للنصوص العربية.
|
| 22 |
+
النص: "{text}"
|
| 23 |
+
المطلوب:
|
| 24 |
+
- sentiment: (إيجابي، سلبي، محايد)
|
| 25 |
+
- reason: (السبب في جملة قصيرة)
|
| 26 |
+
- confidence: (نسبة مئوية)
|
| 27 |
+
اجعل الرد مختصراً جداً.
|
|
|
|
|
|
|
|
|
|
| 28 |
"""
|
| 29 |
|
| 30 |
payload = {
|
| 31 |
+
"contents": [{"parts": [{"text": prompt_text}]}]
|
|
|
|
|
|
|
| 32 |
}
|
|
|
|
| 33 |
headers = {'Content-Type': 'application/json'}
|
| 34 |
|
| 35 |
try:
|
|
|
|
| 36 |
response = requests.post(url, headers=headers, data=json.dumps(payload))
|
| 37 |
|
| 38 |
if response.status_code != 200:
|
| 39 |
+
return f"خطأ ({response.status_code})", "❌"
|
| 40 |
|
| 41 |
data = response.json()
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
result_text = data['candidates'][0]['content']['parts'][0]['text']
|
|
|
|
|
|
|
| 44 |
clean_result = result_text.replace("```json", "").replace("```", "").strip()
|
| 45 |
|
|
|
|
| 46 |
emoji = "🤔"
|
| 47 |
+
if "إيجابي" in clean_result: emoji = "🤩 إيجابي"
|
| 48 |
+
elif "سلبي" in clean_result: emoji = "😡 سلبي"
|
| 49 |
+
else: emoji = "😐 محايد"
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
return clean_result, emoji
|
| 52 |
+
except:
|
| 53 |
+
return "فشل استخراج الرد", "⚠️"
|
|
|
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
+
return f"خطأ: {str(e)}", "❌"
|
| 57 |
|
| 58 |
+
# تصميم الواجهة
|
| 59 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {direction: rtl; text-align: right;} footer {display: none !important;}") as demo:
|
| 60 |
|
| 61 |
with gr.Row():
|
| 62 |
gr.Markdown("# 📊 تطبيق انطباع (Entibaa)")
|
|
|
|
| 66 |
with gr.Row():
|
| 67 |
input_text = gr.Textbox(
|
| 68 |
label="أدخل التعليق هنا لتحليله",
|
| 69 |
+
placeholder="اكتب تعليقاً للتجربة...",
|
| 70 |
lines=3,
|
| 71 |
text_align="right"
|
| 72 |
)
|
|
|
|
| 74 |
analyze_btn = gr.Button("تحليل الانطباع 🚀", variant="primary")
|
| 75 |
|
| 76 |
with gr.Row():
|
| 77 |
+
output_text = gr.Textbox(label="التقرير", lines=4, text_align="right")
|
| 78 |
+
sentiment_badge = gr.Label(label="النتيجة")
|
| 79 |
|
| 80 |
analyze_btn.click(analyze_sentiment, inputs=input_text, outputs=[output_text, sentiment_badge])
|
| 81 |
|
| 82 |
+
# ---------------------------------------------------------
|
| 83 |
+
# 👇👇👇 هنا تمت إضافة اسم�� وتفاصيل المشروع 👇👇👇
|
| 84 |
+
# ---------------------------------------------------------
|
| 85 |
+
gr.HTML("""
|
| 86 |
+
<div style="text-align: center; margin-top: 30px; padding: 20px; border-top: 1px solid #eee;">
|
| 87 |
+
<h3 style="color: #4a5568; margin-bottom: 5px;">إعداد وتطوير: محمد طارق شلبي</h3>
|
| 88 |
+
<p style="color: #718096; font-size: 14px;">
|
| 89 |
+
🎓 مشروع تخرج الدفعة الخامسة - برنامج سفراء الذكاء الاصطناعي
|
| 90 |
+
</p>
|
| 91 |
+
</div>
|
| 92 |
+
""")
|
| 93 |
+
|
| 94 |
demo.launch()
|