File size: 3,929 Bytes
84575f9
bb9c586
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84575f9
 
bb9c586
84575f9
 
 
 
bb9c586
 
 
 
 
84575f9
bb9c586
 
 
 
84575f9
bb9c586
84575f9
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import gradio as gr
from fpdf import FPDF
import tempfile
import datetime

# --------------------------
# Questions Bank
# --------------------------

english_questions = [
    {
        "question": "Choose the correct synonym of 'Happy':",
        "options": ["Sad", "Joyful", "Angry", "Tired"],
        "answer": "Joyful"
    },
    {
        "question": "Choose the correct antonym of 'Cold':",
        "options": ["Hot", "Freezing", "Cool", "Chilly"],
        "answer": "Hot"
    },
    {
        "question": "Fill in the blank: She _____ to school every day.",
        "options": ["go", "goes", "gone", "going"],
        "answer": "goes"
    }
] * 10  # Repeat to make 30 for example

islamiat_questions = [
    {
        "question": "Who was the first Caliph of Islam?",
        "options": ["Hazrat Umar (RA)", "Hazrat Abu Bakr (RA)", "Hazrat Ali (RA)", "Hazrat Usman (RA)"],
        "answer": "Hazrat Abu Bakr (RA)"
    },
    {
        "question": "How many times Muslims pray in a day?",
        "options": ["3", "4", "5", "6"],
        "answer": "5"
    },
    {
        "question": "Which Surah is the shortest in the Quran?",
        "options": ["Al-Fatiha", "Al-Ikhlas", "Al-Kawthar", "Al-Nas"],
        "answer": "Al-Kawthar"
    }
] * 10  # Repeat to make 30

math_questions = [
    {
        "question": "What is 15 + 6?",
        "options": ["21", "20", "22", "19"],
        "answer": "21"
    },
    {
        "question": "What is 9 × 7?",
        "options": ["63", "72", "56", "49"],
        "answer": "63"
    },
    {
        "question": "What is 100 ÷ 4?",
        "options": ["20", "25", "24", "30"],
        "answer": "25"
    }
] * 10  # Repeat to make 30

questions_bank = {
    "English": english_questions,
    "Islamiat": islamiat_questions,
    "Math": math_questions
}

# --------------------------
# PDF Generation Function
# --------------------------

def generate_pdf(name, father_name, roll_number, score, total):
    percentage = (score / total) * 100
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=16)
    pdf.cell(200, 10, txt="5th Class Test Result Card", ln=True, align='C')
    pdf.ln(10)
    pdf.set_font("Arial", size=12)
    pdf.cell(200, 10, txt=f"Name: {name}", ln=True)
    pdf.cell(200, 10, txt=f"Father's Name: {father_name}", ln=True)
    pdf.cell(200, 10, txt=f"Roll Number: {roll_number}", ln=True)
    pdf.cell(200, 10, txt=f"Score: {score}/{total}", ln=True)
    pdf.cell(200, 10, txt=f"Percentage: {percentage:.2f}%", ln=True)
    pdf.cell(200, 10, txt=f"Date: {datetime.date.today()}", ln=True)

    temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
    pdf.output(temp_pdf.name)
    return temp_pdf.name

# --------------------------
# Test Logic
# --------------------------

def test_app(name, father_name, roll_number, *answers):
    score = 0
    total = sum(len(q_list) for q_list in questions_bank.values())

    # Check answers
    idx = 0
    for section in questions_bank:
        for q in questions_bank[section]:
            if answers[idx] == q["answer"]:
                score += 1
            idx += 1

    pdf_path = generate_pdf(name, father_name, roll_number, score, total)
    return f"You scored {score}/{total} ({(score/total)*100:.2f}%)", pdf_path

# --------------------------
# Build Gradio Interface
# --------------------------

inputs = [
    gr.Textbox(label="Name"),
    gr.Textbox(label="Father's Name"),
    gr.Textbox(label="Roll Number")
]

# Add questions dynamically
for section, q_list in questions_bank.items():
    inputs.append(gr.Label(value=f"--- {section} Section ---"))
    for q in q_list:
        inputs.append(gr.Radio(choices=q["options"], label=q["question"]))

outputs = [
    gr.Textbox(label="Result"),
    gr.File(label="Download Result PDF")
]

demo = gr.Interface(fn=test_app, inputs=inputs, outputs=outputs, title="5th Class Test")

if __name__ == "__main__":
    demo.launch()