Spaces:
Sleeping
Sleeping
| import random | |
| import datetime | |
| import tempfile | |
| from typing import List, Dict | |
| import gradio as gr | |
| from reportlab.lib.pagesizes import letter | |
| from reportlab.pdfgen import canvas | |
| # --------------------------- Question Pool Generator --------------------------- # | |
| def generate_question_pool(seed: int = 42) -> List[Dict]: | |
| random.seed(seed) | |
| pool = [] | |
| # Helper to add a question | |
| def add(q, options, answer): | |
| pool.append({"q": q, "options": options, "answer": answer}) | |
| # 1) Simple sequences | |
| for start, step in [(2,2), (3,3), (5,5), (1,4), (7,2), (10,5), (4,6)]: | |
| seq = [start + i*step for i in range(4)] | |
| next_val = start + 4*step | |
| wrongs = [next_val + d for d in (-2, 2, 4)] | |
| options = [str(next_val)] + list(map(str, wrongs)) | |
| random.shuffle(options) | |
| add(f"Find the next number: {', '.join(map(str, seq))}, ?", options, str(next_val)) | |
| # 2) Even/odd identification | |
| for n in [9, 11, 13, 15, 17, 19, 21, 23]: | |
| options = ["2", "4", "6", str(n)] | |
| random.shuffle(options) | |
| add("Which number is odd?", options, str(n)) | |
| for n in [12, 18, 24, 36, 44, 56, 64, 88]: | |
| options = [str(n), "15", "21", "27"] | |
| random.shuffle(options) | |
| add("Which number is even?", options, str(n)) | |
| # 3) Odd one out (multiples) | |
| for base, group in [(3,[6,9,12,14]), (5,[10,20,25,24]), (4,[8,16,12,18]), (7,[14,21,28,26])]: | |
| # The last one in group is the odd one out | |
| options = list(map(str, group)) | |
| answer = str([x for x in group if x % base != 0][0]) | |
| random.shuffle(options) | |
| add("Find the odd one out.", options, answer) | |
| # 4) Shapes | |
| add("Which shape has 3 sides?", ["Square", "Triangle", "Circle", "Rectangle"], "Triangle") | |
| add("Which shape has no sides?", ["Triangle", "Square", "Circle", "Pentagon"], "Circle") | |
| add("How many sides does a rectangle have?", ["3", "4", "5", "6"], "4") | |
| add("A square has all sides...", ["Unequal", "Equal", "Curved", "Zero"], "Equal") | |
| # 5) Simple arithmetic in brackets | |
| for a,b,c in [(5,8,3),(10,6,4),(7,9,5),(12,7,2),(8,5,1)]: | |
| expr = f"{a} + ({b} - {c})" | |
| val = a + (b - c) | |
| options = [str(val), str(val+1), str(val-1), str(val+2)] | |
| random.shuffle(options) | |
| add(f"{expr} = ?", options, str(val)) | |
| # 6) Greatest/Smallest numbers | |
| for group in [[12,7,19,3],[45,60,58,59],[101,99,100,98],[31,29,35,33]]: | |
| options = list(map(str, group)) | |
| answer = str(max(group)) | |
| random.shuffle(options) | |
| add("Which is the greatest number?", options, answer) | |
| for group in [[12,7,19,3],[45,60,58,59],[101,99,100,98],[31,29,35,33]]: | |
| options = list(map(str, group)) | |
| answer = str(min(group)) | |
| random.shuffle(options) | |
| add("Which is the smallest number?", options, answer) | |
| # 7) Word analogies (simple & age-appropriate) | |
| analogies = [ | |
| ("CAT is to KITTEN as DOG is to ___", ["CUB", "CALF", "PUPPY", "COLT"], "PUPPY"), | |
| ("HEN is to CHICK as COW is to ___", ["CALF", "PUPPY", "LAMB", "FOAL"], "CALF"), | |
| ("SUN is to DAY as MOON is to ___", ["LIGHT", "NIGHT", "STAR", "SKY"], "NIGHT"), | |
| ("BIRD is to NEST as BEE is to ___", ["HIVE", "HOLE", "CAVE", "WEB"], "HIVE"), | |
| ("TEACHER is to SCHOOL as DOCTOR is to ___", ["HOSPITAL", "MARKET", "HOME", "PARK"], "HOSPITAL"), | |
| ("HAND is to GLOVE as FOOT is to ___", ["HAT", "SOCK", "BAG", "BELT"], "SOCK"), | |
| ("FISH is to WATER as CAMEL is to ___", ["DESERT", "RIVER", "FOREST", "ICE"], "DESERT"), | |
| ("EAR is to HEAR as EYE is to ___", ["SEE", "TOUCH", "SMELL", "TASTE"], "SEE"), | |
| ("RAIN is to WET as FIRE is to ___", ["HOT", "COLD", "DRY", "DARK"], "HOT"), | |
| ("DAY is to LIGHT as NIGHT is to ___", ["BLACK", "DARK", "WHITE", "SUN"], "DARK"), | |
| ] | |
| for q, opts, ans in analogies: | |
| options = opts[:] | |
| random.shuffle(options) | |
| add(q, options, ans) | |
| # 8) Simple ordering | |
| ordering_groups = [ | |
| ["April", "June", "May", "March"], | |
| ["Monday", "Wednesday", "Friday", "Sunday"], | |
| ["One", "Three", "Two", "Four"], | |
| ["Spring", "Summer", "Winter", "Autumn"], | |
| ] | |
| for group in ordering_groups: | |
| # Ask which comes first alphabetically | |
| options = group[:] | |
| answer = min(group) | |
| random.shuffle(options) | |
| add("Which comes first alphabetically?", options, answer) | |
| # 9) Simple logic | |
| logic_qs = [ | |
| ("If a clock shows 3 o'clock, the hour hand points to ___", ["3", "6", "9", "12"], "3"), | |
| ("Ali is older than Sara, and Sara is older than Hamid. Who is the youngest?", ["Ali", "Sara", "Hamid", "All same"], "Hamid"), | |
| ("A bicycle has ___ wheels.", ["1", "2", "3", "4"], "2"), | |
| ("Which is a mammal?", ["Frog", "Shark", "Whale", "Lizard"], "Whale"), | |
| ("Which animal cannot fly?", ["Sparrow", "Eagle", "Bat", "Penguin"], "Penguin"), | |
| ] | |
| for q, opts, ans in logic_qs: | |
| options = opts[:] | |
| random.shuffle(options) | |
| add(q, options, ans) | |
| # 10) Simple fractions (very basic) | |
| add("Which is larger?", ["1/2", "1/3", "1/4", "1/5"], "1/2") | |
| add("Which is smaller?", ["3/4", "1/2", "1/3", "1/5"], "1/5") | |
| # 11) Patterns with letters/numbers | |
| patterns = [ | |
| ("What comes next? A, C, E, G, ___", ["H", "I", "J", "K"], "I"), | |
| ("What comes next? B, D, F, H, ___", ["I", "J", "K", "L"], "J"), | |
| ("What comes next? 5, 10, 15, 20, ___", ["22", "24", "25", "30"], "25"), | |
| ("What comes next? 1, 4, 9, 16, ___", ["20", "24", "25", "30"], "25"), | |
| ] | |
| for q, opts, ans in patterns: | |
| options = opts[:] | |
| random.shuffle(options) | |
| add(q, options, ans) | |
| # 12) Simple sums & differences | |
| for a,b in [(25,17),(40,12),(33,9),(18,7),(29,16)]: | |
| val = a + b | |
| options = [str(val), str(val-1), str(val+1), str(val-2)] | |
| random.shuffle(options) | |
| add(f"{a} + {b} = ?", options, str(val)) | |
| for a,b in [(25,17),(40,12),(33,9),(18,7),(29,16)]: | |
| val = a - b | |
| options = [str(val), str(val-1), str(val+1), str(val+2)] | |
| random.shuffle(options) | |
| add(f"{a} - {b} = ?", options, str(val)) | |
| # Ensure pool size > 100 | |
| # Add a few quick general knowledge suitable for 5th grade | |
| gk = [ | |
| ("How many days are there in a week?", ["5", "6", "7", "8"], "7"), | |
| ("How many months are there in a year?", ["10", "11", "12", "13"], "12"), | |
| ("Which planet do we live on?", ["Mars", "Venus", "Earth", "Jupiter"], "Earth"), | |
| ("How many letters are there in the English alphabet?", ["24", "25", "26", "27"], "26"), | |
| ("What do bees make?", ["Milk", "Honey", "Juice", "Oil"], "Honey"), | |
| ("What do we breathe in?", ["Carbon dioxide", "Oxygen", "Hydrogen", "Nitrogen"], "Oxygen"), | |
| ("Which color results by mixing red and blue?", ["Green", "Purple", "Orange", "Brown"], "Purple"), | |
| ("Which season is the coldest?", ["Summer", "Winter", "Spring", "Autumn"], "Winter"), | |
| ("Which sense organ helps us smell?", ["Ear", "Nose", "Tongue", "Skin"], "Nose"), | |
| ("How many sides does a triangle have?", ["2", "3", "4", "5"], "3"), | |
| ] | |
| for q, opts, ans in gk: | |
| options = opts[:] | |
| random.shuffle(options) | |
| add(q, options, ans) | |
| return pool | |
| QUESTION_POOL = generate_question_pool(seed=2025) | |
| # --------------------------- Helpers --------------------------- # | |
| def sample_quiz(n=50) -> List[Dict]: | |
| """Sample n questions and shuffle options per question""" | |
| qs = random.sample(QUESTION_POOL, k=n) | |
| # Shuffle options & keep correct as text | |
| quiz = [] | |
| for item in qs: | |
| opts = item["options"][:] | |
| random.shuffle(opts) | |
| quiz.append({ | |
| "q": item["q"], | |
| "options": opts, | |
| "answer": item["answer"] | |
| }) | |
| return quiz | |
| def build_pdf(name, father, roll, score, total): | |
| temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") | |
| c = canvas.Canvas(temp_file.name, pagesize=letter) | |
| c.setFont("Helvetica-Bold", 16) | |
| c.drawString(140, 760, "5th Class Intelligence Test - Result Card") | |
| c.setFont("Helvetica", 12) | |
| c.drawString(50, 720, f"Name: {name}") | |
| c.drawString(50, 700, f"Father's Name: {father}") | |
| c.drawString(50, 680, f"Roll Number: {roll}") | |
| percentage = round((score/total)*100, 2) | |
| c.drawString(50, 660, f"Score: {score} / {total}") | |
| c.drawString(50, 640, f"Percentage: {percentage}%") | |
| c.drawString(50, 620, f"Date: {datetime.date.today().isoformat()}") | |
| c.line(50, 610, 560, 610) | |
| c.setFont("Helvetica-Oblique", 10) | |
| c.drawString(50, 595, "Well done! Keep practicing logical thinking, patterns, and basic math.") | |
| c.save() | |
| return temp_file.name | |
| # --------------------------- Gradio App Logic --------------------------- # | |
| def start_test(name, father, roll): | |
| if not name or not father or not roll: | |
| return ([gr.update()] * 50) + [gr.update(visible=False), "Please fill all details to start the test.", None, None] | |
| quiz = sample_quiz(50) | |
| # Build updates for 50 Radio components | |
| updates = [] | |
| for i in range(50): | |
| q = quiz[i] | |
| updates.append( | |
| gr.update(label=f"Q{i+1}. {q['q']}", choices=q["options"], value=None) | |
| ) | |
| # Show quiz box, store quiz in JSON string in state | |
| return updates + [gr.update(visible=True), "Test loaded. Answer all questions and click Submit.", quiz, {"name": name, "father": father, "roll": roll}] | |
| def submit_test(*args): | |
| # args: 50 answers + quiz_state + info_state | |
| answers = list(args[:50]) | |
| quiz = args[50] # list of dicts | |
| info = args[51] # dict with name/father/roll | |
| if not quiz or not isinstance(quiz, list): | |
| return "Please start the test first.", None | |
| score = 0 | |
| for i, sel in enumerate(answers): | |
| correct = quiz[i]["answer"] | |
| if sel == correct: | |
| score += 1 | |
| result_text = f"Name: {info['name']}\nFather's Name: {info['father']}\nRoll No: {info['roll']}\nScore: {score}/50\nPercentage: {round(score/50*100,2)}%" | |
| pdf_path = build_pdf(info['name'], info['father'], info['roll'], score, 50) | |
| return result_text, pdf_path | |
| # --------------------------- UI --------------------------- # | |
| with gr.Blocks(title="5th Class Intelligence Test (Gradio)") as demo: | |
| gr.Markdown("# 5th Class Intelligence Test\nAnswer 50 questions. Download your result as PDF.") | |
| with gr.Group(): | |
| with gr.Row(): | |
| name = gr.Textbox(label="Student Name", placeholder="Enter full name") | |
| father = gr.Textbox(label="Father's Name", placeholder="Enter father's name") | |
| roll = gr.Textbox(label="Roll Number", placeholder="Enter roll number") | |
| start = gr.Button("Start Test", variant="primary") | |
| quiz_box = gr.Accordion("Questions (50)", open=True, visible=False) | |
| radios = [] | |
| with quiz_box: | |
| for i in range(50): | |
| r = gr.Radio(label=f"Q{i+1}", choices=["A", "B", "C", "D"], show_label=True) | |
| radios.append(r) | |
| submit = gr.Button("Submit") | |
| status = gr.Markdown("") | |
| result_text = gr.Textbox(label="Result", interactive=False) | |
| result_pdf = gr.File(label="Download Result PDF", interactive=False) | |
| quiz_state = gr.State(value=None) # list of q dicts | |
| info_state = gr.State(value=None) # dict | |
| # Start button wires | |
| start_outputs = radios + [quiz_box, status, quiz_state, info_state] | |
| start.click( | |
| start_test, | |
| inputs=[name, father, roll], | |
| outputs=start_outputs | |
| ) | |
| # Submit button wires | |
| submit.click( | |
| submit_test, | |
| inputs=radios + [quiz_state, info_state], | |
| outputs=[result_text, result_pdf] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |