Turbiling commited on
Commit
85c48b4
·
verified ·
1 Parent(s): 580be88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -75
app.py CHANGED
@@ -12,65 +12,98 @@ import random
12
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
13
  client = Groq(api_key=GROQ_API_KEY)
14
 
15
- # Grades
16
  grades = [f"Grade {i}" for i in range(1, 13)]
17
-
18
- # Difficulty levels
19
  difficulty_levels = ["Pretest (Beginner)", "Intermediate", "Advanced"]
20
-
21
- # MCQ count range
22
  min_mcq, max_mcq = 5, 50
23
 
24
- # Example chapters database (fill official chapters later)
25
- chapters_by_grade_subject = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  "Grade 9": {
27
- "Biology": [
28
- "INTRODUCTION TO BIOLOGY","SOLVING A BIOLOGICAL PROBLEM","BIODIVERSITY",
29
- "CELLS AND TISSUES","CELL CYCLE","ENZYMES","BIOENERGETICS",
30
- "NUTRITION","TRANSPORT"
31
- ],
32
- "Chemistry": [
33
- "FUNDAMENTALS OF CHEMISTRY","STRUCTURE OF ATOMS","PERIODIC TABLE AND PERIODICITY OF PROPERTIES",
34
- "STRUCTURE OF MOLECULES","PHYSICAL STATES OF MATTER","SOLUTIONS",
35
- "ELECTROCHEMISTRY","CHEMICAL REACTIVITY"
36
- ],
37
- "Physics": [
38
- "PHYSICAL QUANTITIES AND MEASUREMENT","KINEMATICS","DYNAMICS",
39
- "Turning Effect of Forces","GRAVITATION","WORK AND ENERGY",
40
- "PROPERTIES OF MATTER","THERMAL PROPERTIES OF MATTER","TRANSFER OF HEAT"
41
- ],
42
- "Pakistan Studies": [
43
- "IDEOLOGICAL BASIS OF PAKISTAN","MAKING OF PAKISTAN",
44
- "LAND AND ENVIRONMENT","HISTORY OF PAKISTAN"
45
- ],
46
- "Mathematics": ["Chapter 1", "Chapter 2"], # Placeholder
47
- "English": ["Chapter 1", "Chapter 2"],
48
- "Urdu": ["Chapter 1", "Chapter 2"]
49
  },
50
  "Grade 10": {
51
- "Biology": [
52
- "GASEOUS EXCHANGE","HOMEOSTASIS","COORDINATION","SUPPORT AND MOVEMENT",
53
- "REPRODUCTION","INHERITANCE","MAN AND HIS ENVIRONMENT","BIOTECHNOLOGY","PHARMA"
54
- ],
55
- "Chemistry": [
56
- "CHEMICAL EQUILIBRIUM","ACIDS, BASES AND SALTS","ORGANIC CHEMISTRY",
57
- "HYDROCARBONS","BIOCHEMISTRY","ENVIRONMENTAL CHEMISTRY I: THE ATMOSPHERE",
58
- "ENVIRONMENTAL CHEMISTRY II: WATER","CHEMICAL INDUSTRIES"
59
- ],
60
- "Physics": [
61
- "SIMPLE HARMONIC MOTION AND WAVES","SOUND","GEOMETRICAL OPTICS","ELECTROSTATICS",
62
- "CURRENT ELECTRICITY","ELECTROMAGNETISM","INTRODUCTORY ELECTRONICS",
63
- "INFORMATION AND COMMUNICATION TECHNOLOGY","RADIOACTIVITY"
64
- ],
65
- "Pakistan Studies": [
66
- "HISTORY OF PAKISTAN-II","PAKISTAN IN WORLD AFFAIRS","ECONOMIC DEVELOPMENTS",
67
- "POPULATION, SOCIETY AND CULTURE OF PAKISTAN"
68
- ],
69
- "Mathematics": ["Chapter 1", "Chapter 2"], # Placeholder
70
- "English": ["Chapter 1", "Chapter 2"],
71
- "Urdu": ["Chapter 1", "Chapter 2"]
72
- },
73
- # Placeholder for other grades (1–8, 11–12)
74
  }
75
 
76
  def generate_mcqs(grade, subject, chapter, difficulty, num_questions):
@@ -88,21 +121,13 @@ def generate_mcqs(grade, subject, chapter, difficulty, num_questions):
88
  text = response.choices[0].message.content
89
  mcqs = re.split(r'\n?\d+\.', text)
90
  mcqs = [q.strip() for q in mcqs if q.strip()]
91
- # Random answers as placeholder
92
  answer_key = [random.choice(["A","B","C","D"]) for _ in mcqs]
93
 
94
  return mcqs[:num_questions], answer_key[:num_questions]
95
 
96
  def export_to_word(mcqs, answers, grade, subject, chapter, difficulty, num_questions):
97
  doc = Document()
98
- heading = doc.add_heading("WEEKLY/MONTHLY TEST", level=1)
99
- heading.alignment = WD_ALIGN_PARAGRAPH.CENTER
100
- run = heading.runs[0]
101
- run.bold = True
102
- run.font.color.rgb = RGBColor(0,128,0)
103
- run.font.size = Pt(18)
104
-
105
- # Info table
106
  table = doc.add_table(rows=6, cols=2)
107
  table.style = 'Table Grid'
108
  info = [
@@ -120,8 +145,7 @@ def export_to_word(mcqs, answers, grade, subject, chapter, difficulty, num_quest
120
 
121
  # MCQs
122
  for i, q in enumerate(mcqs, start=1):
123
- p = doc.add_paragraph(f"{i}. {q}")
124
- p.alignment = WD_ALIGN_PARAGRAPH.LEFT
125
 
126
  # Answer Key
127
  doc.add_paragraph("\nAnswer Key:")
@@ -160,18 +184,9 @@ with gr.Blocks() as demo:
160
  output_text = gr.Textbox(label="Status")
161
  output_file = gr.File(label="Download Test")
162
 
163
- def update_subjects(selected_grade):
164
- return gr.update(choices=list(chapters_by_grade_subject.get(selected_grade, {}).keys()), value=None)
165
- grade_dropdown.change(update_subjects, inputs=grade_dropdown, outputs=subject_dropdown)
166
 
167
- def update_chapters(selected_subject, selected_grade):
168
- return gr.update(choices=chapters_by_grade_subject.get(selected_grade, {}).get(selected_subject, []), value=None)
169
- subject_dropdown.change(update_chapters, inputs=[subject_dropdown, grade_dropdown], outputs=chapter_dropdown)
170
-
171
- generate_btn.click(
172
- on_generate,
173
- inputs=[grade_dropdown, subject_dropdown, chapter_dropdown, difficulty_dropdown, num_mcq_slider],
174
- outputs=[output_text, output_file]
175
- )
176
 
177
  demo.launch()
 
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
+ # Subjects and chapters by grade
20
+ chapters_by_subject_and_grade = {
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
+ "Grade 3": {
36
+ "Mathematics": ["Multiplication & Division", "Fractions (intro)", "Geometry Basics", "Measurement", "Patterns"],
37
+ "English": ["Tenses (simple)", "Comprehension Passages", "Vocabulary Development", "Sentence Writing"],
38
+ "Urdu": ["قواعد كا تعارف", "کہیں کہانیاں"],
39
+ "Science": ["Plants & Animals", "Human Body (basic)", "Materials & Uses"],
40
+ "Islamiyat": ["Seerat Stories & Morals", "Basic Akhlaq"]
41
+ },
42
+ "Grade 4": {
43
+ "Mathematics": ["Fractions & Decimals (intro)", "Geometry (angles, shapes)", "Data & Graphs", "Word Problems"],
44
+ "English": ["Grammar (parts of speech)", "Paragraph Writing", "Comprehension", "Listening & Speaking"],
45
+ "Urdu": ["نظم و نثر کا مطالعہ", "قواعد"],
46
+ "Science": ["Ecosystems", "Forces & Simple Machines", "Heat & Energy"],
47
+ "Islamiyat": ["Pillars of Islam", "Seerah Highlights"],
48
+ "Social Studies": ["Local Geography", "Community & Careers", "Citizenship"]
49
+ },
50
+ "Grade 5": {
51
+ "Mathematics": ["Number Operations", "Percentage & Ratio (intro)", "Geometry & Area", "Statistics (intro)"],
52
+ "English": ["Advanced Comprehension", "Essay & Letter Writing", "Grammar Consolidation"],
53
+ "Urdu": ["ادبی مطالعہ اور تحریر", "قواعد"],
54
+ "Science": ["Cells (intro)", "Matter & its Properties", "Energy & Sound"],
55
+ "Islamiyat": ["Quranic Stories & Morals"],
56
+ "Social Studies": ["History (local & national)", "Map Skills", "Basic Economics"]
57
+ },
58
+ "Grade 6": {
59
+ "Mathematics": ["Number System & Factors", "Algebraic Expressions (intro)", "Geometry Basics", "Data Handling"],
60
+ "English": ["Grammar & Usage", "Comprehension", "Writing Paragraphs", "Poetry & Prose"],
61
+ "Urdu": ["نثر و نظم", "قواعد ِاردو", "تحریر"],
62
+ "Science": ["Atoms & Molecules (intro)", "Human Body Systems", "Plants & Ecology"],
63
+ "Islamiyat": ["Seerah & Ahkam (intro)"],
64
+ "Social Studies": ["Physical Geography", "Early Civilizations", "Citizenship"]
65
+ },
66
+ "Grade 7": {
67
+ "Mathematics": ["Algebra (linear)", "Geometry (polygons, areas)", "Mensuration", "Graphs"],
68
+ "English": ["Advanced Grammar", "Comprehension Passages", "Composition"],
69
+ "Urdu": ["ادبی اور تنقیدی مطالعہ", "قواعد"],
70
+ "Science": ["Cell Biology", "Electricity & Magnetism (basic)", "Properties of Matter"],
71
+ "Islamiyat": ["Islamic History & Ethics"],
72
+ "Social Studies": ["Regional Geography", "Historical Events & Movements"],
73
+ "Computer": ["ICT Basics", "Introductory Coding Concepts"]
74
+ },
75
+ "Grade 8": {
76
+ "Mathematics": ["Linear Equations & Inequalities", "Coordinate Geometry", "Pythagoras & Trigonometry (intro)", "Probability"],
77
+ "English": ["Critical Reading", "Extended Writing", "Grammar Review"],
78
+ "Urdu": ["ادبی تنقید", "تحریر"],
79
+ "Science": ["Forces & Motion", "Heat & Temperature", "Waves & Optics (intro)"],
80
+ "Islamiyat": ["Fiqh Basics & Seerah"],
81
+ "Social Studies": ["Civics & Government", "Economic Concepts"],
82
+ "Computer": ["Algorithms & Flowcharts", "Application Software"]
83
+ },
84
  "Grade 9": {
85
+ "Mathematics": ["Number Systems & Algebra", "Coordinate Geometry", "Trigonometry", "Graphs"],
86
+ "English": ["Advanced Grammar", "Comprehension", "Writing Skills"],
87
+ "Urdu": ["ادب اور نثر", "نظم", "مضامین"],
88
+ "Biology": ["INTRODUCTION TO BIOLOGY","SOLVING A BIOLOGICAL PROBLEM","BIODIVERSITY","CELLS AND TISSUES","CELL CYCLE","ENZYMES","BIOENERGETICS","NUTRITION","TRANSPORT"],
89
+ "Chemistry": ["FUNDAMENTALS OF CHEMISTRY","STRUCTURE OF ATOMS","PERIODIC TABLE AND PERIODICITY OF PROPERTIES","STRUCTURE OF MOLECULES","PHYSICAL STATES OF MATTER","SOLUTIONS","ELECTROCHEMISTRY","CHEMICAL REACTIVITY"],
90
+ "Physics": ["PHYSICAL QUANTITIES AND MEASUREMENT","KINEMATICS","DYNAMICS","TURNING EFFECT OF FORCES","GRAVITATION","WORK AND ENERGY","PROPERTIES OF MATTER","THERMAL PROPERTIES OF MATTER","TRANSFER OF HEAT"],
91
+ "Islamiyat": ["Seerat of Prophet", "Faith & Beliefs", "Worship Practices"],
92
+ "Pakistan Studies": ["IDEOLOGICAL BASIS OF PAKISTAN","MAKING OF PAKISTAN","LAND AND ENVIRONMENT","HISTORY OF PAKISTAN"],
93
+ "Computer": ["Fundamentals of Computing", "ICT Applications"]
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  },
95
  "Grade 10": {
96
+ "Mathematics": ["Advanced Algebra", "Trigonometry", "Geometry", "Probability"],
97
+ "English": ["Literature", "Advanced Composition", "Comprehension"],
98
+ "Urdu": ["ادبِ جدید", "تحقیقی مضمون"],
99
+ "Biology": ["GASEOUS EXCHANGE","HOMEOSTASIS","COORDINATION","SUPPORT AND MOVEMENT","REPRODUCTION","INHERITANCE","MAN AND HIS ENVIRONMENT","BIOTECHNOLOGY","PHARMA"],
100
+ "Chemistry": ["CHEMICAL EQUILIBRIUM","ACIDS, BASES AND SALTS","ORGANIC CHEMISTRY","HYDROCARBONS","BIOCHEMISTRY","ENVIRONMENTAL CHEMISTRY I: THE ATMOSPHERE","ENVIRONMENTAL CHEMISTRY II: WATER","CHEMICAL INDUSTRIES"],
101
+ "Physics": ["SIMPLE HARMONIC MOTION AND WAVES","SOUND","GEOMETRICAL OPTICS","ELECTROSTATICS","CURRENT ELECTRICITY","ELECTROMAGNETISM","INTRODUCTORY ELECTRONICS","INFORMATION AND COMMUNICATION TECHNOLOGY","RADIOACTIVITY"],
102
+ "Islamiyat": ["Islamic Studies II", "Ethics & Society", "Fiqh Basics"],
103
+ "Pakistan Studies": ["HISTORY OF PAKISTAN-II","PAKISTAN IN WORLD AFFAIRS","ECONOMIC DEVELOPMENTS","POPULATION, SOCIETY AND CULTURE OF PAKISTAN"],
104
+ "Computer": ["Programming Basics", "ICT in Society"]
105
+ }
106
+ # Higher Secondary (Grades 11-12) can be similarly added
 
 
 
 
 
 
 
 
 
 
 
 
107
  }
108
 
109
  def generate_mcqs(grade, subject, chapter, difficulty, num_questions):
 
121
  text = response.choices[0].message.content
122
  mcqs = re.split(r'\n?\d+\.', text)
123
  mcqs = [q.strip() for q in mcqs if q.strip()]
 
124
  answer_key = [random.choice(["A","B","C","D"]) for _ in mcqs]
125
 
126
  return mcqs[:num_questions], answer_key[:num_questions]
127
 
128
  def export_to_word(mcqs, answers, grade, subject, chapter, difficulty, num_questions):
129
  doc = Document()
130
+ # Table at top
 
 
 
 
 
 
 
131
  table = doc.add_table(rows=6, cols=2)
132
  table.style = 'Table Grid'
133
  info = [
 
145
 
146
  # MCQs
147
  for i, q in enumerate(mcqs, start=1):
148
+ doc.add_paragraph(f"{i}. {q}")
 
149
 
150
  # Answer Key
151
  doc.add_paragraph("\nAnswer Key:")
 
184
  output_text = gr.Textbox(label="Status")
185
  output_file = gr.File(label="Download Test")
186
 
187
+ grade_dropdown.change(lambda g: gr.update(choices=list(chapters_by_subject_and_grade.get(g, {}).keys()), value=None), grade_dropdown, subject_dropdown)
188
+ 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)
 
189
 
190
+ generate_btn.click(on_generate, inputs=[grade_dropdown, subject_dropdown, chapter_dropdown, difficulty_dropdown, num_mcq_slider], outputs=[output_text, output_file])
 
 
 
 
 
 
 
 
191
 
192
  demo.launch()