Turbiling commited on
Commit
5835efa
·
verified ·
1 Parent(s): 1ff7dbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -45
app.py CHANGED
@@ -1,35 +1,47 @@
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, Inches
7
  from docx.enum.text import WD_ALIGN_PARAGRAPH
8
 
9
  # -------------------------
10
- # Initialize GROQ client
11
  # -------------------------
12
- GROQ_API_KEY = os.getenv("GROQ_API_KEY")
13
- client = Groq(api_key=GROQ_API_KEY)
14
-
15
  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
  # -------------------------
20
- # Chapters by grade & subject (use your full dictionary here)
 
21
  # -------------------------
22
  chapters_by_subject_and_grade = {
23
- # Paste the full dictionary from your previous code (Grades 1-12 with all subjects & chapters)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  }
25
 
26
  # -------------------------
27
- # Generate MCQs (dummy placeholder or Groq call)
28
  # -------------------------
29
  def generate_mcqs(grade, subject, chapter, difficulty, num_questions):
30
  mcqs = [f"Sample question {i} for {subject} chapter {chapter}" for i in range(1, num_questions+1)]
31
- options_list = [["Option A", "Option B", "Option C", "Option D"] for _ in range(num_questions)]
32
- answers = ["A" for _ in range(num_questions)]
33
  return mcqs, options_list, answers
34
 
35
  # -------------------------
@@ -38,41 +50,42 @@ def generate_mcqs(grade, subject, chapter, difficulty, num_questions):
38
  def export_to_word(mcqs, options_list, answers, grade, subject, chapter, difficulty):
39
  doc = Document()
40
  style = doc.styles['Normal']
41
- font = style.font
42
- font.name = 'Times New Roman'
43
- font.size = Pt(12)
44
-
45
  heading = doc.add_paragraph("MONTHLY/WEEKLY TEST")
46
  heading.alignment = WD_ALIGN_PARAGRAPH.CENTER
47
  heading.runs[0].bold = True
48
  heading.runs[0].font.size = Pt(16)
49
-
50
- doc.add_paragraph()
51
-
 
52
  table = doc.add_table(rows=6, cols=2)
53
  table.style = 'Table Grid'
54
  info = [
55
- ("Grade", grade),
56
- ("Subject", subject),
57
- ("Chapter", chapter),
58
- ("Difficulty", difficulty),
59
- ("Number of Questions", str(len(mcqs))),
60
  ("Date", datetime.now().strftime("%d-%m-%Y"))
61
  ]
62
  for row, (label, value) in zip(table.rows, info):
63
  row.cells[0].text = label
64
  row.cells[1].text = value
65
  row.cells[0].paragraphs[0].runs[0].bold = True
66
-
67
- doc.add_paragraph()
68
-
 
69
  for i, (q, opts) in enumerate(zip(mcqs, options_list), start=1):
70
  doc.add_paragraph(f"{i}. {q}")
71
  for letter, text in zip(["A","B","C","D"], opts):
72
  p = doc.add_paragraph(f"{letter}. {text}")
73
  p.paragraph_format.left_indent = Inches(0.25)
74
-
75
- doc.add_paragraph()
 
 
76
  doc.add_paragraph("ANSWER KEY")
77
  ans_table = doc.add_table(rows=len(answers)+1, cols=5)
78
  ans_table.style = 'Table Grid'
@@ -84,7 +97,7 @@ def export_to_word(mcqs, options_list, answers, grade, subject, chapter, difficu
84
  ans_table.cell(r, 0).text = str(r)
85
  for c, option in enumerate(["A","B","C","D"], start=1):
86
  ans_table.cell(r, c).text = "✔" if ans==option else ""
87
-
88
  file_path = f"{grade}_{subject}_{chapter}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.docx"
89
  doc.save(file_path)
90
  return file_path
@@ -102,35 +115,33 @@ def on_generate(grade, subject, chapter, difficulty, num_questions):
102
  # -------------------------
103
  with gr.Blocks() as demo:
104
  gr.Markdown("### **MCQ Test Generator - Developer: Najaf Ali Sharqi**")
105
-
106
  with gr.Row():
107
  grade_dropdown = gr.Dropdown(choices=grades, label="Select Grade")
108
  subject_dropdown = gr.Dropdown(choices=[], label="Select Subject")
109
  chapter_dropdown = gr.Dropdown(choices=[], label="Select Chapter")
110
-
111
  with gr.Row():
112
  difficulty_dropdown = gr.Dropdown(choices=difficulty_levels, label="Select Difficulty")
113
  num_mcq_slider = gr.Slider(minimum=min_mcq, maximum=max_mcq, value=20, step=1, label="Number of MCQs")
114
-
115
  generate_btn = gr.Button("Generate Test")
116
  output_text = gr.Textbox(label="Status")
117
  output_file = gr.File(label="Download Test")
118
-
119
- # -------------------------
120
- # Dynamic dropdown updates
121
- # -------------------------
122
- def update_subjects(selected_grade):
123
- subjects = list(chapters_by_subject_and_grade.get(selected_grade, {}).keys())
124
  return gr.update(choices=subjects, value=subjects[0] if subjects else None)
125
-
126
- def update_chapters(selected_subject, selected_grade):
127
- chapters = chapters_by_subject_and_grade.get(selected_grade, {}).get(selected_subject, [])
128
  return gr.update(choices=chapters, value=chapters[0] if chapters else None)
129
-
130
- grade_dropdown.change(update_subjects, inputs=[grade_dropdown], outputs=[subject_dropdown])
131
- subject_dropdown.change(update_chapters, inputs=[subject_dropdown, grade_dropdown], outputs=[chapter_dropdown])
132
-
133
- generate_btn.click(on_generate,
134
  inputs=[grade_dropdown, subject_dropdown, chapter_dropdown, difficulty_dropdown, num_mcq_slider],
135
  outputs=[output_text, output_file])
136
 
 
1
  import os
2
  import gradio as gr
 
3
  from datetime import datetime
4
  from docx import Document
5
  from docx.shared import Pt, Inches
6
  from docx.enum.text import WD_ALIGN_PARAGRAPH
7
 
8
  # -------------------------
9
+ # Grades and Difficulty
10
  # -------------------------
 
 
 
11
  grades = [f"Grade {i}" for i in range(1, 13)]
12
  difficulty_levels = ["Pretest (Beginner)", "Intermediate", "Advanced"]
13
  min_mcq, max_mcq = 5, 50
14
 
15
  # -------------------------
16
+ # Subjects and Chapters
17
+ # Use full dictionary for grades 1-12
18
  # -------------------------
19
  chapters_by_subject_and_grade = {
20
+ # ----------------- Grades 1-8 (example) -----------------
21
+ "Grade 1": {
22
+ "Mathematics": ["Counting & Number Recognition", "Basic Addition", "Basic Subtraction", "Shapes & Space", "Measurement (Length, Weight)"],
23
+ "English": ["Alphabet & Phonics", "Listening & Speaking", "Basic Vocabulary", "Simple Sentences", "Early Reading"],
24
+ "Urdu": ["حروف اور آوازیں", "الفاظ", "سادہ جملے", "پڑھائی کا تعارف", "چھوٹی کہانیاں"],
25
+ "Islamiyat": ["Iman Basics", "Prophets' Stories (selected)", "Good Manners", "Simple Prayers & Worship"],
26
+ "General Knowledge": ["My Home & Family", "Plants & Animals", "Seasons & Weather", "My School"]
27
+ },
28
+ "Grade 2": {
29
+ "Mathematics": ["Place Value", "Addition & Subtraction Facts", "Money", "Time (clock)", "Basic Shapes"],
30
+ "English": ["Reading Short Texts", "Simple Grammar (nouns, verbs)", "Vocabulary Building", "Speaking Activities"],
31
+ "Urdu": ["روزمرہ الفاظ", "مختصر کہانیاں", "قرأتِ آسان"],
32
+ "Islamiyat": ["Stories of Prophets", "Good Deeds", "Worship Basics"],
33
+ "General Knowledge": ["Local Community", "Transportation & Safety", "Plants & Animals"]
34
+ },
35
+ # ... add all remaining grades 3-12 using your previous dictionary
36
  }
37
 
38
  # -------------------------
39
+ # Dummy MCQ generator (replace with actual AI API)
40
  # -------------------------
41
  def generate_mcqs(grade, subject, chapter, difficulty, num_questions):
42
  mcqs = [f"Sample question {i} for {subject} chapter {chapter}" for i in range(1, num_questions+1)]
43
+ options_list = [["Option A", "Option B", "Option C", "Option D"] for _ in mcqs]
44
+ answers = ["A"] * num_questions
45
  return mcqs, options_list, answers
46
 
47
  # -------------------------
 
50
  def export_to_word(mcqs, options_list, answers, grade, subject, chapter, difficulty):
51
  doc = Document()
52
  style = doc.styles['Normal']
53
+ style.font.name = 'Times New Roman'
54
+ style.font.size = Pt(12)
55
+
56
+ # Heading
57
  heading = doc.add_paragraph("MONTHLY/WEEKLY TEST")
58
  heading.alignment = WD_ALIGN_PARAGRAPH.CENTER
59
  heading.runs[0].bold = True
60
  heading.runs[0].font.size = Pt(16)
61
+
62
+ doc.add_paragraph() # empty line
63
+
64
+ # Info Table
65
  table = doc.add_table(rows=6, cols=2)
66
  table.style = 'Table Grid'
67
  info = [
68
+ ("Grade", grade), ("Subject", subject), ("Chapter", chapter),
69
+ ("Difficulty", difficulty), ("Number of Questions", str(len(mcqs))),
 
 
 
70
  ("Date", datetime.now().strftime("%d-%m-%Y"))
71
  ]
72
  for row, (label, value) in zip(table.rows, info):
73
  row.cells[0].text = label
74
  row.cells[1].text = value
75
  row.cells[0].paragraphs[0].runs[0].bold = True
76
+
77
+ doc.add_paragraph() # empty line
78
+
79
+ # MCQs
80
  for i, (q, opts) in enumerate(zip(mcqs, options_list), start=1):
81
  doc.add_paragraph(f"{i}. {q}")
82
  for letter, text in zip(["A","B","C","D"], opts):
83
  p = doc.add_paragraph(f"{letter}. {text}")
84
  p.paragraph_format.left_indent = Inches(0.25)
85
+
86
+ doc.add_paragraph() # empty line
87
+
88
+ # Answer Key
89
  doc.add_paragraph("ANSWER KEY")
90
  ans_table = doc.add_table(rows=len(answers)+1, cols=5)
91
  ans_table.style = 'Table Grid'
 
97
  ans_table.cell(r, 0).text = str(r)
98
  for c, option in enumerate(["A","B","C","D"], start=1):
99
  ans_table.cell(r, c).text = "✔" if ans==option else ""
100
+
101
  file_path = f"{grade}_{subject}_{chapter}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.docx"
102
  doc.save(file_path)
103
  return file_path
 
115
  # -------------------------
116
  with gr.Blocks() as demo:
117
  gr.Markdown("### **MCQ Test Generator - Developer: Najaf Ali Sharqi**")
118
+
119
  with gr.Row():
120
  grade_dropdown = gr.Dropdown(choices=grades, label="Select Grade")
121
  subject_dropdown = gr.Dropdown(choices=[], label="Select Subject")
122
  chapter_dropdown = gr.Dropdown(choices=[], label="Select Chapter")
123
+
124
  with gr.Row():
125
  difficulty_dropdown = gr.Dropdown(choices=difficulty_levels, label="Select Difficulty")
126
  num_mcq_slider = gr.Slider(minimum=min_mcq, maximum=max_mcq, value=20, step=1, label="Number of MCQs")
127
+
128
  generate_btn = gr.Button("Generate Test")
129
  output_text = gr.Textbox(label="Status")
130
  output_file = gr.File(label="Download Test")
131
+
132
+ # Dropdown updates
133
+ def update_subjects(grade):
134
+ subjects = list(chapters_by_subject_and_grade.get(grade, {}).keys())
 
 
135
  return gr.update(choices=subjects, value=subjects[0] if subjects else None)
136
+
137
+ def update_chapters(subject, grade):
138
+ chapters = chapters_by_subject_and_grade.get(grade, {}).get(subject, [])
139
  return gr.update(choices=chapters, value=chapters[0] if chapters else None)
140
+
141
+ grade_dropdown.change(update_subjects, grade_dropdown, subject_dropdown)
142
+ subject_dropdown.change(update_chapters, [subject_dropdown, grade_dropdown], chapter_dropdown)
143
+
144
+ generate_btn.click(on_generate,
145
  inputs=[grade_dropdown, subject_dropdown, chapter_dropdown, difficulty_dropdown, num_mcq_slider],
146
  outputs=[output_text, output_file])
147