Turbiling commited on
Commit
80a2554
·
verified ·
1 Parent(s): 943c096

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +270 -287
app.py CHANGED
@@ -1,318 +1,301 @@
1
- import os
2
  import gradio as gr
3
- from groq import Groq
4
- from datetime import datetime
5
- from docx import Document
6
- from docx.shared import Pt, RGBColor
7
- from docx.enum.text import WD_ALIGN_PARAGRAPH
8
- import re
9
  import random
10
 
11
- # Initialize GROQ client
12
- GROQ_API_KEY = os.getenv("GROQ_API_KEY")
13
- client = Groq(api_key=GROQ_API_KEY)
14
-
15
- # Grades and subjects
16
- grades = [f"Grade {i}" for i in range(1, 13)]
17
- subjects_by_grade = {
18
- "Grade 1": ["Mathematics", "English", "Urdu", "Islamiyat", "General Knowledge"],
19
- "Grade 2": ["Mathematics", "English", "Urdu", "Islamiyat", "General Knowledge"],
20
- "Grade 3": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "General Knowledge"],
21
- "Grade 4": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "General Knowledge"],
22
- "Grade 5": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "Social Studies"],
23
- "Grade 6": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "Social Studies"],
24
- "Grade 7": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "Social Studies", "Computer"],
25
- "Grade 8": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "Social Studies", "Computer"],
26
- "Grade 9": ["Mathematics", "English", "Urdu", "Biology", "Chemistry", "Physics", "Islamiyat", "Pakistan Studies"],
27
- "Grade 10": ["Mathematics", "English", "Urdu", "Biology", "Chemistry", "Physics", "Islamiyat", "Pakistan Studies"],
28
- "Grade 11": ["Mathematics", "English", "Urdu", "Biology", "Chemistry", "Physics", "Computer", "Islamiyat", "Pakistan Studies"],
29
- "Grade 12": ["Mathematics", "English", "Urdu", "Biology", "Chemistry", "Physics", "Computer", "Islamiyat", "Pakistan Studies"]
30
- }
31
 
32
- # Complete chapter lists for all subjects & grades (Option A: chapter-based system)
33
- # For Grades 9 & 10, I used the exact chapters you supplied for Biology, Chemistry, Physics, Pakistan Studies.
34
- chapters_by_subject_and_grade = {
35
- "Grade 1": {
36
- "Mathematics": ["Counting & Numbers", "Basic Addition", "Basic Subtraction", "Shapes", "Measurements"],
37
- "English": ["Alphabet & Sounds", "Basic Words", "Simple Sentences", "Listening & Speaking", "Reading Short Texts"],
38
- "Urdu": ["حروف", "الفاظ", "سادہ جملے", "پڑھنا", "لکھنا"],
39
- "Islamiyat": ["Basic Beliefs", "Prophets Stories", "Good Manners", "Prayers & Worship"],
40
- "General Knowledge": ["My Body", "My Home", "My School", "Seasons"]
41
- },
42
- "Grade 2": {
43
- "Mathematics": ["Place Value", "Addition & Subtraction", "Money", "Time", "Shapes"],
44
- "English": ["Parts of Speech Basics", "Simple Grammar", "Short Comprehension", "Vocabulary Building"],
45
- "Urdu": ["الفاظ و جملے", "مختصر کہانیاں", "حروفِ صحیح"],
46
- "Islamiyat": ["Prophets", "Good Deeds", "Mosque Etiquette"],
47
- "General Knowledge": ["Plants & Animals", "Local Community", "Transport"]
48
- },
49
- "Grade 3": {
50
- "Mathematics": ["Multiplication", "Division", "Fractions", "Geometry Basics", "Measurement"],
51
- "English": ["Tenses (simple)", "Comprehension Passages", "Vocabulary", "Sentence Formation"],
52
- "Urdu": ["کالم و مضامین", "قواعدِ اردو", "کہانیاں"],
53
- "Science": ["Plants", "Animals", "Human Body", "Materials"],
54
- "Islamiyat": ["Basics of Iman", "Seerat", "Manners"]
55
- },
56
- "Grade 4": {
57
- "Mathematics": ["Fractions & Decimals", "Geometry", "Data Handling", "Word Problems"],
58
- "English": ["Grammar (intermediate)", "Comprehension", "Writing Short Paragraphs"],
59
- "Urdu": ["نظم و نثر", "قواعد", "مفردات"],
60
- "Science": ["Ecosystems", "Forces", "Heat & Energy"],
61
- "Islamiyat": ["Pillars of Islam", "Seerah Stories"]
62
- },
63
- "Grade 5": {
64
- "Mathematics": ["Numbers & Operations", "Geometry", "Statistics", "Ratio & Proportion"],
65
- "English": ["Advanced Grammar Basics", "Paragraph Writing", "Comprehension"],
66
- "Urdu": ["ادبی مطالعہ", "تعبیر و تشریح"],
67
- "Science": ["Cells & Life", "Matter", "Energy"],
68
- "Islamiyat": ["Quranic Stories", "Akhlq"]
69
- },
70
- "Grade 6": {
71
- "Mathematics": ["Number System", "Algebra Introduction", "Geometry", "Data Handling"],
72
- "English": ["Grammar (complex)", "Essay Writing", "Comprehension Skills"],
73
- "Urdu": ["ادب و نحو", "تحریر"],
74
- "Science": ["Atoms & Molecules", "Human Body", "Plants"],
75
- "Islamiyat": ["Seerah & Ahkam"]
76
- },
77
- "Grade 7": {
78
- "Mathematics": ["Algebra", "Geometry", "Mensuration", "Statistics"],
79
- "English": ["Poetry & Prose", "Grammar", "Comprehension"],
80
- "Urdu": ["نثر و شاعری", "قواعد"],
81
- "Science": ["Cell Biology", "Human Systems", "Electricity Basics"],
82
- "Islamiyat": ["Islamic History", "Moral Teachings"],
83
- "Social Studies": ["Geography Basics", "History", "Civics"],
84
- "Computer": ["Basic ICT", "Computer Hardware", "Introduction to Coding"]
85
- },
86
- "Grade 8": {
87
- "Mathematics": ["Linear Equations", "Geometry", "Pythagoras", "Graphs"],
88
- "English": ["Advanced Comprehension", "Composition", "Grammar"],
89
- "Urdu": ["تجزیہ ادب", "تحقیقِ مختصر"],
90
- "Science": ["Forces & Motion", "Heat", "Waves Basics"],
91
- "Islamiyat": ["Islamic Law Basics", "Seerah"],
92
- "Social Studies": ["Regional Geography", "Historical Events"],
93
- "Computer": ["Algorithms", "Basic Programming"]
94
- },
95
  "Grade 9": {
96
- "Mathematics": ["Number Systems & Algebra", "Coordinate Geometry", "Trigonometry", "Graphs"],
97
- "English": ["Advanced Grammar", "Comprehension", "Writing Skills"],
98
- "Urdu": ["ادب اور نثر", "نظم", "مضامین"],
99
  "Biology": [
100
- "INTRODUCTION TO BIOLOGY",
101
- "SOLVING A BIOLOGICAL PROBLEM",
102
- "BIODIVERSITY",
103
- "CELLS AND TISSUES",
104
- "CELL CYCLE",
105
- "ENZYMES",
106
- "BIOENERGETICS",
107
- "NUTRITION",
108
- "TRANSPORT"
109
  ],
110
  "Chemistry": [
111
- "FUNDAMENTALS OF CHEMISTRY",
112
- "STRUCTURE OF ATOMS",
113
- "PERIODIC TABLE AND PERIODICITY OF PROPERTIES",
114
- "STRUCTURE OF MOLECULES",
115
- "PHYSICAL STATES OF MATTER",
116
- "SOLUTIONS",
117
- "ELECTROCHEMISTRY",
118
- "CHEMICAL REACTIVITY"
119
  ],
120
  "Physics": [
121
- "PHYSICAL QUANTITIES AND MEASUREMENT",
122
- "KINEMATICS",
123
- "DYNAMICS",
124
  "Turning Effect of Forces",
125
- "GRAVITATION",
126
- "WORK AND ENERGY",
127
- "PROPERTIES OF MATTER",
128
- "THERMAL PROPERTIES OF MATTER",
129
- "TRANSFER OF HEAT"
130
  ],
131
- "Islamiyat": ["Seerat of Prophet", "Faith & Beliefs", "Worship Practices"],
132
  "Pakistan Studies": [
133
- "IDEOLOGICAL BASIS OF PAKISTAN",
134
- "MAKING OF PAKISTAN",
135
- "LAND AND ENVIRONMENT",
136
- "HISTORY OF PAKISTAN"
137
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  },
 
139
  "Grade 10": {
140
- "Mathematics": ["Advanced Algebra", "Trigonometry", "Geometry", "Probability"],
141
- "English": ["Literature", "Advanced Composition", "Comprehension"],
142
- "Urdu": ["ادبِ جدید", "تحقیقی مضمون"],
143
  "Biology": [
144
- "GASEOUS EXCHANGE",
145
- "HOMEOSTASIS",
146
- "COORDINATION",
147
- "SUPPORT AND MOVEMENT",
148
- "REPRODUCTION",
149
- "INHERITANCE",
150
- "MAN AND HIS ENVIRONMENT",
151
- "BIOTECHNOLOGY",
152
- "PHARMA"
153
  ],
154
  "Chemistry": [
155
- "CHEMICAL EQUILIBRIUM",
156
- "ACIDS, BASES AND SALTS",
157
- "ORGANIC CHEMISTRY",
158
- "HYDROCARBONS",
159
- "BIOCHEMISTRY",
160
- "ENVIRONMENTAL CHEMISTRY I: THE ATMOSPHERE",
161
- "ENVIRONMENTAL CHEMISTRY II: WATER",
162
- "CHEMICAL INDUSTRIES"
163
  ],
164
  "Physics": [
165
- "SIMPLE HARMONIC MOTION AND WAVES",
166
- "SOUND",
167
- "GEOMETRICAL OPTICS",
168
- "ELECTROSTATICS",
169
- "CURRENT ELECTRICITY",
170
- "ELECTROMAGNETISM",
171
- "INTRODUCTORY ELECTRONICS",
172
- "INFORMATION AND COMMUNICATION TECHNOLOGY",
173
- "RADIOACTIVITY"
174
  ],
175
- "Islamiyat": ["Islamic Studies II", "Ethics & Society", "Fiqh Basics"],
176
  "Pakistan Studies": [
177
- "HISTORY OF PAKISTAN-II",
178
- "PAKISTAN IN WORLD AFFAIRS",
179
- "ECONOMIC DEVELOPMENTS",
180
- "POPULATION, SOCIETY AND CULTURE OF PAKISTAN"
181
- ]
182
- },
183
- "Grade 11": {
184
- "Mathematics": ["Advanced Algebra & Calculus Intro", "Coordinate Geometry", "Trigonometry", "Vectors"],
185
- "English": ["Academic Writing", "Literature Studies", "Critical Reading"],
186
- "Urdu": ["ادبی مطالعہ", "نظم و نثر کی تشریح"],
187
- "Biology": ["Molecular Biology", "Plant Physiology", "Animal Physiology", "Genetics"],
188
- "Chemistry": ["Physical Chemistry", "Inorganic Chemistry", "Organic Reactions"],
189
- "Physics": ["Mechanics", "Waves", "Thermodynamics", "Electricity & Magnetism"],
190
- "Computer": ["Programming Fundamentals", "Data Structures Intro", "Databases"],
191
- "Islamiyat": ["Advanced Seerah", "Comparative Religion"],
192
- "Pakistan Studies": ["Modern History", "Government & Politics"]
193
- },
194
- "Grade 12": {
195
- "Mathematics": ["Calculus", "Advanced Algebra", "Statistics", "Analytical Geometry"],
196
- "English": ["Research Writing", "Advanced Literature", "Critical Analysis"],
197
- "Urdu": ["تحقیق و تنقید", "ادبی شخصیات"],
198
- "Biology": ["Cell Signalling", "Developmental Biology", "Human Physiology"],
199
- "Chemistry": ["Biochemistry", "Industrial Chemistry", "Environmental Chemistry"],
200
- "Physics": ["Modern Physics", "Electrodynamics", "Optics", "Quantum Basics"],
201
- "Computer": ["Algorithms & Complexity", "Web Technologies"],
202
- "Islamiyat": ["Advanced Islamic Thought", "Islamic Jurisprudence"],
203
- "Pakistan Studies": ["Contemporary Issues", "Economic Policies"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
  }
205
  }
206
 
207
- mediums = ["English"]
 
 
208
 
209
- def generate_mcqs(grade, subject, chapter):
210
- # Generate English MCQs aligned with Pakistani curriculum
211
- system_msg = "You are an educational expert. Generate 20 MCQs strictly aligned with the Pakistani National Curriculum."
212
- user_msg = f"Create 20 MCQs for {grade} {subject} on the chapter/topic '{chapter}'. Each question must have 4 options. Do not add extra sentences before or after. Answers will be provided separately in the answer key table."
 
 
 
213
 
214
- response = client.chat.completions.create(
215
- messages=[
216
- {"role": "system", "content": system_msg},
217
- {"role": "user", "content": user_msg}
218
- ],
219
- model="llama-3.3-70b-versatile"
220
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
 
222
- text = response.choices[0].message.content
223
- mcqs = re.split(r'\n?\d+\.', text)
224
- mcqs = [q.strip() for q in mcqs if q.strip()]
225
-
226
- # Random answer key for demo
227
- answer_key = [random.choice(["A","B","C","D"]) for _ in mcqs]
228
-
229
- return mcqs[:20], answer_key[:20]
230
-
231
- def export_to_word(mcqs, answers, grade, subject, chapter):
232
- doc = Document()
233
-
234
- # Heading
235
- heading = doc.add_heading("WEEKLY/MONTHLY TEST", level=1)
236
- heading.alignment = WD_ALIGN_PARAGRAPH.CENTER
237
- run = heading.runs[0]
238
- run.bold = True
239
- run.font.color.rgb = RGBColor(0, 128, 0)
240
- run.font.size = Pt(18)
241
-
242
- # Info table
243
- table = doc.add_table(rows=5, cols=2)
244
- table.style = 'Table Grid'
245
- info = [
246
- ("Grade", grade),
247
- ("Subject", subject),
248
- ("Chapter", chapter),
249
- ("Date", datetime.now().strftime("%d-%m-%Y")),
250
- ("Medium", "English")
251
- ]
252
- for row, (label, value) in zip(table.rows, info):
253
- row.cells[0].text = label
254
- row.cells[1].text = value
255
- row.cells[0].paragraphs[0].runs[0].bold = True
256
-
257
- # Add MCQs
258
- for i, q in enumerate(mcqs, start=1):
259
- p = doc.add_paragraph(f"{i}. {q}")
260
- p.alignment = WD_ALIGN_PARAGRAPH.LEFT
261
-
262
- # Answer key table
263
- doc.add_paragraph("\nAnswer Key:")
264
- ans_table = doc.add_table(rows=21, cols=5)
265
- ans_table.style = 'Table Grid'
266
- headers = ["Q#", "A", "B", "C", "D"]
267
- for idx, text in enumerate(headers):
268
- ans_table.cell(0, idx).text = text
269
- ans_table.cell(0, idx).paragraphs[0].runs[0].bold = True
270
-
271
- for r, ans in enumerate(answers, start=1):
272
- ans_table.cell(r,0).text = str(r)
273
- for c, option in enumerate(["A","B","C","D"], start=1):
274
- ans_table.cell(r,c).text = "✔" if ans==option else ""
275
-
276
- safe_chapter = re.sub(r'[\\/*?:"<>|]', "_", chapter)
277
- file_path = f"{grade}_{subject}_{safe_chapter}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.docx"
278
- doc.save(file_path)
279
- return file_path
280
-
281
- def on_generate(grade, subject, chapter):
282
- mcqs, answers = generate_mcqs(grade, subject, chapter)
283
- file_path = export_to_word(mcqs, answers, grade, subject, chapter)
284
- return f"Test ready: {file_path}", file_path
285
-
286
- # Gradio UI
287
- with gr.Blocks() as demo:
288
- gr.Markdown("### **MCQ Test Generator - Developer: Najaf Ali Sharqi**")
289
  with gr.Row():
290
- grade_dropdown = gr.Dropdown(choices=grades, label="Select Grade")
291
- subject_dropdown = gr.Dropdown(choices=[], label="Select Subject")
292
- chapter_dropdown = gr.Dropdown(choices=[], label="Select Chapter")
293
-
294
- generate_btn = gr.Button("Generate Test")
295
- output_text = gr.Textbox(label="Status")
296
- output_file = gr.File(label="Download Test")
297
-
298
- def update_subjects(selected_grade):
299
- return gr.update(choices=subjects_by_grade.get(selected_grade, []), value=None)
300
- grade_dropdown.change(update_subjects, inputs=grade_dropdown, outputs=subject_dropdown)
301
-
302
- def update_chapters(selected_subject, selected_grade):
303
- # Look up chapters for the specific grade and subject
304
- chapters = chapters_by_subject_and_grade.get(selected_grade, {}).get(selected_subject, [])
305
- # If not found, return an empty list so the user knows it's unsupported
306
- return gr.update(choices=chapters, value=None)
307
-
308
- # When subject changes, update chapters. Also update when grade changes to refresh available chapters.
309
- subject_dropdown.change(update_chapters, inputs=[subject_dropdown, grade_dropdown], outputs=chapter_dropdown)
310
- grade_dropdown.change(update_chapters, inputs=[subject_dropdown, grade_dropdown], outputs=chapter_dropdown)
311
-
312
- generate_btn.click(
313
- on_generate,
314
- inputs=[grade_dropdown, subject_dropdown, chapter_dropdown],
315
- outputs=[output_text, output_file]
 
 
 
 
316
  )
317
 
318
- demo.launch(share=True, ssr_mode=False)
 
 
1
  import gradio as gr
 
 
 
 
 
 
2
  import random
3
 
4
+ # ---------------------------------------------------------
5
+ # SUBJECTS + CHAPTERS (Existing + Newly Added)
6
+ # ---------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ curriculum = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  "Grade 9": {
 
 
 
10
  "Biology": [
11
+ "Introduction to Biology",
12
+ "Solving a Biological Problem",
13
+ "Biodiversity",
14
+ "Cells and Tissues",
15
+ "Cell Cycle",
16
+ "Enzymes",
17
+ "Bioenergetics",
18
+ "Nutrition",
19
+ "Transport"
20
  ],
21
  "Chemistry": [
22
+ "Fundamentals of Chemistry",
23
+ "Structure of Atoms",
24
+ "Periodic Table",
25
+ "Structure of Molecules",
26
+ "Physical States of Matter",
27
+ "Solutions",
28
+ "Electrochemistry",
29
+ "Chemical Reactivity"
30
  ],
31
  "Physics": [
32
+ "Physical Quantities & Measurement",
33
+ "Kinematics",
34
+ "Dynamics",
35
  "Turning Effect of Forces",
36
+ "Gravitation",
37
+ "Work and Energy",
38
+ "Properties of Matter",
39
+ "Thermal Properties of Matter",
40
+ "Transfer of Heat"
41
  ],
 
42
  "Pakistan Studies": [
43
+ "Ideological Basis of Pakistan",
44
+ "Pakistan Movement",
45
+ "Land & Environment",
46
+ "History of Pakistan (1857–1947)",
47
+ "Economic Development"
48
+ ],
49
+
50
+ # ======================
51
+ # NEW COMPULSORY SUBJECTS
52
+ # ======================
53
+
54
+ "Mathematics": [
55
+ "Matrices & Determinants",
56
+ "Real & Complex Numbers",
57
+ "Logarithms",
58
+ "Algebraic Expressions",
59
+ "Factorization",
60
+ "Linear Equations",
61
+ "Quadratic Equations",
62
+ "Ratio & Proportion",
63
+ "Financial Arithmetic",
64
+ "Geometry"
65
+ ],
66
+
67
+ "English": [
68
+ "Reading Comprehension",
69
+ "Poetry",
70
+ "Grammar & Composition",
71
+ "Letter Writing",
72
+ "Story Writing",
73
+ "Tenses",
74
+ "Vocabulary"
75
+ ],
76
+
77
+ "Urdu": [
78
+ "Nazms",
79
+ "Ghazals",
80
+ "Essays",
81
+ "Comprehension",
82
+ "Grammar",
83
+ "Muhavray",
84
+ "Tashreeh"
85
+ ],
86
+
87
+ "Islamiyat": [
88
+ "Quranic Passages",
89
+ "Ahadith",
90
+ "Seerat-un-Nabi",
91
+ "Islamic Beliefs",
92
+ "Islamic History"
93
+ ],
94
+
95
+ # Optional Subjects
96
+ "Computer Science": [
97
+ "Fundamentals of IT",
98
+ "Hardware & Software",
99
+ "Number Systems",
100
+ "Operating Systems",
101
+ "Introduction to Programming"
102
+ ],
103
+ "General Science": [
104
+ "Our Universe",
105
+ "Matter & Energy",
106
+ "Life & Living Organisms",
107
+ "Environment",
108
+ "Technology in Everyday Life"
109
+ ],
110
+ "Education": [
111
+ "Aims of Education",
112
+ "Curriculum",
113
+ "Teaching Methods",
114
+ "Assessment"
115
+ ],
116
  },
117
+
118
  "Grade 10": {
 
 
 
119
  "Biology": [
120
+ "Gaseous Exchange",
121
+ "Homeostasis",
122
+ "Coordination & Control",
123
+ "Support & Movement",
124
+ "Reproduction",
125
+ "Inheritance",
126
+ "Man & His Environment",
127
+ "Biotechnology",
128
+ "Pharmacology"
129
  ],
130
  "Chemistry": [
131
+ "Chemical Equilibrium",
132
+ "Acids, Bases & Salts",
133
+ "Organic Chemistry",
134
+ "Hydrocarbons",
135
+ "Biochemistry",
136
+ "Environmental Chemistry"
 
 
137
  ],
138
  "Physics": [
139
+ "Simple Harmonic Motion",
140
+ "Wave Motion",
141
+ "Sound",
142
+ "Geometrical Optics",
143
+ "Electrostatics",
144
+ "Current Electricity",
145
+ "Electromagnetism",
146
+ "Information & Communication Technology"
 
147
  ],
 
148
  "Pakistan Studies": [
149
+ "Pakistan's Geography",
150
+ "Culture & Society",
151
+ "Civic Responsibilities",
152
+ "Government System",
153
+ "Economic Planning"
154
+ ],
155
+
156
+ # NEW COMPULSORY SUBJECTS
157
+
158
+ "Mathematics": [
159
+ "Quadratic Equations",
160
+ "Theory of Quadratic Equations",
161
+ "Rational Expressions",
162
+ "Variation",
163
+ "Linear Graphs",
164
+ "Circles",
165
+ "Geometry",
166
+ "Statistics",
167
+ ],
168
+
169
+ "English": [
170
+ "Prose",
171
+ "Poetry",
172
+ "Drama",
173
+ "Essay Writing",
174
+ "Tenses",
175
+ "Direct & Indirect",
176
+ "Active & Passive Voice"
177
+ ],
178
+
179
+ "Urdu": [
180
+ "Ghazals",
181
+ "Nazms",
182
+ "Mazameen",
183
+ "Grammar",
184
+ "Muhavray",
185
+ "Takhlees"
186
+ ],
187
+
188
+ "Islamiyat": [
189
+ "Quranic Teachings",
190
+ "Ahadith",
191
+ "Islamic System",
192
+ "Fiqh",
193
+ "Islamic History"
194
+ ],
195
+
196
+ "Computer Science": [
197
+ "Programming Basics",
198
+ "Database Concepts",
199
+ "Computer Networks",
200
+ "Internet & Web",
201
+ "Algorithms"
202
+ ],
203
+
204
+ "General Science": [
205
+ "Atoms & Molecules",
206
+ "Human Body Systems",
207
+ "Electricity",
208
+ "Magnetism",
209
+ "Environment"
210
+ ],
211
+
212
+ "Education": [
213
+ "Educational Psychology",
214
+ "Learning Theories",
215
+ "Curriculum Development",
216
+ "Assessment in Education"
217
+ ],
218
  }
219
  }
220
 
221
+ # ---------------------------------------------------------
222
+ # MCQ GENERATION FUNCTION
223
+ # ---------------------------------------------------------
224
 
225
+ def generate_mcqs(grade, subject, chapter, difficulty, mcq_count):
226
+ mcqs = []
227
+ for i in range(mcq_count):
228
+ mcq = f"{i+1}. ({difficulty}) MCQ from {chapter}, {subject}, {grade}"
229
+ options = ["A) Option 1", "B) Option 2", "C) Option 3", "D) Option 4"]
230
+ correct = random.choice(["A", "B", "C", "D"])
231
+ mcqs.append((mcq, options, correct))
232
 
233
+ # Format output
234
+ output = ""
235
+ for q, opts, _ in mcqs:
236
+ output += q + "\n"
237
+ for opt in opts:
238
+ output += opt + "\n"
239
+ output += "\n"
240
+
241
+ # Answer Key
242
+ output += "\nAnswer Key:\n"
243
+ for idx, (_, _, correct) in enumerate(mcqs, 1):
244
+ output += f"{idx}. {correct}\n"
245
+
246
+ return output
247
+
248
+ # ---------------------------------------------------------
249
+ # UPDATE CHAPTER LIST WHEN SUBJECT CHANGES
250
+ # ---------------------------------------------------------
251
+
252
+ def update_subjects(grade):
253
+ subjects = list(curriculum[grade].keys())
254
+ return gr.update(choices=subjects, value=subjects[0])
255
+
256
+ def update_chapters(grade, subject):
257
+ chapters = curriculum[grade][subject]
258
+ return gr.update(choices=chapters, value=chapters[0])
259
+
260
+ # ---------------------------------------------------------
261
+ # UI
262
+ # ---------------------------------------------------------
263
+
264
+ with gr.Blocks(title="Pakistan Curriculum MCQ Generator") as demo:
265
+
266
+ gr.Markdown("## **MCQs Generator (Grades 9 & 10 — National Curriculum 2006)**")
267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
  with gr.Row():
269
+ grade = gr.Dropdown(choices=list(curriculum.keys()), label="Select Grade", value="Grade 9")
270
+ subject = gr.Dropdown(choices=list(curriculum["Grade 9"].keys()), label="Select Subject")
271
+ chapter = gr.Dropdown(choices=list(curriculum["Grade 9"]["Biology"]), label="Select Chapter")
272
+
273
+ difficulty = gr.Dropdown(
274
+ ["Beginner (Pre-test)", "Intermediate", "Advanced"],
275
+ label="Difficulty Level",
276
+ value="Beginner (Pre-test)"
277
+ )
278
+
279
+ mcq_count = gr.Slider(
280
+ minimum=5,
281
+ maximum=50,
282
+ value=20,
283
+ step=1,
284
+ label="Number of MCQs"
285
+ )
286
+
287
+ generate_button = gr.Button("Generate MCQs")
288
+
289
+ output = gr.Textbox(label="Generated Test", lines=30)
290
+
291
+ # Events
292
+ grade.change(update_subjects, inputs=grade, outputs=subject)
293
+ subject.change(update_chapters, inputs=[grade, subject], outputs=chapter)
294
+
295
+ generate_button.click(
296
+ fn=generate_mcqs,
297
+ inputs=[grade, subject, chapter, difficulty, mcq_count],
298
+ outputs=output
299
  )
300
 
301
+ demo.launch()