Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,81 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
from mistralai import Mistral
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
model_name = "mistral-small-latest"
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def generate_question(text):
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
prompt = f"""
|
| 12 |
You are an expert Arabic educational content designer.
|
| 13 |
|
| 14 |
TASK:
|
| 15 |
-
Generate ONE comprehensive question that covers the main
|
| 16 |
|
| 17 |
STRICT RULES:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
2. The question should:
|
| 25 |
-
- Cover the main idea of the text
|
| 26 |
-
- Encourage recalling most key points
|
| 27 |
-
- NOT require listing every tiny detail
|
| 28 |
-
|
| 29 |
-
3. DO NOT:
|
| 30 |
-
- Repeat phrases
|
| 31 |
-
- Ask multiple questions
|
| 32 |
-
- Explain anything
|
| 33 |
-
- Add analysis or commentary
|
| 34 |
-
|
| 35 |
-
4. Output ONLY the question.
|
| 36 |
|
| 37 |
TEXT:
|
| 38 |
{text}
|
| 39 |
|
| 40 |
FINAL QUESTION:
|
| 41 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
-
response =
|
| 44 |
-
|
| 45 |
-
messages=[{"role": "user", "content": prompt}],
|
| 46 |
-
temperature=0.2
|
| 47 |
-
)
|
| 48 |
|
| 49 |
-
return
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
+
# --- إعداد API ---
|
| 6 |
+
API_KEY = os.environ.get("MISTRAL_API_KEY")
|
|
|
|
| 7 |
|
| 8 |
+
if not API_KEY:
|
| 9 |
+
raise ValueError("❌ لازم تحط MISTRAL_API_KEY")
|
| 10 |
+
|
| 11 |
+
API_URL = "https://api.mistral.ai/v1/chat/completions"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# --- توليد السؤال ---
|
| 15 |
def generate_question(text):
|
| 16 |
|
| 17 |
+
if not text.strip():
|
| 18 |
+
return "يرجى إدخال نص"
|
| 19 |
+
|
| 20 |
prompt = f"""
|
| 21 |
You are an expert Arabic educational content designer.
|
| 22 |
|
| 23 |
TASK:
|
| 24 |
+
Generate ONE comprehensive question that covers the main idea of the text.
|
| 25 |
|
| 26 |
STRICT RULES:
|
| 27 |
+
- Maximum 25 words
|
| 28 |
+
- One sentence only
|
| 29 |
+
- No repetition
|
| 30 |
+
- No explanation
|
| 31 |
+
- Use clear Modern Standard Arabic
|
| 32 |
+
- The question should start with: "ما" or "كيف"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
TEXT:
|
| 35 |
{text}
|
| 36 |
|
| 37 |
FINAL QUESTION:
|
| 38 |
"""
|
| 39 |
+
|
| 40 |
+
headers = {
|
| 41 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 42 |
+
"Content-Type": "application/json"
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
data = {
|
| 46 |
+
"model": "mistral-small-latest",
|
| 47 |
+
"messages": [
|
| 48 |
+
{"role": "user", "content": prompt}
|
| 49 |
+
],
|
| 50 |
+
"temperature": 0.2
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
try:
|
| 54 |
+
response = requests.post(API_URL, headers=headers, json=data)
|
| 55 |
+
result = response.json()
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
return result["choices"][0]["message"]["content"].strip()
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
+
return f"❌ خطأ: {str(e)}"
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# --- واجهة Gradio ---
|
| 64 |
+
demo = gr.Interface(
|
| 65 |
+
fn=generate_question,
|
| 66 |
+
inputs=gr.Textbox(
|
| 67 |
+
lines=10,
|
| 68 |
+
label="📘 الفقرة",
|
| 69 |
+
placeholder="ضع الفقرة هنا..."
|
| 70 |
+
),
|
| 71 |
+
outputs=gr.Textbox(
|
| 72 |
+
lines=3,
|
| 73 |
+
label="❓ السؤال العام"
|
| 74 |
+
),
|
| 75 |
+
title="🎓 مولد السؤال الشامل",
|
| 76 |
+
description="يولد سؤالاً واحداً شاملاً يغطي الفكرة العامة للنص."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# --- تشغيل ---
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
demo.launch()
|