Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import os
|
| 3 |
+
from google import genai
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
API_KEY = os.environ.get("gimin_mcq")
|
| 7 |
+
|
| 8 |
+
if API_KEY:
|
| 9 |
+
client = genai.Client(api_key=API_KEY)
|
| 10 |
+
else:
|
| 11 |
+
client = genai.Client()
|
| 12 |
+
|
| 13 |
+
MODEL_NAME = "gemini-1.5-flash"
|
| 14 |
+
|
| 15 |
+
def generate_main_question_gemini(paragraph: str, difficulty: str = "كتيير سهل"):
|
| 16 |
+
if not paragraph or paragraph.strip() == "":
|
| 17 |
+
return "رجاءً أدخل فقرة أولاً."
|
| 18 |
+
|
| 19 |
+
prompt = f"""
|
| 20 |
+
الفقرة التالية:
|
| 21 |
+
{paragraph}
|
| 22 |
+
|
| 23 |
+
المطلوب:
|
| 24 |
+
- بشكل بسيط أنشئ سؤالًا أساسيًا باللغة العربية (مستوى الصعوبة: {difficulty}).
|
| 25 |
+
"""
|
| 26 |
+
try:
|
| 27 |
+
response = client.models.generate_content(model=MODEL_NAME, contents=prompt)
|
| 28 |
+
return response.text.strip()
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"حدث خطأ أثناء الاتصال بالـ API: {e}"
|
| 31 |
+
|
| 32 |
+
with gr.Blocks() as demo:
|
| 33 |
+
gr.Markdown("## MainQuestion — مولّد سؤال أساسي (عربي)")
|
| 34 |
+
paragraph = gr.Textbox(label="الفقرة (النص)", lines=8, placeholder="ألصق الفقرة هون...")
|
| 35 |
+
difficulty = gr.Dropdown(label="مستوى الصعوبة", choices=["كتيير سهل","سهل","متوسط","صعب"], value="كتيير سهل")
|
| 36 |
+
output = gr.Textbox(label="السؤال الأساسي", lines=3)
|
| 37 |
+
run_btn = gr.Button("إنشئ السؤال")
|
| 38 |
+
run_btn.click(fn=generate_main_question_gemini, inputs=[paragraph, difficulty], outputs=output)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
demo.launch(share=True, show_error=True)
|