Turbiling commited on
Commit
395869d
·
verified ·
1 Parent(s): e0c2ef5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -21
app.py CHANGED
@@ -11,29 +11,51 @@ import re
11
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
12
  client = Groq(api_key=GROQ_API_KEY)
13
 
14
- # Sample grade, subject, topic mapping
 
 
 
 
 
 
15
  grades = [f"Grade {i}" for i in range(1, 13)]
 
 
16
  subjects_by_grade = {
17
- "Grade 1": ["Mathematics", "English", "Urdu"],
18
- "Grade 2": ["Mathematics", "English", "Urdu"],
19
- "Grade 3": ["Mathematics", "English", "Urdu", "Science"],
20
- "Grade 4": ["Mathematics", "English", "Urdu", "Science"],
21
  "Grade 5": ["Mathematics", "English", "Urdu", "Science", "Islamiyat"],
22
- # ... add for all grades
 
 
 
 
 
 
23
  }
 
 
24
  topics_by_subject = {
25
- "Mathematics": ["Addition", "Subtraction", "Multiplication"],
26
- "English": ["Grammar", "Vocabulary", "Comprehension"],
27
  "Urdu": ["Harf", "Jumla", "Qissa"],
28
  "Science": ["Cell Biology", "Human Body Systems", "Plants"],
29
- "Islamiyat": ["Seerat", "Ahkam", "Aqaid"]
 
 
 
 
 
30
  }
 
31
  mediums = ["English", "Urdu"]
32
 
33
  def generate_mcqs(grade, subject, topic, medium):
34
  if medium == "Urdu":
35
  system_msg = "آپ ایک تعلیمی ماہر ہیں۔ پاکستان کے قومی نصاب کے مطابق 20 ایم سی کیو تیار کریں۔"
36
- user_msg = f"{grade} {subject} کا موضوع {topic} پر 20 ایم سی کیو تیار کریں۔ ہر سوال میں 4 آپشنز شامل ہوں، درست جواب کو الگ نہ کریں۔"
37
  else:
38
  system_msg = "You are an educational expert. Generate 20 MCQs aligned with Pakistani National Curriculum."
39
  user_msg = f"Create 20 MCQs for {grade} {subject} on the topic {topic}. Each question must have 4 options."
@@ -45,15 +67,16 @@ def generate_mcqs(grade, subject, topic, medium):
45
  ],
46
  model="llama-3.3-70b-versatile"
47
  )
 
48
  text = response.choices[0].message.content
49
  # Split questions by number pattern
50
  mcqs = re.split(r'\n?\d+\.', text)
51
  mcqs = [q.strip() for q in mcqs if q.strip()]
52
- return mcqs
53
 
54
  def export_to_word(mcqs, grade, subject, topic, medium):
55
  doc = Document()
56
- # Add heading
57
  heading = doc.add_heading("WEEKLY/MONTHLY TEST", level=1)
58
  heading.alignment = WD_ALIGN_PARAGRAPH.CENTER
59
  run = heading.runs[0]
@@ -61,7 +84,7 @@ def export_to_word(mcqs, grade, subject, topic, medium):
61
  run.font.color.rgb = RGBColor(0, 128, 0)
62
  run.font.size = Pt(18)
63
 
64
- # Add info table
65
  table = doc.add_table(rows=5, cols=2)
66
  table.style = 'Table Grid'
67
  info = [
@@ -78,13 +101,26 @@ def export_to_word(mcqs, grade, subject, topic, medium):
78
  if medium == "Urdu":
79
  row.cells[1].paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.RIGHT
80
 
81
- # Add MCQs
82
  for i, q in enumerate(mcqs, start=1):
83
- p = doc.add_paragraph(f"{i}. {q}")
84
  if medium == "Urdu":
 
 
85
  p.alignment = WD_ALIGN_PARAGRAPH.RIGHT
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
- # Save document
88
  file_path = f"{grade}_{subject}_{topic}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.docx"
89
  doc.save(file_path)
90
  return file_path
@@ -94,6 +130,7 @@ def on_generate(grade, subject, topic, medium):
94
  file_path = export_to_word(mcqs, grade, subject, topic, medium)
95
  return f"Test ready: {file_path}", file_path
96
 
 
97
  with gr.Blocks() as demo:
98
  gr.Markdown("### **MCQ Test Generator - Developer: Najaf Ali Sharqi**")
99
  with gr.Row():
@@ -106,16 +143,17 @@ with gr.Blocks() as demo:
106
  output_text = gr.Textbox(label="Status")
107
  output_file = gr.File(label="Download Test")
108
 
109
- # Update subjects when grade changes
110
  def update_subjects(selected_grade):
111
- return gr.update(choices=subjects_by_grade.get(selected_grade, []), value=None)
112
  grade_dropdown.change(update_subjects, inputs=grade_dropdown, outputs=subject_dropdown)
113
 
114
- # Update topics when subject changes
115
  def update_topics(selected_subject):
116
- return gr.update(choices=topics_by_subject.get(selected_subject, []), value=None)
117
  subject_dropdown.change(update_topics, inputs=subject_dropdown, outputs=topic_dropdown)
118
 
119
- generate_btn.click(on_generate, inputs=[grade_dropdown, subject_dropdown, topic_dropdown, medium_dropdown], outputs=[output_text, output_file])
 
120
 
121
  demo.launch(share=True, ssr_mode=False)
 
11
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
12
  client = Groq(api_key=GROQ_API_KEY)
13
 
14
+ # Urdu number mapping
15
+ urdu_numbers = ["۰","۱","۲","۳","۴","۵","۶","۷","۸","۹"]
16
+
17
+ def convert_to_urdu_number(n):
18
+ return ''.join(urdu_numbers[int(d)] for d in str(n))
19
+
20
+ # Grades
21
  grades = [f"Grade {i}" for i in range(1, 13)]
22
+
23
+ # Subjects by grade according to Pakistani curriculum
24
  subjects_by_grade = {
25
+ "Grade 1": ["Mathematics", "English", "Urdu", "Islamiyat"],
26
+ "Grade 2": ["Mathematics", "English", "Urdu", "Islamiyat"],
27
+ "Grade 3": ["Mathematics", "English", "Urdu", "Science", "Islamiyat"],
28
+ "Grade 4": ["Mathematics", "English", "Urdu", "Science", "Islamiyat"],
29
  "Grade 5": ["Mathematics", "English", "Urdu", "Science", "Islamiyat"],
30
+ "Grade 6": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "Pakistan Studies"],
31
+ "Grade 7": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "Pakistan Studies"],
32
+ "Grade 8": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "Pakistan Studies"],
33
+ "Grade 9": ["Mathematics", "English", "Urdu", "Physics", "Chemistry", "Biology", "Islamiyat", "Pakistan Studies"],
34
+ "Grade 10": ["Mathematics", "English", "Urdu", "Physics", "Chemistry", "Biology", "Islamiyat", "Pakistan Studies"],
35
+ "Grade 11": ["Mathematics", "English", "Urdu", "Physics", "Chemistry", "Biology", "Computer Science", "Pakistan Studies", "Islamiyat"],
36
+ "Grade 12": ["Mathematics", "English", "Urdu", "Physics", "Chemistry", "Biology", "Computer Science", "Pakistan Studies", "Islamiyat"]
37
  }
38
+
39
+ # Topics by subject (example topics)
40
  topics_by_subject = {
41
+ "Mathematics": ["Algebra", "Geometry", "Fractions"],
42
+ "English": ["Grammar", "Comprehension", "Vocabulary"],
43
  "Urdu": ["Harf", "Jumla", "Qissa"],
44
  "Science": ["Cell Biology", "Human Body Systems", "Plants"],
45
+ "Physics": ["Mechanics", "Optics", "Electricity"],
46
+ "Chemistry": ["Elements & Compounds", "Chemical Reactions", "Periodic Table"],
47
+ "Biology": ["Genetics", "Ecology", "Microbiology"],
48
+ "Islamiyat": ["Seerat", "Ahkam", "Aqaid"],
49
+ "Pakistan Studies": ["History", "Geography", "Constitution"],
50
+ "Computer Science": ["Programming", "Data Structures", "Networks"]
51
  }
52
+
53
  mediums = ["English", "Urdu"]
54
 
55
  def generate_mcqs(grade, subject, topic, medium):
56
  if medium == "Urdu":
57
  system_msg = "آپ ایک تعلیمی ماہر ہیں۔ پاکستان کے قومی نصاب کے مطابق 20 ایم سی کیو تیار کریں۔"
58
+ user_msg = f"{grade} {subject} کا موضوع {topic} پر 20 ایم سی کیو تیار کریں۔ ہر سوال میں 4 آپشنز شامل ہوں، جوابات الگ نہ کریں۔"
59
  else:
60
  system_msg = "You are an educational expert. Generate 20 MCQs aligned with Pakistani National Curriculum."
61
  user_msg = f"Create 20 MCQs for {grade} {subject} on the topic {topic}. Each question must have 4 options."
 
67
  ],
68
  model="llama-3.3-70b-versatile"
69
  )
70
+
71
  text = response.choices[0].message.content
72
  # Split questions by number pattern
73
  mcqs = re.split(r'\n?\d+\.', text)
74
  mcqs = [q.strip() for q in mcqs if q.strip()]
75
+ return mcqs[:20] # Ensure exactly 20 questions
76
 
77
  def export_to_word(mcqs, grade, subject, topic, medium):
78
  doc = Document()
79
+ # Heading
80
  heading = doc.add_heading("WEEKLY/MONTHLY TEST", level=1)
81
  heading.alignment = WD_ALIGN_PARAGRAPH.CENTER
82
  run = heading.runs[0]
 
84
  run.font.color.rgb = RGBColor(0, 128, 0)
85
  run.font.size = Pt(18)
86
 
87
+ # Info table
88
  table = doc.add_table(rows=5, cols=2)
89
  table.style = 'Table Grid'
90
  info = [
 
101
  if medium == "Urdu":
102
  row.cells[1].paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.RIGHT
103
 
104
+ # MCQs
105
  for i, q in enumerate(mcqs, start=1):
 
106
  if medium == "Urdu":
107
+ number = convert_to_urdu_number(i)
108
+ p = doc.add_paragraph(f"{q} {number}.")
109
  p.alignment = WD_ALIGN_PARAGRAPH.RIGHT
110
+ else:
111
+ p = doc.add_paragraph(f"{i}. {q}")
112
+
113
+ # Answer key table (5 columns)
114
+ ans_table = doc.add_table(rows=5, cols=5)
115
+ ans_table.style = 'Table Grid'
116
+ for i in range(20):
117
+ row = ans_table.rows[i % 5]
118
+ col = i % 5
119
+ row.cells[col].text = f"Q{i+1}: ____"
120
+ if medium == "Urdu":
121
+ row.cells[col].paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.RIGHT
122
 
123
+ # Save file
124
  file_path = f"{grade}_{subject}_{topic}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.docx"
125
  doc.save(file_path)
126
  return file_path
 
130
  file_path = export_to_word(mcqs, grade, subject, topic, medium)
131
  return f"Test ready: {file_path}", file_path
132
 
133
+ # Gradio UI
134
  with gr.Blocks() as demo:
135
  gr.Markdown("### **MCQ Test Generator - Developer: Najaf Ali Sharqi**")
136
  with gr.Row():
 
143
  output_text = gr.Textbox(label="Status")
144
  output_file = gr.File(label="Download Test")
145
 
146
+ # Update subjects dynamically
147
  def update_subjects(selected_grade):
148
+ return gr.Dropdown.update(choices=subjects_by_grade.get(selected_grade, []), value=None)
149
  grade_dropdown.change(update_subjects, inputs=grade_dropdown, outputs=subject_dropdown)
150
 
151
+ # Update topics dynamically
152
  def update_topics(selected_subject):
153
+ return gr.Dropdown.update(choices=topics_by_subject.get(selected_subject, []), value=None)
154
  subject_dropdown.change(update_topics, inputs=subject_dropdown, outputs=topic_dropdown)
155
 
156
+ generate_btn.click(on_generate, inputs=[grade_dropdown, subject_dropdown, topic_dropdown, medium_dropdown],
157
+ outputs=[output_text, output_file])
158
 
159
  demo.launch(share=True, ssr_mode=False)