Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
|
@@ -8,130 +9,446 @@ 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 |
-
#
|
|
|
|
|
|
|
| 16 |
grades = [f"Grade {i}" for i in range(1, 13)]
|
| 17 |
-
|
| 18 |
-
# Subjects by grade
|
| 19 |
subjects_by_grade = {
|
| 20 |
"Grade 1": ["Mathematics", "English", "Urdu", "Islamiyat", "General Knowledge"],
|
| 21 |
"Grade 2": ["Mathematics", "English", "Urdu", "Islamiyat", "General Knowledge"],
|
| 22 |
"Grade 3": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "General Knowledge"],
|
| 23 |
-
"Grade 4": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "
|
| 24 |
"Grade 5": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "Social Studies"],
|
| 25 |
"Grade 6": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "Social Studies"],
|
| 26 |
"Grade 7": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "Social Studies", "Computer"],
|
| 27 |
"Grade 8": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "Social Studies", "Computer"],
|
| 28 |
-
"Grade 9": ["Mathematics", "English", "Urdu", "Biology", "Chemistry", "Physics", "Islamiyat", "Pakistan Studies"
|
| 29 |
-
"Grade 10": ["Mathematics", "English", "Urdu", "Biology", "Chemistry", "Physics", "Islamiyat", "Pakistan Studies"
|
| 30 |
"Grade 11": ["Mathematics", "English", "Urdu", "Biology", "Chemistry", "Physics", "Computer", "Islamiyat", "Pakistan Studies"],
|
| 31 |
"Grade 12": ["Mathematics", "English", "Urdu", "Biology", "Chemistry", "Physics", "Computer", "Islamiyat", "Pakistan Studies"]
|
| 32 |
}
|
| 33 |
|
| 34 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
chapters_by_subject_and_grade = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
"Grade 11": {
|
| 37 |
-
"Mathematics": ["Advanced Algebra", "
|
| 38 |
-
"Biology": ["Cell Structure", "Biological Molecules", "Enzymes", "Plant Diversity"],
|
| 39 |
-
"Chemistry": ["Stoichiometry", "Atomic Structure", "Bonding", "States of Matter"],
|
| 40 |
-
"Physics": ["Forces & Motion", "Work & Energy", "Oscillations & Waves"],
|
| 41 |
-
"Computer": ["Programming Fundamentals", "Data Structures", "Databases"],
|
| 42 |
"English": ["Academic Writing", "Literature Studies", "Critical Reading"],
|
| 43 |
"Urdu": ["ادبی مطالعہ", "نظم و نثر کی تشریح"],
|
| 44 |
-
"
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
"Grade 12": {
|
| 48 |
"Mathematics": ["Calculus", "Advanced Algebra", "Statistics", "Analytical Geometry"],
|
| 49 |
-
"Biology": ["Respiration & Exchange","Homeostasis","Support & Movement","Reproduction","Genetics"],
|
| 50 |
-
"Chemistry": ["s and p Block Elements","d Block & Transition Elements","Organic Reactions","Hydrocarbons"],
|
| 51 |
-
"Physics": ["Electromagnetism","AC Circuits","Semiconductors","Atomic & Nuclear Physics"],
|
| 52 |
-
"Computer": ["Advanced Programming", "Web Technologies", "Networks"],
|
| 53 |
"English": ["Research Writing", "Advanced Literature", "Critical Analysis"],
|
| 54 |
"Urdu": ["تحقیق و تنقید", "ادبی شخصیات"],
|
| 55 |
-
"
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
}
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
mcqs = [
|
| 65 |
-
answers = [random.choice(["A","B","C","D"]) for _ in mcqs]
|
| 66 |
-
return mcqs, answers
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
def export_to_word(mcqs, answers, grade, subject, chapter, difficulty):
|
| 69 |
doc = Document()
|
| 70 |
-
|
|
|
|
| 71 |
heading.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
| 72 |
run = heading.runs[0]
|
| 73 |
run.bold = True
|
|
|
|
| 74 |
run.font.size = Pt(18)
|
| 75 |
|
| 76 |
-
table = doc.add_table(rows=
|
| 77 |
table.style = 'Table Grid'
|
| 78 |
-
info = [
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
for row, (label, value) in zip(table.rows, info):
|
| 81 |
row.cells[0].text = label
|
| 82 |
row.cells[1].text = value
|
| 83 |
row.cells[0].paragraphs[0].runs[0].bold = True
|
| 84 |
|
| 85 |
for i, q in enumerate(mcqs, start=1):
|
| 86 |
-
doc.add_paragraph(f"{i}. {q}")
|
|
|
|
| 87 |
|
| 88 |
doc.add_paragraph("\nAnswer Key:")
|
| 89 |
-
ans_table = doc.add_table(rows=
|
| 90 |
ans_table.style = 'Table Grid'
|
| 91 |
headers = ["Q#", "A", "B", "C", "D"]
|
| 92 |
for idx, text in enumerate(headers):
|
| 93 |
ans_table.cell(0, idx).text = text
|
| 94 |
ans_table.cell(0, idx).paragraphs[0].runs[0].bold = True
|
|
|
|
| 95 |
for r, ans in enumerate(answers, start=1):
|
| 96 |
ans_table.cell(r,0).text = str(r)
|
| 97 |
for c, option in enumerate(["A","B","C","D"], start=1):
|
| 98 |
ans_table.cell(r,c).text = "✔" if ans==option else ""
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
doc.save(file_path)
|
| 101 |
return file_path
|
| 102 |
|
| 103 |
-
def on_generate(grade, subject, chapter, difficulty
|
| 104 |
-
mcqs, answers = generate_mcqs(grade, subject, chapter, difficulty
|
| 105 |
file_path = export_to_word(mcqs, answers, grade, subject, chapter, difficulty)
|
| 106 |
return f"Test ready: {file_path}", file_path
|
| 107 |
|
| 108 |
-
#
|
|
|
|
|
|
|
| 109 |
with gr.Blocks() as demo:
|
| 110 |
-
gr.Markdown("### **MCQ
|
| 111 |
with gr.Row():
|
| 112 |
grade_dropdown = gr.Dropdown(choices=grades, label="Select Grade")
|
| 113 |
-
subject_dropdown = gr.Dropdown(choices=[], label="Select Subject"
|
| 114 |
-
chapter_dropdown = gr.Dropdown(choices=[], label="Select Chapter"
|
| 115 |
-
difficulty_dropdown = gr.Dropdown(choices=difficulty_levels, label="Select Difficulty")
|
| 116 |
-
num_mcqs_slider = gr.Slider(minimum=5, maximum=50, step=1, label="Number of MCQs", value=20)
|
| 117 |
-
generate_btn = gr.Button("Generate Test")
|
| 118 |
-
output_text = gr.Textbox(label="Status")
|
| 119 |
-
output_file = gr.File(label="Download Test")
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
return gr.update(choices=chapters, value=chapters[0] if chapters else None)
|
| 130 |
|
|
|
|
|
|
|
| 131 |
grade_dropdown.change(update_subjects, inputs=grade_dropdown, outputs=subject_dropdown)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
subject_dropdown.change(update_chapters, inputs=[subject_dropdown, grade_dropdown], outputs=chapter_dropdown)
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
from groq import Groq
|
|
|
|
| 9 |
import re
|
| 10 |
import random
|
| 11 |
|
| 12 |
+
# ---------------------------
|
| 13 |
# Initialize GROQ client
|
| 14 |
+
# ---------------------------
|
| 15 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 16 |
client = Groq(api_key=GROQ_API_KEY)
|
| 17 |
|
| 18 |
+
# ---------------------------
|
| 19 |
+
# Grades and subjects
|
| 20 |
+
# ---------------------------
|
| 21 |
grades = [f"Grade {i}" for i in range(1, 13)]
|
|
|
|
|
|
|
| 22 |
subjects_by_grade = {
|
| 23 |
"Grade 1": ["Mathematics", "English", "Urdu", "Islamiyat", "General Knowledge"],
|
| 24 |
"Grade 2": ["Mathematics", "English", "Urdu", "Islamiyat", "General Knowledge"],
|
| 25 |
"Grade 3": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "General Knowledge"],
|
| 26 |
+
"Grade 4": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "General Knowledge"],
|
| 27 |
"Grade 5": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "Social Studies"],
|
| 28 |
"Grade 6": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "Social Studies"],
|
| 29 |
"Grade 7": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "Social Studies", "Computer"],
|
| 30 |
"Grade 8": ["Mathematics", "English", "Urdu", "Science", "Islamiyat", "Social Studies", "Computer"],
|
| 31 |
+
"Grade 9": ["Mathematics", "English", "Urdu", "Biology", "Chemistry", "Physics", "Islamiyat", "Pakistan Studies"],
|
| 32 |
+
"Grade 10": ["Mathematics", "English", "Urdu", "Biology", "Chemistry", "Physics", "Islamiyat", "Pakistan Studies"],
|
| 33 |
"Grade 11": ["Mathematics", "English", "Urdu", "Biology", "Chemistry", "Physics", "Computer", "Islamiyat", "Pakistan Studies"],
|
| 34 |
"Grade 12": ["Mathematics", "English", "Urdu", "Biology", "Chemistry", "Physics", "Computer", "Islamiyat", "Pakistan Studies"]
|
| 35 |
}
|
| 36 |
|
| 37 |
+
# ---------------------------
|
| 38 |
+
# Chapter lists (Grades 9-12)
|
| 39 |
+
# Based on Govt. of Pakistan, Ministry of Education
|
| 40 |
+
# National Curriculum (2006) — official chapter names used for XI-XII
|
| 41 |
+
# Sources: Biology XI-XII, Chemistry XI-XII, Physics XI-XII, Pakistan Studies XI-XII.
|
| 42 |
+
# ---------------------------
|
| 43 |
+
|
| 44 |
chapters_by_subject_and_grade = {
|
| 45 |
+
# Grades 1-8 & other subjects have reasonable placeholders (unchanged from earlier)
|
| 46 |
+
"Grade 1": {
|
| 47 |
+
"Mathematics": ["Counting & Numbers", "Basic Addition", "Basic Subtraction", "Shapes", "Measurements"],
|
| 48 |
+
"English": ["Alphabet & Sounds", "Basic Words", "Simple Sentences", "Listening & Speaking", "Reading Short Texts"],
|
| 49 |
+
"Urdu": ["حروف", "الفاظ", "سادہ جملے", "پڑھنا", "لکھنا"],
|
| 50 |
+
"Islamiyat": ["Basic Beliefs", "Prophets Stories", "Good Manners", "Prayers & Worship"],
|
| 51 |
+
"General Knowledge": ["My Body", "My Home", "My School", "Seasons"]
|
| 52 |
+
},
|
| 53 |
+
"Grade 2": {
|
| 54 |
+
"Mathematics": ["Place Value", "Addition & Subtraction", "Money", "Time", "Shapes"],
|
| 55 |
+
"English": ["Parts of Speech Basics", "Simple Grammar", "Short Comprehension", "Vocabulary Building"],
|
| 56 |
+
"Urdu": ["الفاظ و جملے", "مختصر کہانیاں", "حروفِ صحیح"],
|
| 57 |
+
"Islamiyat": ["Prophets", "Good Deeds", "Mosque Etiquette"],
|
| 58 |
+
"General Knowledge": ["Plants & Animals", "Local Community", "Transport"]
|
| 59 |
+
},
|
| 60 |
+
"Grade 3": {
|
| 61 |
+
"Mathematics": ["Multiplication", "Division", "Fractions", "Geometry Basics", "Measurement"],
|
| 62 |
+
"English": ["Tenses (simple)", "Comprehension Passages", "Vocabulary", "Sentence Formation"],
|
| 63 |
+
"Urdu": ["کالم و مضامین", "قواعدِ اردو", "کہانیاں"],
|
| 64 |
+
"Science": ["Plants", "Animals", "Human Body", "Materials"],
|
| 65 |
+
"Islamiyat": ["Basics of Iman", "Seerat", "Manners"]
|
| 66 |
+
},
|
| 67 |
+
"Grade 4": {
|
| 68 |
+
"Mathematics": ["Fractions & Decimals", "Geometry", "Data Handling", "Word Problems"],
|
| 69 |
+
"English": ["Grammar (intermediate)", "Comprehension", "Writing Short Paragraphs"],
|
| 70 |
+
"Urdu": ["نظم و نثر", "قواعد", "مفردات"],
|
| 71 |
+
"Science": ["Ecosystems", "Forces", "Heat & Energy"],
|
| 72 |
+
"Islamiyat": ["Pillars of Islam", "Seerah Stories"]
|
| 73 |
+
},
|
| 74 |
+
"Grade 5": {
|
| 75 |
+
"Mathematics": ["Numbers & Operations", "Geometry", "Statistics", "Ratio & Proportion"],
|
| 76 |
+
"English": ["Advanced Grammar Basics", "Paragraph Writing", "Comprehension"],
|
| 77 |
+
"Urdu": ["ادبی مطالعہ", "تعبیر و تشریح"],
|
| 78 |
+
"Science": ["Cells & Life", "Matter", "Energy"],
|
| 79 |
+
"Islamiyat": ["Quranic Stories", "Akhlq"]
|
| 80 |
+
},
|
| 81 |
+
"Grade 6": {
|
| 82 |
+
"Mathematics": ["Number System", "Algebra Introduction", "Geometry", "Data Handling"],
|
| 83 |
+
"English": ["Grammar (complex)", "Essay Writing", "Comprehension Skills"],
|
| 84 |
+
"Urdu": ["ادب و نحو", "تحریر"],
|
| 85 |
+
"Science": ["Atoms & Molecules", "Human Body", "Plants"],
|
| 86 |
+
"Islamiyat": ["Seerah & Ahkam"]
|
| 87 |
+
},
|
| 88 |
+
"Grade 7": {
|
| 89 |
+
"Mathematics": ["Algebra", "Geometry", "Mensuration", "Statistics"],
|
| 90 |
+
"English": ["Poetry & Prose", "Grammar", "Comprehension"],
|
| 91 |
+
"Urdu": ["نثر و شاعری", "قواعد"],
|
| 92 |
+
"Science": ["Cell Biology", "Human Systems", "Electricity Basics"],
|
| 93 |
+
"Islamiyat": ["Islamic History", "Moral Teachings"],
|
| 94 |
+
"Social Studies": ["Geography Basics", "History", "Civics"],
|
| 95 |
+
"Computer": ["Basic ICT", "Computer Hardware", "Introduction to Coding"]
|
| 96 |
+
},
|
| 97 |
+
"Grade 8": {
|
| 98 |
+
"Mathematics": ["Linear Equations", "Geometry", "Pythagoras", "Graphs"],
|
| 99 |
+
"English": ["Advanced Comprehension", "Composition", "Grammar"],
|
| 100 |
+
"Urdu": ["تجزیہ ادب", "تحقیقِ مختصر"],
|
| 101 |
+
"Science": ["Forces & Motion", "Heat", "Waves Basics"],
|
| 102 |
+
"Islamiyat": ["Islamic Law Basics", "Seerah"],
|
| 103 |
+
"Social Studies": ["Regional Geography", "Historical Events"],
|
| 104 |
+
"Computer": ["Algorithms", "Basic Programming"]
|
| 105 |
+
},
|
| 106 |
+
|
| 107 |
+
# -----------------------
|
| 108 |
+
# Grade 9 (kept as you gave)
|
| 109 |
+
# -----------------------
|
| 110 |
+
"Grade 9": {
|
| 111 |
+
"Mathematics": ["Number Systems & Algebra", "Coordinate Geometry", "Trigonometry", "Graphs"],
|
| 112 |
+
"English": ["Advanced Grammar", "Comprehension", "Writing Skills"],
|
| 113 |
+
"Urdu": ["ادب اور نثر", "نظم", "مضامین"],
|
| 114 |
+
"Biology": [
|
| 115 |
+
"INTRODUCTION TO BIOLOGY",
|
| 116 |
+
"SOLVING A BIOLOGICAL PROBLEM",
|
| 117 |
+
"BIODIVERSITY",
|
| 118 |
+
"CELLS AND TISSUES",
|
| 119 |
+
"CELL CYCLE",
|
| 120 |
+
"ENZYMES",
|
| 121 |
+
"BIOENERGETICS",
|
| 122 |
+
"NUTRITION",
|
| 123 |
+
"TRANSPORT"
|
| 124 |
+
],
|
| 125 |
+
"Chemistry": [
|
| 126 |
+
"FUNDAMENTALS OF CHEMISTRY",
|
| 127 |
+
"STRUCTURE OF ATOMS",
|
| 128 |
+
"PERIODIC TABLE AND PERIODICITY OF PROPERTIES",
|
| 129 |
+
"STRUCTURE OF MOLECULES",
|
| 130 |
+
"PHYSICAL STATES OF MATTER",
|
| 131 |
+
"SOLUTIONS",
|
| 132 |
+
"ELECTROCHEMISTRY",
|
| 133 |
+
"CHEMICAL REACTIVITY"
|
| 134 |
+
],
|
| 135 |
+
"Physics": [
|
| 136 |
+
"PHYSICAL QUANTITIES AND MEASUREMENT",
|
| 137 |
+
"KINEMATICS",
|
| 138 |
+
"DYNAMICS",
|
| 139 |
+
"TURNING EFFECT OF FORCES",
|
| 140 |
+
"GRAVITATION",
|
| 141 |
+
"WORK AND ENERGY",
|
| 142 |
+
"PROPERTIES OF MATTER",
|
| 143 |
+
"THERMAL PROPERTIES OF MATTER",
|
| 144 |
+
"TRANSFER OF HEAT"
|
| 145 |
+
],
|
| 146 |
+
"Islamiyat": ["Seerat of Prophet", "Faith & Beliefs", "Worship Practices"],
|
| 147 |
+
"Pakistan Studies": [
|
| 148 |
+
"IDEOLOGICAL BASIS OF PAKISTAN",
|
| 149 |
+
"MAKING OF PAKISTAN",
|
| 150 |
+
"LAND AND ENVIRONMENT",
|
| 151 |
+
"HISTORY OF PAKISTAN"
|
| 152 |
+
]
|
| 153 |
+
},
|
| 154 |
+
|
| 155 |
+
# -----------------------
|
| 156 |
+
# Grade 10 (kept as you gave)
|
| 157 |
+
# -----------------------
|
| 158 |
+
"Grade 10": {
|
| 159 |
+
"Mathematics": ["Advanced Algebra", "Trigonometry", "Geometry", "Probability"],
|
| 160 |
+
"English": ["Literature", "Advanced Composition", "Comprehension"],
|
| 161 |
+
"Urdu": ["ادبِ جدید", "تحقیقی مضمون"],
|
| 162 |
+
"Biology": [
|
| 163 |
+
"GASEOUS EXCHANGE",
|
| 164 |
+
"HOMEOSTASIS",
|
| 165 |
+
"COORDINATION",
|
| 166 |
+
"SUPPORT AND MOVEMENT",
|
| 167 |
+
"REPRODUCTION",
|
| 168 |
+
"INHERITANCE",
|
| 169 |
+
"MAN AND HIS ENVIRONMENT",
|
| 170 |
+
"BIOTECHNOLOGY",
|
| 171 |
+
"PHARMA"
|
| 172 |
+
],
|
| 173 |
+
"Chemistry": [
|
| 174 |
+
"CHEMICAL EQUILIBRIUM",
|
| 175 |
+
"ACIDS, BASES AND SALTS",
|
| 176 |
+
"ORGANIC CHEMISTRY",
|
| 177 |
+
"HYDROCARBONS",
|
| 178 |
+
"BIOCHEMISTRY",
|
| 179 |
+
"ENVIRONMENTAL CHEMISTRY I: THE ATMOSPHERE",
|
| 180 |
+
"ENVIRONMENTAL CHEMISTRY II: WATER",
|
| 181 |
+
"CHEMICAL INDUSTRIES"
|
| 182 |
+
],
|
| 183 |
+
"Physics": [
|
| 184 |
+
"SIMPLE HARMONIC MOTION AND WAVES",
|
| 185 |
+
"SOUND",
|
| 186 |
+
"GEOMETRICAL OPTICS",
|
| 187 |
+
"ELECTROSTATICS",
|
| 188 |
+
"CURRENT ELECTRICITY",
|
| 189 |
+
"ELECTROMAGNETISM",
|
| 190 |
+
"INTRODUCTORY ELECTRONICS",
|
| 191 |
+
"INFORMATION AND COMMUNICATION TECHNOLOGY",
|
| 192 |
+
"RADIOACTIVITY"
|
| 193 |
+
],
|
| 194 |
+
"Islamiyat": ["Islamic Studies II", "Ethics & Society", "Fiqh Basics"],
|
| 195 |
+
"Pakistan Studies": [
|
| 196 |
+
"HISTORY OF PAKISTAN-II",
|
| 197 |
+
"PAKISTAN IN WORLD AFFAIRS",
|
| 198 |
+
"ECONOMIC DEVELOPMENTS",
|
| 199 |
+
"POPULATION, SOCIETY AND CULTURE OF PAKISTAN"
|
| 200 |
+
]
|
| 201 |
+
},
|
| 202 |
+
|
| 203 |
+
# -----------------------
|
| 204 |
+
# Grade 11 (XI) — official chapter/unit titles from National Curriculum 2006 (XI-XII)
|
| 205 |
+
# Sources: Biology XI-XII; Chemistry XI-XII; Physics XI-XII; Pakistan Studies XI-XII.
|
| 206 |
+
# -----------------------
|
| 207 |
"Grade 11": {
|
| 208 |
+
"Mathematics": ["Advanced Algebra & Calculus Intro", "Coordinate Geometry", "Trigonometry", "Vectors"],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
"English": ["Academic Writing", "Literature Studies", "Critical Reading"],
|
| 210 |
"Urdu": ["ادبی مطالعہ", "نظم و نثر کی تشریح"],
|
| 211 |
+
"Biology": [
|
| 212 |
+
"Cell Structure and Functions",
|
| 213 |
+
"Biological Molecules",
|
| 214 |
+
"Enzymes",
|
| 215 |
+
"Bioenergetics",
|
| 216 |
+
"Acellular Life",
|
| 217 |
+
"Prokaryotes",
|
| 218 |
+
"Protists and Fungi",
|
| 219 |
+
"Diversity among Plants",
|
| 220 |
+
"Diversity among Animals",
|
| 221 |
+
"Form and Functions in Plants",
|
| 222 |
+
"Digestion",
|
| 223 |
+
"Circulation",
|
| 224 |
+
"Immunity"
|
| 225 |
+
],
|
| 226 |
+
"Chemistry": [
|
| 227 |
+
"Stoichiometry",
|
| 228 |
+
"Atomic Structure",
|
| 229 |
+
"Theories of Covalent Bonding and Shapes of Molecules",
|
| 230 |
+
"States of Matter I: Gases",
|
| 231 |
+
"States of Matter II: Liquids",
|
| 232 |
+
"States of Matter III: Solids",
|
| 233 |
+
"Chemical Equilibrium",
|
| 234 |
+
"Acids, Bases and Salts",
|
| 235 |
+
"Chemical Kinetics",
|
| 236 |
+
"Solutions and Colloids",
|
| 237 |
+
"Thermochemistry",
|
| 238 |
+
"Oxidation, Reduction and Electrochemistry"
|
| 239 |
+
],
|
| 240 |
+
"Physics": [
|
| 241 |
+
"Measurement",
|
| 242 |
+
"Vectors and Equilibrium",
|
| 243 |
+
"Forces and Motion",
|
| 244 |
+
"Work and Energy",
|
| 245 |
+
"Rotational and Circular Motion",
|
| 246 |
+
"Fluid Dynamics",
|
| 247 |
+
"Oscillations",
|
| 248 |
+
"Waves",
|
| 249 |
+
"Physical Optics",
|
| 250 |
+
"Thermodynamics",
|
| 251 |
+
"Electrostatics",
|
| 252 |
+
"Current Electricity"
|
| 253 |
+
],
|
| 254 |
+
"Computer": ["Programming Fundamentals", "Data Structures Intro", "Databases"],
|
| 255 |
+
"Islamiyat": ["Advanced Seerah", "Comparative Religion"],
|
| 256 |
+
"Pakistan Studies": [
|
| 257 |
+
"Pakistan in Geographical Perspective",
|
| 258 |
+
"Pakistan Resources",
|
| 259 |
+
"Historical Perspective (Ancient to Modern)",
|
| 260 |
+
"Islam and Pakistan",
|
| 261 |
+
"Administrative System",
|
| 262 |
+
"Political and Constitutional Developments",
|
| 263 |
+
"Human Rights"
|
| 264 |
+
]
|
| 265 |
},
|
| 266 |
+
|
| 267 |
+
# -----------------------
|
| 268 |
+
# Grade 12 (XII) — official chapter/unit titles from National Curriculum 2006 (XI-XII)
|
| 269 |
+
# -----------------------
|
| 270 |
"Grade 12": {
|
| 271 |
"Mathematics": ["Calculus", "Advanced Algebra", "Statistics", "Analytical Geometry"],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
"English": ["Research Writing", "Advanced Literature", "Critical Analysis"],
|
| 273 |
"Urdu": ["تحقیق و تنقید", "ادبی شخصیات"],
|
| 274 |
+
"Biology": [
|
| 275 |
+
"Respiration",
|
| 276 |
+
"Homeostasis",
|
| 277 |
+
"Support and Movement",
|
| 278 |
+
"Nervous Coordination",
|
| 279 |
+
"Chemical Coordination",
|
| 280 |
+
"Behavior",
|
| 281 |
+
"Reproduction",
|
| 282 |
+
"Development and Aging",
|
| 283 |
+
"Inheritance",
|
| 284 |
+
"Chromosome and DNA",
|
| 285 |
+
"Evolution",
|
| 286 |
+
"Man and His Environment",
|
| 287 |
+
"Biotechnology",
|
| 288 |
+
"Biology and Human Welfare"
|
| 289 |
+
],
|
| 290 |
+
"Chemistry": [
|
| 291 |
+
"s and p Block Elements",
|
| 292 |
+
"d-Block Elements",
|
| 293 |
+
"Organic Compounds",
|
| 294 |
+
"Hydrocarbons",
|
| 295 |
+
"Alkyl Halides and Amines",
|
| 296 |
+
"Alcohols and Phenols",
|
| 297 |
+
"Aldehydes and Ketones",
|
| 298 |
+
"Carboxylic Acids and Functional Derivatives",
|
| 299 |
+
"Biochemistry",
|
| 300 |
+
"Industrial Chemistry",
|
| 301 |
+
"Environmental Chemistry",
|
| 302 |
+
"Analytical Chemistry"
|
| 303 |
+
],
|
| 304 |
+
"Physics": [
|
| 305 |
+
"Electromagnetism",
|
| 306 |
+
"Electromagnetic Induction",
|
| 307 |
+
"Alternating Current",
|
| 308 |
+
"Physics of Solids",
|
| 309 |
+
"Electronics",
|
| 310 |
+
"Dawn of Modern Physics",
|
| 311 |
+
"Atomic Spectra",
|
| 312 |
+
"Nuclear Physics",
|
| 313 |
+
"(Plus continuation of units from Grade XI as needed)"
|
| 314 |
+
],
|
| 315 |
+
"Computer": ["Web Technologies", "Advanced Programming Concepts"],
|
| 316 |
+
"Islamiyat": ["Advanced Islamic Thought", "Islamic Jurisprudence"],
|
| 317 |
+
"Pakistan Studies": [
|
| 318 |
+
"Society and Culture",
|
| 319 |
+
"Foreign Relations of Pakistan",
|
| 320 |
+
"Economic Development",
|
| 321 |
+
"Sports, Tourism and National Identity",
|
| 322 |
+
"Contemporary Challenges and Policies"
|
| 323 |
+
]
|
| 324 |
}
|
| 325 |
}
|
| 326 |
|
| 327 |
+
# ---------------------------
|
| 328 |
+
# Mediums
|
| 329 |
+
# ---------------------------
|
| 330 |
+
mediums = ["English"]
|
| 331 |
+
|
| 332 |
+
# ---------------------------
|
| 333 |
+
# MCQ generation function (unchanged behaviour, now uses 'chapter' + 'difficulty')
|
| 334 |
+
# ---------------------------
|
| 335 |
+
def generate_mcqs(grade, subject, chapter, difficulty):
|
| 336 |
+
system_msg = "You are an educational expert. Generate MCQs strictly aligned with the Pakistani National Curriculum (2006)."
|
| 337 |
+
user_msg = (
|
| 338 |
+
f"Create 20 MCQs for {grade} {subject} on the chapter '{chapter}'. "
|
| 339 |
+
f"Difficulty level: {difficulty}.\n"
|
| 340 |
+
"Each question must have 4 options labeled A, B, C, D. "
|
| 341 |
+
"Do not add extra sentences before or after. "
|
| 342 |
+
"Provide answers separately in an answer key table at the end."
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
response = client.chat.completions.create(
|
| 346 |
+
messages=[
|
| 347 |
+
{"role": "system", "content": system_msg},
|
| 348 |
+
{"role": "user", "content": user_msg}
|
| 349 |
+
],
|
| 350 |
+
model="llama-3.3-70b-versatile"
|
| 351 |
+
)
|
| 352 |
|
| 353 |
+
text = response.choices[0].message.content
|
| 354 |
+
mcqs = re.split(r'\n?\d+\.', text)
|
| 355 |
+
mcqs = [q.strip() for q in mcqs if q.strip()]
|
|
|
|
|
|
|
| 356 |
|
| 357 |
+
# Random answer key (demo) — keep your original behaviour
|
| 358 |
+
answer_key = [random.choice(["A","B","C","D"]) for _ in mcqs]
|
| 359 |
+
|
| 360 |
+
return mcqs[:20], answer_key[:20]
|
| 361 |
+
|
| 362 |
+
# ---------------------------
|
| 363 |
+
# DOCX export (kept unchanged)
|
| 364 |
+
# ---------------------------
|
| 365 |
def export_to_word(mcqs, answers, grade, subject, chapter, difficulty):
|
| 366 |
doc = Document()
|
| 367 |
+
|
| 368 |
+
heading = doc.add_heading("WEEKLY/MONTHLY TEST", level=1)
|
| 369 |
heading.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
| 370 |
run = heading.runs[0]
|
| 371 |
run.bold = True
|
| 372 |
+
run.font.color.rgb = RGBColor(0, 128, 0)
|
| 373 |
run.font.size = Pt(18)
|
| 374 |
|
| 375 |
+
table = doc.add_table(rows=6, cols=2)
|
| 376 |
table.style = 'Table Grid'
|
| 377 |
+
info = [
|
| 378 |
+
("Grade", grade),
|
| 379 |
+
("Subject", subject),
|
| 380 |
+
("Chapter", chapter),
|
| 381 |
+
("Difficulty", difficulty),
|
| 382 |
+
("Date", datetime.now().strftime("%d-%m-%Y")),
|
| 383 |
+
("Medium", "English")
|
| 384 |
+
]
|
| 385 |
for row, (label, value) in zip(table.rows, info):
|
| 386 |
row.cells[0].text = label
|
| 387 |
row.cells[1].text = value
|
| 388 |
row.cells[0].paragraphs[0].runs[0].bold = True
|
| 389 |
|
| 390 |
for i, q in enumerate(mcqs, start=1):
|
| 391 |
+
p = doc.add_paragraph(f"{i}. {q}")
|
| 392 |
+
p.alignment = WD_ALIGN_PARAGRAPH.LEFT
|
| 393 |
|
| 394 |
doc.add_paragraph("\nAnswer Key:")
|
| 395 |
+
ans_table = doc.add_table(rows=21, cols=5)
|
| 396 |
ans_table.style = 'Table Grid'
|
| 397 |
headers = ["Q#", "A", "B", "C", "D"]
|
| 398 |
for idx, text in enumerate(headers):
|
| 399 |
ans_table.cell(0, idx).text = text
|
| 400 |
ans_table.cell(0, idx).paragraphs[0].runs[0].bold = True
|
| 401 |
+
|
| 402 |
for r, ans in enumerate(answers, start=1):
|
| 403 |
ans_table.cell(r,0).text = str(r)
|
| 404 |
for c, option in enumerate(["A","B","C","D"], start=1):
|
| 405 |
ans_table.cell(r,c).text = "✔" if ans==option else ""
|
| 406 |
+
|
| 407 |
+
safe_chapter = re.sub(r'[\\/*?:"<>|]', "_", chapter)
|
| 408 |
+
file_path = f"{grade}_{subject}_{safe_chapter}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.docx"
|
| 409 |
doc.save(file_path)
|
| 410 |
return file_path
|
| 411 |
|
| 412 |
+
def on_generate(grade, subject, chapter, difficulty):
|
| 413 |
+
mcqs, answers = generate_mcqs(grade, subject, chapter, difficulty)
|
| 414 |
file_path = export_to_word(mcqs, answers, grade, subject, chapter, difficulty)
|
| 415 |
return f"Test ready: {file_path}", file_path
|
| 416 |
|
| 417 |
+
# ---------------------------
|
| 418 |
+
# Gradio UI (kept layout & behaviour; added Difficulty dropdown)
|
| 419 |
+
# ---------------------------
|
| 420 |
with gr.Blocks() as demo:
|
| 421 |
+
gr.Markdown("### **MCQ Test Generator - Developer: Najaf Ali Sharqi**")
|
| 422 |
with gr.Row():
|
| 423 |
grade_dropdown = gr.Dropdown(choices=grades, label="Select Grade")
|
| 424 |
+
subject_dropdown = gr.Dropdown(choices=[], label="Select Subject")
|
| 425 |
+
chapter_dropdown = gr.Dropdown(choices=[], label="Select Chapter")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
|
| 427 |
+
difficulty_dropdown = gr.Dropdown(
|
| 428 |
+
choices=["Absolute Beginner (Pretest)", "Intermediate", "Advanced"],
|
| 429 |
+
value="Intermediate",
|
| 430 |
+
label="Select Difficulty Level"
|
| 431 |
+
)
|
| 432 |
|
| 433 |
+
generate_btn = gr.Button("Generate Test")
|
| 434 |
+
output_text = gr.Textbox(label="Status / Generated MCQs", lines=18)
|
| 435 |
+
output_file = gr.File(label="Download Test (.docx)")
|
|
|
|
| 436 |
|
| 437 |
+
def update_subjects(selected_grade):
|
| 438 |
+
return gr.update(choices=subjects_by_grade.get(selected_grade, []), value=None)
|
| 439 |
grade_dropdown.change(update_subjects, inputs=grade_dropdown, outputs=subject_dropdown)
|
| 440 |
+
|
| 441 |
+
def update_chapters(selected_subject, selected_grade):
|
| 442 |
+
chapters = chapters_by_subject_and_grade.get(selected_grade, {}).get(selected_subject, [])
|
| 443 |
+
return gr.update(choices=chapters, value=None)
|
| 444 |
+
|
| 445 |
subject_dropdown.change(update_chapters, inputs=[subject_dropdown, grade_dropdown], outputs=chapter_dropdown)
|
| 446 |
+
grade_dropdown.change(update_chapters, inputs=[subject_dropdown, grade_dropdown], outputs=chapter_dropdown)
|
| 447 |
+
|
| 448 |
+
generate_btn.click(
|
| 449 |
+
on_generate,
|
| 450 |
+
inputs=[grade_dropdown, subject_dropdown, chapter_dropdown, difficulty_dropdown],
|
| 451 |
+
outputs=[output_text, output_file]
|
| 452 |
+
)
|
| 453 |
|
| 454 |
+
demo.launch(share=True, ssr_mode=False)
|