Entibaa / app.py
shalabyelectronics's picture
Update app.py
f2b4067 verified
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()