Turbiling commited on
Commit
0baf33f
Β·
verified Β·
1 Parent(s): 80a2554

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +276 -273
app.py CHANGED
@@ -1,301 +1,304 @@
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()
 
 
 
 
1
  import gradio as gr
2
  import random
3
 
4
+ # -----------------------------------------------------------------------------------
5
+ # FIXED BOOKS & CHAPTERS for Grade 9 & 10 (given earlier, unchanged)
6
+ # -----------------------------------------------------------------------------------
7
+
8
+ subjects_chapters = {
9
+ "Biology 9": [
10
+ "Introduction to Biology",
11
+ "Solving a Biological Problem",
12
+ "Biodiversity",
13
+ "Cells and Tissues",
14
+ "Cell Cycle",
15
+ "Enzymes",
16
+ "Bioenergetics",
17
+ "Nutrition",
18
+ "Transport",
19
+ "Gaseous Exchange"
20
+ ],
21
+ "Biology 10": [
22
+ "Homeostasis",
23
+ "Coordination and Control",
24
+ "Support and Movement",
25
+ "Reproduction",
26
+ "Inheritance",
27
+ "Man and His Environment",
28
+ "Biotechnology",
29
+ "Pharmacology"
30
+ ],
31
+
32
+ "Chemistry 9": [
33
+ "Fundamentals of Chemistry",
34
+ "Structure of Atoms",
35
+ "Periodic Table",
36
+ "Structure of Molecules",
37
+ "Physical States of Matter",
38
+ "Solutions",
39
+ "Electrochemistry",
40
+ "Chemical Reactivity"
41
+ ],
42
+ "Chemistry 10": [
43
+ "Chemical Equilibrium",
44
+ "Acids, Bases and Salts",
45
+ "Organic Chemistry",
46
+ "Hydrocarbons",
47
+ "Biochemistry",
48
+ "Environmental Chemistry",
49
+ "Chemical Industries"
50
+ ],
51
+
52
+ "Physics 9": [
53
+ "Physical Quantities & Measurements",
54
+ "Kinematics",
55
+ "Dynamics",
56
+ "Turning Effect of Forces",
57
+ "Gravitation",
58
+ "Work & Energy",
59
+ "Properties of Matter",
60
+ "Thermal Properties of Matter",
61
+ "Transfer of Heat"
62
+ ],
63
+ "Physics 10": [
64
+ "Sound",
65
+ "Electrostatics",
66
+ "Current Electricity",
67
+ "Electromagnetism",
68
+ "Basic Electronics",
69
+ "Information & Communication Technology",
70
+ "Atomic & Nuclear Physics"
71
+ ],
72
+
73
+ "Pakistan Studies 9": [
74
+ "Ideological Basis of Pakistan",
75
+ "Pakistan Movement",
76
+ "Land & Environment",
77
+ "History of Pakistan (1947–1971)"
78
+ ],
79
+ "Pakistan Studies 10": [
80
+ "Culture of Pakistan",
81
+ "Economy of Pakistan",
82
+ "Government of Pakistan",
83
+ "Human Rights in Pakistan"
84
+ ],
85
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
+ # -----------------------------------------------------------------------------------
88
+ # NEWLY ADDED SECONDARY + HIGHER SECONDARY SUBJECTS (2006 Curriculum)
89
+ # -----------------------------------------------------------------------------------
90
+
91
+ subjects_chapters.update({
92
+ # -----------------------------------------
93
+ # English Compulsory (9–10)
94
+ # -----------------------------------------
95
+ "English 9–10": [
96
+ "Reading Comprehension",
97
+ "Writing Skills",
98
+ "Grammar & Vocabulary",
99
+ "Formal & Informal Letters",
100
+ "Dialogue Writing",
101
+ "Story Writing",
102
+ "Essay Writing",
103
+ "Translation Skills"
104
+ ],
105
+
106
+ # -----------------------------------------
107
+ # Urdu Compulsory (9–10)
108
+ # -----------------------------------------
109
+ "Urdu 9–10": [
110
+ "Ghazaal",
111
+ "Nazam",
112
+ "Novel / Afsana",
113
+ "Drama",
114
+ "Grammar",
115
+ "Khulasa Nigar",
116
+ "Mazmoon Nigar",
117
+ "Takhleeqi Nigarish"
118
+ ],
119
+
120
+ # -----------------------------------------
121
+ # Islamiyat Compulsory (9–10)
122
+ # -----------------------------------------
123
+ "Islamiyat 9–10": [
124
+ "Quranic Verses",
125
+ "Ahadith",
126
+ "Seerat-un-Nabi",
127
+ "Islamic Beliefs",
128
+ "Ibadat",
129
+ "Islamic Moral Values",
130
+ "Islamic Social System"
131
+ ],
132
+
133
+ # -----------------------------------------
134
+ # Mathematics Science Group (9–10)
135
+ # -----------------------------------------
136
+ "Mathematics 9–10": [
137
+ "Algebraic Expressions",
138
+ "Factorization",
139
+ "Quadratic Equations",
140
+ "Matrices & Determinants",
141
+ "Geometry",
142
+ "Trigonometry",
143
+ "Statistics"
144
+ ],
145
+
146
+ # -----------------------------------------
147
+ # Computer Science (9–10)
148
+ # -----------------------------------------
149
+ "Computer Science 9–10": [
150
+ "Introduction to Computers",
151
+ "Hardware Components",
152
+ "Software & Operating Systems",
153
+ "Basics of Programming",
154
+ "MS Office Applications",
155
+ "Computer Networks",
156
+ "Internet & IT Ethics"
157
+ ],
158
+
159
+ # -----------------------------------------------------------------------------------
160
+ # HIGHER SECONDARY (11–12)
161
+ # -----------------------------------------------------------------------------------
162
+
163
+ "Biology 11–12": [
164
+ "Cell Structure & Function",
165
+ "Biological Molecules",
166
+ "Plant Physiology",
167
+ "Animal Physiology",
168
+ "Reproduction",
169
+ "Genetics",
170
+ "Evolution",
171
+ "Biotechnology"
172
+ ],
173
+ "Chemistry 11–12": [
174
+ "Stoichiometry",
175
+ "Thermochemistry",
176
+ "Chemical Kinetics",
177
+ "Electrochemistry",
178
+ "s & p Block Elements",
179
+ "Transition Elements",
180
+ "Alcohols & Phenols",
181
+ "Carboxylic Acids"
182
+ ],
183
+ "Physics 11–12": [
184
+ "Vectors & Motion",
185
+ "Circular Motion",
186
+ "Fluid Dynamics",
187
+ "Oscillations & Waves",
188
+ "Electromagnetism",
189
+ "Optics",
190
+ "Modern Physics",
191
+ "Nuclear Physics"
192
+ ],
193
+ "Pakistan Studies 11–12": [
194
+ "Historical Perspectives",
195
+ "Constitutional Development",
196
+ "Foreign Policy of Pakistan",
197
+ "Economy & Resources"
198
+ ],
199
+ "Mathematics 11–12": [
200
+ "Functions & Limits",
201
+ "Differentiation",
202
+ "Integration",
203
+ "Linear Algebra",
204
+ "Trigonometric Identities",
205
+ "Conic Sections",
206
+ "Vectors"
207
+ ],
208
+ "English 11–12": [
209
+ "Reading & Comprehension",
210
+ "Essay Writing",
211
+ "Grammar",
212
+ "Poetry",
213
+ "Short Stories",
214
+ "Drama"
215
+ ],
216
+ })
217
+
218
+ # -----------------------------------------------------------------------------------
219
+ # MCQ GENERATOR FUNCTION
220
+ # -----------------------------------------------------------------------------------
221
+
222
+ def generate_mcqs(subject, chapter, difficulty, mcq_count):
223
+ if not subject or not chapter:
224
+ return "Please select subject and chapter."
225
+
226
+ difficulty_text = {
227
+ "Pretest (Absolute Beginner)": "Very easy MCQs based strictly on recall-level knowledge.",
228
+ "Intermediate": "Moderate difficulty MCQs based on understanding and conceptual clarity.",
229
+ "Advanced": "Challenging MCQs requiring analysis and higher-order thinking."
230
  }
 
231
 
232
+ prompt_style = difficulty_text[difficulty]
 
 
233
 
 
234
  mcqs = []
235
+ for i in range(1, mcq_count + 1):
236
+ mcqs.append(
237
+ f"{i}. (Difficulty: {difficulty}) {prompt_style} β€” MCQ from '{chapter}'\n"
238
+ f"A. Option 1\nB. Option 2\nC. Option 3\nD. Option 4\n"
239
+ )
240
+
241
+ answer_key = "\n".join([f"{i}. {random.choice(['A','B','C','D'])}" for i in range(1, mcq_count + 1)])
242
+
243
+ test_output = "\n".join(mcqs)
244
+ final_output = (
245
+ "MCQ TEST\n\n"
246
+ + test_output
247
+ + "\n\nANSWER KEY\n"
248
+ + answer_key
249
+ )
 
 
 
 
 
 
 
 
 
250
 
251
+ return final_output
 
 
252
 
253
+ # -----------------------------------------------------------------------------------
254
+ # GRADIO UI
255
+ # -----------------------------------------------------------------------------------
256
 
257
+ def get_chapters(subject):
258
+ return gr.update(choices=subjects_chapters.get(subject, []), value=None)
 
259
 
260
+ with gr.Blocks(title="MCQ Test Generator for Grades 9–12") as demo:
261
 
262
+ gr.Markdown("## **MCQ Test Generator (Pakistan National Curriculum 2006)**")
263
 
264
  with gr.Row():
265
+ subject = gr.Dropdown(
266
+ label="Select Subject",
267
+ choices=list(subjects_chapters.keys())
268
+ )
269
+ chapter = gr.Dropdown(label="Select Chapter")
270
+
271
+ subject.change(fn=get_chapters, inputs=subject, outputs=chapter)
272
 
273
  difficulty = gr.Dropdown(
274
+ label="Select Test Difficulty Level",
275
+ choices=[
276
+ "Pretest (Absolute Beginner)",
277
+ "Intermediate",
278
+ "Advanced"
279
+ ],
280
+ value="Intermediate"
281
  )
282
 
283
+ mcq_slider = gr.Slider(
284
+ label="Select Number of MCQs",
285
  minimum=5,
286
+ maximum=100,
287
+ step=5,
288
+ value=20
 
289
  )
290
 
291
+ generate_btn = gr.Button("Generate MCQs")
292
 
293
+ output = gr.Textbox(label="Generated MCQ Test", lines=30)
294
 
295
+ generate_btn.click(
 
 
 
 
296
  fn=generate_mcqs,
297
+ inputs=[subject, chapter, difficulty, mcq_slider],
298
  outputs=output
299
  )
300
 
301
+ # -----------------------------------------------------------------------------------
302
+
303
+ if __name__ == "__main__":
304
+ demo.launch()