Test5 / app.py
Hidayatmahar's picture
Update app.py
bb9c586 verified
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()