Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fpdf import FPDF
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
# ----------------------------
|
| 7 |
+
# Data: Questions
|
| 8 |
+
# ----------------------------
|
| 9 |
+
QUESTIONS = [
|
| 10 |
+
{"q": "1) 125 + 248 = ?", "options": ["343", "373", "383", "413"], "answer": "373"},
|
| 11 |
+
{"q": "2) 600 − 375 = ?", "options": ["125", "215", "225", "235"], "answer": "225"},
|
| 12 |
+
{"q": "3) 45 × 8 = ?", "options": ["320", "360", "380", "400"], "answer": "360"},
|
| 13 |
+
{"q": "4) 924 ÷ 4 = ?", "options": ["221", "231", "241", "246"], "answer": "231"},
|
| 14 |
+
{"q": "5) Which fraction is larger?", "options": ["1/4", "1/3", "1/5", "1/6"], "answer": "1/3"},
|
| 15 |
+
{"q": "6) 3/4 + 2/4 = ?", "options": ["3/8", "1/4", "5/4", "1"], "answer": "5/4"},
|
| 16 |
+
{"q": "7) 5.6 + 2.45 = ?", "options": ["7.95", "8.05", "8.15", "8.25"], "answer": "8.05"},
|
| 17 |
+
{"q": "8) 7.8 − 3.25 = ?", "options": ["4.45", "4.55", "4.65", "4.75"], "answer": "4.55"},
|
| 18 |
+
{"q": "9) 1/3 of 12 is:", "options": ["2", "3", "4", "5"], "answer": "4"},
|
| 19 |
+
{"q": "10) Area of rectangle 12cm × 8cm:", "options": ["20 cm²", "80 cm²", "96 cm²", "100 cm²"], "answer": "96 cm²"},
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
# ----------------------------
|
| 23 |
+
# Helpers
|
| 24 |
+
# ----------------------------
|
| 25 |
+
def evaluate(name, father_name, roll_no, *user_answers):
|
| 26 |
+
# Store answers
|
| 27 |
+
answers = list(user_answers)
|
| 28 |
+
correct = 0
|
| 29 |
+
details = []
|
| 30 |
+
for i, q in enumerate(QUESTIONS):
|
| 31 |
+
selected = answers[i]
|
| 32 |
+
is_correct = (selected == q["answer"])
|
| 33 |
+
if is_correct:
|
| 34 |
+
correct += 1
|
| 35 |
+
details.append({
|
| 36 |
+
"no": i + 1,
|
| 37 |
+
"question": q["q"],
|
| 38 |
+
"selected": selected if selected else "—",
|
| 39 |
+
"correct": q["answer"],
|
| 40 |
+
"status": "✔" if is_correct else "✘",
|
| 41 |
+
})
|
| 42 |
+
|
| 43 |
+
total = len(QUESTIONS)
|
| 44 |
+
percent = round((correct / total) * 100, 2)
|
| 45 |
+
|
| 46 |
+
# Create PDF
|
| 47 |
+
pdf = FPDF()
|
| 48 |
+
pdf.add_page()
|
| 49 |
+
pdf.set_auto_page_break(auto=True, margin=12)
|
| 50 |
+
pdf.set_font("Arial", "B", 16)
|
| 51 |
+
pdf.cell(0, 10, "5th Class Test - Result Card", ln=True, align="C")
|
| 52 |
+
pdf.ln(2)
|
| 53 |
+
|
| 54 |
+
pdf.set_font("Arial", "", 12)
|
| 55 |
+
pdf.cell(0, 8, f"Name: {name}", ln=True)
|
| 56 |
+
pdf.cell(0, 8, f"Father's Name: {father_name}", ln=True)
|
| 57 |
+
pdf.cell(0, 8, f"Roll Number: {roll_no}", ln=True)
|
| 58 |
+
pdf.cell(0, 8, f"Date & Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True)
|
| 59 |
+
pdf.ln(4)
|
| 60 |
+
|
| 61 |
+
pdf.set_font("Arial", "B", 13)
|
| 62 |
+
pdf.cell(0, 8, "Summary", ln=True)
|
| 63 |
+
pdf.set_font("Arial", "", 12)
|
| 64 |
+
pdf.cell(0, 8, f"Total Questions: {total}", ln=True)
|
| 65 |
+
pdf.cell(0, 8, f"Correct Answers: {correct}", ln=True)
|
| 66 |
+
pdf.cell(0, 8, f"Percentage: {percent}%", ln=True)
|
| 67 |
+
pdf.ln(4)
|
| 68 |
+
|
| 69 |
+
# Table header
|
| 70 |
+
pdf.set_font("Arial", "B", 12)
|
| 71 |
+
pdf.cell(10, 8, "No", border=1, align="C")
|
| 72 |
+
pdf.cell(90, 8, "Question", border=1)
|
| 73 |
+
pdf.cell(30, 8, "Selected", border=1, align="C")
|
| 74 |
+
pdf.cell(30, 8, "Correct", border=1, align="C")
|
| 75 |
+
pdf.cell(20, 8, "Status", border=1, ln=True, align="C")
|
| 76 |
+
|
| 77 |
+
pdf.set_font("Arial", "", 11)
|
| 78 |
+
for row in details:
|
| 79 |
+
q_text = row["question"]
|
| 80 |
+
if len(q_text) > 60:
|
| 81 |
+
q_text = q_text[:57] + "..."
|
| 82 |
+
pdf.cell(10, 8, str(row["no"]), border=1, align="C")
|
| 83 |
+
pdf.cell(90, 8, q_text, border=1)
|
| 84 |
+
pdf.cell(30, 8, row["selected"], border=1, align="C")
|
| 85 |
+
pdf.cell(30, 8, row["correct"], border=1, align="C")
|
| 86 |
+
pdf.cell(20, 8, row["status"], border=1, ln=True, align="C")
|
| 87 |
+
|
| 88 |
+
# Output PDF as bytes
|
| 89 |
+
pdf_bytes = BytesIO()
|
| 90 |
+
pdf.output(pdf_bytes)
|
| 91 |
+
pdf_bytes.seek(0)
|
| 92 |
+
|
| 93 |
+
# Return percentage & downloadable file
|
| 94 |
+
return f"✅ Score: {correct}/{total} — {percent}%", pdf_bytes
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
# ----------------------------
|
| 98 |
+
# Build Gradio UI
|
| 99 |
+
# ----------------------------
|
| 100 |
+
inputs = [
|
| 101 |
+
gr.Textbox(label="Student Name"),
|
| 102 |
+
gr.Textbox(label="Father's Name"),
|
| 103 |
+
gr.Textbox(label="Roll Number")
|
| 104 |
+
]
|
| 105 |
+
|
| 106 |
+
# Add MCQ questions
|
| 107 |
+
for q in QUESTIONS:
|
| 108 |
+
inputs.append(
|
| 109 |
+
gr.Radio(choices=q["options"], label=q["q"], type="value")
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
outputs = [
|
| 113 |
+
gr.Textbox(label="Result Summary"),
|
| 114 |
+
gr.File(label="Download Result Card (PDF)")
|
| 115 |
+
]
|
| 116 |
+
|
| 117 |
+
demo = gr.Interface(
|
| 118 |
+
fn=evaluate,
|
| 119 |
+
inputs=inputs,
|
| 120 |
+
outputs=outputs,
|
| 121 |
+
title="🧮 5th Class Test",
|
| 122 |
+
description="Answer the questions and get your result card as a PDF."
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
if __name__ == "__main__":
|
| 126 |
+
demo.launch()
|