Turbiling commited on
Commit
703bfac
·
verified ·
1 Parent(s): 05ae123

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -13
app.py CHANGED
@@ -16,11 +16,19 @@ grades = [f"Grade {i}" for i in range(1, 13)]
16
  difficulty_levels = ["Pretest (Beginner)", "Intermediate", "Advanced"]
17
  min_mcq, max_mcq = 5, 50
18
 
19
- # Subjects and chapters (Grades 1–12)
 
 
20
  chapters_by_subject_and_grade = {
21
- # Grades 1–10 already included (omitted here for brevity; same as previous code)
22
- # ----------
23
- # Grades 11
 
 
 
 
 
 
24
  "Grade 11": {
25
  "Mathematics": ["Advanced Algebra & Calculus Intro", "Coordinate Geometry", "Trigonometry", "Vectors"],
26
  "English": ["Academic Writing", "Literature Studies", "Critical Reading"],
@@ -37,7 +45,6 @@ chapters_by_subject_and_grade = {
37
  "Islamiyat": ["Advanced Seerah","Islamic Ethics"],
38
  "Pakistan Studies": ["Pakistan in Geographical Perspective","Resources & Economy","Historical Overview","Political & Constitutional Development","Human Rights & Citizenship"]
39
  },
40
- # Grades 12
41
  "Grade 12": {
42
  "Mathematics": ["Calculus","Advanced Algebra","Statistics","Analytical Geometry"],
43
  "English": ["Research Writing","Advanced Literature","Critical Analysis"],
@@ -56,6 +63,9 @@ chapters_by_subject_and_grade = {
56
  }
57
  }
58
 
 
 
 
59
  def generate_mcqs(grade, subject, chapter, difficulty, num_questions):
60
  system_msg = "You are an educational expert. Generate MCQs strictly aligned with the Pakistani National Curriculum."
61
  user_msg = f"Create {num_questions} MCQs for {grade} {subject} on the chapter '{chapter}' at '{difficulty}' level. Each question must have 4 options. Provide only questions and options. Do not add any introductory text."
@@ -69,17 +79,19 @@ def generate_mcqs(grade, subject, chapter, difficulty, num_questions):
69
  )
70
 
71
  text = response.choices[0].message.content
72
- # Extract questions and options only, removing any prefatory text
73
  mcqs = re.findall(r'\d+\.\s.*?(?=\n\d+\.|\Z)', text, re.DOTALL)
74
  mcqs = [q.strip() for q in mcqs if q.strip()]
75
  answer_key = [random.choice(["A","B","C","D"]) for _ in mcqs]
76
 
77
  return mcqs[:num_questions], answer_key[:num_questions]
78
 
 
 
 
79
  def export_to_word(mcqs, answers, grade, subject, chapter, difficulty, num_questions):
80
  doc = Document()
81
 
82
- # Main heading
83
  heading = doc.add_paragraph("MONTHLY/WEEKLY TEST")
84
  heading.alignment = WD_ALIGN_PARAGRAPH.CENTER
85
  heading.runs[0].bold = True
@@ -128,25 +140,37 @@ def on_generate(grade, subject, chapter, difficulty, num_questions):
128
  file_path = export_to_word(mcqs, answers, grade, subject, chapter, difficulty, num_questions)
129
  return f"Test ready: {file_path}", file_path
130
 
 
131
  # Gradio UI
 
 
 
 
 
 
 
 
 
132
  with gr.Blocks() as demo:
133
  gr.Markdown("### **MCQ Test Generator - Developer: Najaf Ali Sharqi**")
 
134
  with gr.Row():
135
  grade_dropdown = gr.Dropdown(choices=grades, label="Select Grade")
136
  subject_dropdown = gr.Dropdown(choices=[], label="Select Subject")
137
  chapter_dropdown = gr.Dropdown(choices=[], label="Select Chapter")
 
138
  with gr.Row():
139
  difficulty_dropdown = gr.Dropdown(choices=difficulty_levels, label="Select Difficulty")
140
  num_mcq_slider = gr.Slider(minimum=min_mcq, maximum=max_mcq, value=20, step=1, label="Number of MCQs")
 
141
  generate_btn = gr.Button("Generate Test")
142
  output_text = gr.Textbox(label="Status")
143
  output_file = gr.File(label="Download Test")
144
-
145
- # Update subjects on grade change
146
- grade_dropdown.change(lambda g: gr.update(choices=list(chapters_by_subject_and_grade.get(g, {}).keys()), value=None), grade_dropdown, subject_dropdown)
147
- # Update chapters on subject change
148
- subject_dropdown.change(lambda s, g: gr.update(choices=chapters_by_subject_and_grade.get(g, {}).get(s, []), value=None), [subject_dropdown, grade_dropdown], chapter_dropdown)
149
-
150
  generate_btn.click(on_generate, inputs=[grade_dropdown, subject_dropdown, chapter_dropdown, difficulty_dropdown, num_mcq_slider], outputs=[output_text, output_file])
151
 
152
  demo.launch()
 
16
  difficulty_levels = ["Pretest (Beginner)", "Intermediate", "Advanced"]
17
  min_mcq, max_mcq = 5, 50
18
 
19
+ # -------------------------------
20
+ # Subjects and chapters by grade
21
+ # -------------------------------
22
  chapters_by_subject_and_grade = {
23
+ # Grades 1–12 (Grades 1–10 abbreviated here for brevity; 11-12 full as given)
24
+ "Grade 1": {
25
+ "Mathematics": ["Counting & Number Recognition", "Basic Addition", "Basic Subtraction", "Shapes & Space", "Measurement (Length, Weight)"],
26
+ "English": ["Alphabet & Phonics", "Listening & Speaking", "Basic Vocabulary", "Simple Sentences", "Early Reading"],
27
+ "Urdu": ["حروف اور آوازیں", "الفاظ", "سادہ جملے", "پڑھائی کا تعارف", "چھوٹی کہانیاں"],
28
+ "Islamiyat": ["Iman Basics", "Prophets' Stories (selected)", "Good Manners", "Simple Prayers & Worship"],
29
+ "General Knowledge": ["My Home & Family", "Plants & Animals", "Seasons & Weather", "My School"]
30
+ },
31
+ # ... (Grades 2–10 omitted for brevity, include all previous chapters as before)
32
  "Grade 11": {
33
  "Mathematics": ["Advanced Algebra & Calculus Intro", "Coordinate Geometry", "Trigonometry", "Vectors"],
34
  "English": ["Academic Writing", "Literature Studies", "Critical Reading"],
 
45
  "Islamiyat": ["Advanced Seerah","Islamic Ethics"],
46
  "Pakistan Studies": ["Pakistan in Geographical Perspective","Resources & Economy","Historical Overview","Political & Constitutional Development","Human Rights & Citizenship"]
47
  },
 
48
  "Grade 12": {
49
  "Mathematics": ["Calculus","Advanced Algebra","Statistics","Analytical Geometry"],
50
  "English": ["Research Writing","Advanced Literature","Critical Analysis"],
 
63
  }
64
  }
65
 
66
+ # -------------------------------
67
+ # Generate MCQs using GROQ
68
+ # -------------------------------
69
  def generate_mcqs(grade, subject, chapter, difficulty, num_questions):
70
  system_msg = "You are an educational expert. Generate MCQs strictly aligned with the Pakistani National Curriculum."
71
  user_msg = f"Create {num_questions} MCQs for {grade} {subject} on the chapter '{chapter}' at '{difficulty}' level. Each question must have 4 options. Provide only questions and options. Do not add any introductory text."
 
79
  )
80
 
81
  text = response.choices[0].message.content
 
82
  mcqs = re.findall(r'\d+\.\s.*?(?=\n\d+\.|\Z)', text, re.DOTALL)
83
  mcqs = [q.strip() for q in mcqs if q.strip()]
84
  answer_key = [random.choice(["A","B","C","D"]) for _ in mcqs]
85
 
86
  return mcqs[:num_questions], answer_key[:num_questions]
87
 
88
+ # -------------------------------
89
+ # Export to MS Word
90
+ # -------------------------------
91
  def export_to_word(mcqs, answers, grade, subject, chapter, difficulty, num_questions):
92
  doc = Document()
93
 
94
+ # Heading
95
  heading = doc.add_paragraph("MONTHLY/WEEKLY TEST")
96
  heading.alignment = WD_ALIGN_PARAGRAPH.CENTER
97
  heading.runs[0].bold = True
 
140
  file_path = export_to_word(mcqs, answers, grade, subject, chapter, difficulty, num_questions)
141
  return f"Test ready: {file_path}", file_path
142
 
143
+ # -------------------------------
144
  # Gradio UI
145
+ # -------------------------------
146
+ def update_subjects(grade):
147
+ subjects = list(chapters_by_subject_and_grade.get(grade, {}).keys())
148
+ return gr.Dropdown.update(choices=subjects, value=subjects[0] if subjects else None)
149
+
150
+ def update_chapters(subject, grade):
151
+ chapters = chapters_by_subject_and_grade.get(grade, {}).get(subject, [])
152
+ return gr.Dropdown.update(choices=chapters, value=chapters[0] if chapters else None)
153
+
154
  with gr.Blocks() as demo:
155
  gr.Markdown("### **MCQ Test Generator - Developer: Najaf Ali Sharqi**")
156
+
157
  with gr.Row():
158
  grade_dropdown = gr.Dropdown(choices=grades, label="Select Grade")
159
  subject_dropdown = gr.Dropdown(choices=[], label="Select Subject")
160
  chapter_dropdown = gr.Dropdown(choices=[], label="Select Chapter")
161
+
162
  with gr.Row():
163
  difficulty_dropdown = gr.Dropdown(choices=difficulty_levels, label="Select Difficulty")
164
  num_mcq_slider = gr.Slider(minimum=min_mcq, maximum=max_mcq, value=20, step=1, label="Number of MCQs")
165
+
166
  generate_btn = gr.Button("Generate Test")
167
  output_text = gr.Textbox(label="Status")
168
  output_file = gr.File(label="Download Test")
169
+
170
+ # Dropdown dependencies
171
+ grade_dropdown.change(update_subjects, inputs=grade_dropdown, outputs=subject_dropdown)
172
+ subject_dropdown.change(update_chapters, inputs=[subject_dropdown, grade_dropdown], outputs=chapter_dropdown)
173
+
 
174
  generate_btn.click(on_generate, inputs=[grade_dropdown, subject_dropdown, chapter_dropdown, difficulty_dropdown, num_mcq_slider], outputs=[output_text, output_file])
175
 
176
  demo.launch()