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