Spaces:
Sleeping
Sleeping
File size: 4,093 Bytes
4e0e392 6a64024 062425f 1ce5c82 9e3536e 062425f 759da3b 062425f 73c2d55 9c0cb64 6a64024 9c0cb64 6a64024 1ce5c82 9c0cb64 1ce5c82 9c0cb64 1ce5c82 6a64024 73c2d55 6a64024 1ce5c82 73c2d55 6a64024 1ce5c82 3f4aadd 6a64024 9c0cb64 1ce5c82 6a64024 9c0cb64 2f041d4 1ce5c82 73c2d55 1ce5c82 6a64024 73c2d55 1ce5c82 73c2d55 6a64024 1ce5c82 9c0cb64 1ce5c82 6a64024 1ce5c82 6a64024 73c2d55 9c0cb64 1ce5c82 6a64024 9c0cb64 73c2d55 1ce5c82 73c2d55 4e0e392 73c2d55 | 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 | 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()
|