Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,52 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# جلب التوكن
|
| 6 |
-
# تأكد من أنك سميت السر "HF_TOKEN" في إعدادات الـ Space
|
| 7 |
hf_token = os.getenv("HF_TOKEN")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
def analyze_response(prompt):
|
| 14 |
if not prompt:
|
| 15 |
-
return "يرجى كتابة
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
-
#
|
| 19 |
-
response =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
-
return f"
|
| 22 |
-
|
| 23 |
-
# خوارزمية تقييم بسيطة
|
| 24 |
-
# نعتبر أن النموذج فهم إذا كان الرد أطول من 20 حرفاً
|
| 25 |
-
evaluation = ""
|
| 26 |
-
if len(response) > 20:
|
| 27 |
-
evaluation = "النموذج فهم الإدخال وقدم إجابة مفصلة ✅"
|
| 28 |
-
else:
|
| 29 |
-
evaluation = "الإجابة قصيرة جداً، قد لا يكون الفهم كاملاً ⚠️"
|
| 30 |
-
|
| 31 |
-
return response, evaluation
|
| 32 |
|
| 33 |
-
# بناء
|
| 34 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 35 |
gr.Markdown("# 🤖 اختبار فهم النماذج للنصوص (Prompt Understanding Test)")
|
| 36 |
-
gr.Markdown("اكتب أي سؤال أو جملة وسيقوم النموذج بالرد وتقييم مدى فهمه.")
|
| 37 |
|
| 38 |
with gr.Row():
|
| 39 |
-
input_text = gr.Textbox(label="أدخل الـ Prompt هنا", placeholder="مثال:
|
| 40 |
|
| 41 |
btn = gr.Button("إرسال للتحليل", variant="primary")
|
| 42 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# جلب التوكن
|
|
|
|
| 6 |
hf_token = os.getenv("HF_TOKEN")
|
| 7 |
|
| 8 |
+
# سنستخدم نموذج Zephyr القوي جداً والمفتوح
|
| 9 |
+
API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
| 10 |
+
headers = {"Authorization": f"Bearer {hf_token}"}
|
| 11 |
|
| 12 |
def analyze_response(prompt):
|
| 13 |
if not prompt:
|
| 14 |
+
return "يرجى كتابة نص.", "غير محدد ⚪"
|
| 15 |
+
|
| 16 |
+
# تجهيز الرسالة بصيغة الشات ليفهمها النموذج
|
| 17 |
+
payload = {
|
| 18 |
+
"inputs": f"<|system|>\nYou are a helpful assistant.\n<|user|>\n{prompt}\n<|assistant|>\n",
|
| 19 |
+
"parameters": {"max_new_tokens": 200}
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
try:
|
| 23 |
+
# الاتصال المباشر بالسيرفر (أضمن طريقة)
|
| 24 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 25 |
+
json_response = response.json()
|
| 26 |
+
|
| 27 |
+
# استخراج النص من الرد
|
| 28 |
+
if isinstance(json_response, list) and 'generated_text' in json_response[0]:
|
| 29 |
+
generated_text = json_response[0]['generated_text']
|
| 30 |
+
# تنظيف الرد لحذف السؤال الأصلي
|
| 31 |
+
final_answer = generated_text.split("<|assistant|>\n")[-1].strip()
|
| 32 |
+
elif 'error' in json_response:
|
| 33 |
+
return f"خطأ من النموذج: {json_response['error']}", "فشل ❌"
|
| 34 |
+
else:
|
| 35 |
+
final_answer = str(json_response)
|
| 36 |
+
|
| 37 |
+
# التقييم
|
| 38 |
+
evaluation = "✅ النموذج فهم وأجاب" if len(final_answer) > 10 else "⚠️ إجابة قصيرة"
|
| 39 |
+
return final_answer, evaluation
|
| 40 |
+
|
| 41 |
except Exception as e:
|
| 42 |
+
return f"خطأ في الاتصال: {str(e)}", "فشل ❌"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# بناء الواجهة
|
| 45 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 46 |
gr.Markdown("# 🤖 اختبار فهم النماذج للنصوص (Prompt Understanding Test)")
|
|
|
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
+
input_text = gr.Textbox(label="أدخل الـ Prompt هنا", placeholder="مثال: Explain AI simply")
|
| 50 |
|
| 51 |
btn = gr.Button("إرسال للتحليل", variant="primary")
|
| 52 |
|