Sammaali commited on
Commit
4a62ede
·
verified ·
1 Parent(s): 068f21d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py CHANGED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import gradio as gr
4
+ from mistralai import Mistral
5
+
6
+
7
+ api_key = os.environ["MISTRAL_API_KEY"]
8
+ client = Mistral(api_key=api_key)
9
+ model_name = "open-mistral-nemo"
10
+
11
+
12
+ def generate_MCQquestion(text):
13
+
14
+ prompt = f"""
15
+ Context: {text}
16
+
17
+ Task: Generate 3 Multiple Choice Questions (MCQs) in Arabic based on the context.
18
+
19
+ Requirements:
20
+ - Clear and direct questions.
21
+ - Medium difficulty.
22
+ - 4 options per question.
23
+ - Only one correct answer.
24
+ - Output MUST be valid JSON only.
25
+
26
+ JSON Structure:
27
+ {{
28
+ "mcqs": [
29
+ {{
30
+ "question": "string",
31
+ "options": ["opt1", "opt2", "opt3", "opt4"],
32
+ "answer_index": integer
33
+ }}
34
+ ]
35
+ }}
36
+ """
37
+
38
+ try:
39
+ response = client.chat.complete(
40
+ model=model_name,
41
+ messages=[
42
+ {"role": "user", "content": prompt}
43
+ ],
44
+ response_format={"type": "json_object"},
45
+ temperature=0.7
46
+ )
47
+
48
+ content = response.choices[0].message.content
49
+
50
+ data = json.loads(content)
51
+ result_text = ""
52
+
53
+ labels = ["أ", "ب", "ج", "د"]
54
+
55
+ for i, item in enumerate(data["mcqs"], 1):
56
+
57
+ result_text += f"❓ السؤال {i}:\n"
58
+ result_text += f"{item['question']}\n\n"
59
+
60
+ for idx, option in enumerate(item["options"]):
61
+
62
+ if idx == item["answer_index"]:
63
+ result_text += f"✅ {labels[idx]}) {option}\n"
64
+ else:
65
+ result_text += f" {labels[idx]}) {option}\n"
66
+
67
+ result_text += "\n----------------------\n\n"
68
+
69
+ return result_text
70
+
71
+ except Exception as e:
72
+ return f"حدث خطأ:\n{str(e)}"
73
+
74
+
75
+ # Gradio UI
76
+ demo = gr.Interface(
77
+ fn=generate_MCQquestion,
78
+ inputs=gr.Textbox(
79
+ lines=10,
80
+ label="أدخل النص"
81
+ ),
82
+ outputs=gr.Textbox(
83
+ lines=20,
84
+ label="الأسئلة"
85
+ ),
86
+ title="مولد أسئلة اختيار من متعدد (MCQ)",
87
+ description="باستخدام Mistral Nemo"
88
+ )
89
+
90
+ demo.launch()