test6iq / app.py
Hidayatmahar's picture
Update app.py
9c0cb64 verified
import gradio as gr
import json
import random
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
# Load all questions from JSON
with open("questionsiq.json", "r") as f:
all_questions = json.load(f)
# Function to pick 50 random questions
def get_random_questions():
return random.sample(all_questions, 50)
# Store selected questions for the current session
selected_questions = get_random_questions()
# Function to create PDF report
def generate_pdf(name, father, roll, results, filename):
c = canvas.Canvas(filename, pagesize=A4)
width, height = A4
y = height - 50
c.setFont("Helvetica-Bold", 16)
c.drawString(50, y, "Class 6 IQ Test Result")
y -= 30
c.setFont("Helvetica", 12)
c.drawString(50, y, f"Student Name: {name}")
y -= 20
c.drawString(50, y, f"Father's Name: {father}")
y -= 20
c.drawString(50, y, f"Roll Number: {roll}")
y -= 30
correct = sum(1 for r in results if r["Result"] == "Correct")
attempted = sum(1 for r in results if r["Your Answer"])
total = len(results)
percentage = (correct / total) * 100 if total > 0 else 0
c.drawString(50, y, f"Total Questions: {total}")
y -= 20
c.drawString(50, y, f"Attempted Questions: {attempted}")
y -= 20
c.drawString(50, y, f"Correct Answers: {correct}")
y -= 20
c.drawString(50, y, f"Percentage: {percentage:.2f}%")
y -= 30
c.setFont("Helvetica-Bold", 12)
c.drawString(50, y, "Detailed Question-wise Results:")
y -= 20
c.setFont("Helvetica", 10)
for i, r in enumerate(results):
if y < 50:
c.showPage()
y = height - 50
c.drawString(50, y, f"Q{i+1}: {r['Question']}")
y -= 15
c.drawString(60, y, f"Your Answer: {r['Your Answer']} | Correct Answer: {r['Correct Answer']} | Result: {r['Result']}")
y -= 20
c.save()
# Run test function
def run_test(name, father, roll, *answers):
results = []
for i, q in enumerate(selected_questions):
user_ans = answers[i] if i < len(answers) else ""
result = "Correct" if user_ans == q["answer"] else "Wrong"
results.append({
"Question": q["question"],
"Your Answer": user_ans,
"Correct Answer": q["answer"],
"Result": result
})
total_questions = len(selected_questions)
attempted = sum(1 for r in results if r["Your Answer"])
correct = sum(1 for r in results if r["Result"] == "Correct")
percentage = (correct / total_questions) * 100 if total_questions > 0 else 0
# Generate PDF
pdf_file = f"{roll}_result.pdf"
generate_pdf(name, father, roll, results, pdf_file)
# Prepare result text for screen
result_text = (
f"Test Completed!\n\n"
f"Total Questions: {total_questions}\n"
f"Attempted Questions: {attempted}\n"
f"Correct Answers: {correct}\n"
f"Percentage: {percentage:.2f}%\n\n"
f"PDF report generated: {pdf_file}"
)
return result_text, pdf_file
# Build Gradio interface
with gr.Blocks() as demo:
gr.Markdown("## Class 6 IQ Test (50 Random Questions)")
with gr.Row():
name_input = gr.Textbox(label="Student Name")
father_input = gr.Textbox(label="Father's Name")
roll_input = gr.Textbox(label="Roll Number")
gr.Markdown("### Answer all the questions below:")
# Create question components dynamically
question_components = []
for i, q in enumerate(selected_questions):
question_components.append(
gr.Radio(
label=f"Q{i+1}: {q['question']}",
choices=q["options"],
type="value"
)
)
submit_btn = gr.Button("Submit Test")
output_text = gr.Textbox(label="Result", lines=8)
download_pdf = gr.File(label="Download Result PDF")
# Connect submit button
submit_btn.click(
fn=run_test,
inputs=[name_input, father_input, roll_input] + question_components,
outputs=[output_text, download_pdf]
)
demo.launch()