Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,16 +3,16 @@ import json
|
|
| 3 |
import random
|
| 4 |
from reportlab.lib.pagesizes import A4
|
| 5 |
from reportlab.pdfgen import canvas
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
# Load all questions from JSON
|
| 9 |
with open("questionsiq.json", "r") as f:
|
| 10 |
all_questions = json.load(f)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
def get_random_questions():
|
| 14 |
return random.sample(all_questions, 50)
|
| 15 |
|
|
|
|
| 16 |
selected_questions = get_random_questions()
|
| 17 |
|
| 18 |
# Function to create PDF report
|
|
@@ -22,7 +22,7 @@ def generate_pdf(name, father, roll, results, filename):
|
|
| 22 |
y = height - 50
|
| 23 |
|
| 24 |
c.setFont("Helvetica-Bold", 16)
|
| 25 |
-
c.drawString(50, y,
|
| 26 |
y -= 30
|
| 27 |
|
| 28 |
c.setFont("Helvetica", 12)
|
|
@@ -53,7 +53,7 @@ def generate_pdf(name, father, roll, results, filename):
|
|
| 53 |
|
| 54 |
c.setFont("Helvetica", 10)
|
| 55 |
for i, r in enumerate(results):
|
| 56 |
-
if y < 50:
|
| 57 |
c.showPage()
|
| 58 |
y = height - 50
|
| 59 |
c.drawString(50, y, f"Q{i+1}: {r['Question']}")
|
|
@@ -77,10 +77,26 @@ def run_test(name, father, roll, *answers):
|
|
| 77 |
"Result": result
|
| 78 |
})
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
pdf_file = f"{roll}_result.pdf"
|
| 81 |
generate_pdf(name, father, roll, results, pdf_file)
|
| 82 |
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
# Build Gradio interface
|
| 86 |
with gr.Blocks() as demo:
|
|
@@ -93,6 +109,7 @@ with gr.Blocks() as demo:
|
|
| 93 |
|
| 94 |
gr.Markdown("### Answer all the questions below:")
|
| 95 |
|
|
|
|
| 96 |
question_components = []
|
| 97 |
for i, q in enumerate(selected_questions):
|
| 98 |
question_components.append(
|
|
@@ -104,9 +121,10 @@ with gr.Blocks() as demo:
|
|
| 104 |
)
|
| 105 |
|
| 106 |
submit_btn = gr.Button("Submit Test")
|
| 107 |
-
output_text = gr.Textbox(label="Result", lines=
|
| 108 |
download_pdf = gr.File(label="Download Result PDF")
|
| 109 |
|
|
|
|
| 110 |
submit_btn.click(
|
| 111 |
fn=run_test,
|
| 112 |
inputs=[name_input, father_input, roll_input] + question_components,
|
|
|
|
| 3 |
import random
|
| 4 |
from reportlab.lib.pagesizes import A4
|
| 5 |
from reportlab.pdfgen import canvas
|
|
|
|
| 6 |
|
| 7 |
# Load all questions from JSON
|
| 8 |
with open("questionsiq.json", "r") as f:
|
| 9 |
all_questions = json.load(f)
|
| 10 |
|
| 11 |
+
# Function to pick 50 random questions
|
| 12 |
def get_random_questions():
|
| 13 |
return random.sample(all_questions, 50)
|
| 14 |
|
| 15 |
+
# Store selected questions for the current session
|
| 16 |
selected_questions = get_random_questions()
|
| 17 |
|
| 18 |
# Function to create PDF report
|
|
|
|
| 22 |
y = height - 50
|
| 23 |
|
| 24 |
c.setFont("Helvetica-Bold", 16)
|
| 25 |
+
c.drawString(50, y, "Class 6 IQ Test Result")
|
| 26 |
y -= 30
|
| 27 |
|
| 28 |
c.setFont("Helvetica", 12)
|
|
|
|
| 53 |
|
| 54 |
c.setFont("Helvetica", 10)
|
| 55 |
for i, r in enumerate(results):
|
| 56 |
+
if y < 50:
|
| 57 |
c.showPage()
|
| 58 |
y = height - 50
|
| 59 |
c.drawString(50, y, f"Q{i+1}: {r['Question']}")
|
|
|
|
| 77 |
"Result": result
|
| 78 |
})
|
| 79 |
|
| 80 |
+
total_questions = len(selected_questions)
|
| 81 |
+
attempted = sum(1 for r in results if r["Your Answer"])
|
| 82 |
+
correct = sum(1 for r in results if r["Result"] == "Correct")
|
| 83 |
+
percentage = (correct / total_questions) * 100 if total_questions > 0 else 0
|
| 84 |
+
|
| 85 |
+
# Generate PDF
|
| 86 |
pdf_file = f"{roll}_result.pdf"
|
| 87 |
generate_pdf(name, father, roll, results, pdf_file)
|
| 88 |
|
| 89 |
+
# Prepare result text for screen
|
| 90 |
+
result_text = (
|
| 91 |
+
f"Test Completed!\n\n"
|
| 92 |
+
f"Total Questions: {total_questions}\n"
|
| 93 |
+
f"Attempted Questions: {attempted}\n"
|
| 94 |
+
f"Correct Answers: {correct}\n"
|
| 95 |
+
f"Percentage: {percentage:.2f}%\n\n"
|
| 96 |
+
f"PDF report generated: {pdf_file}"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
return result_text, pdf_file
|
| 100 |
|
| 101 |
# Build Gradio interface
|
| 102 |
with gr.Blocks() as demo:
|
|
|
|
| 109 |
|
| 110 |
gr.Markdown("### Answer all the questions below:")
|
| 111 |
|
| 112 |
+
# Create question components dynamically
|
| 113 |
question_components = []
|
| 114 |
for i, q in enumerate(selected_questions):
|
| 115 |
question_components.append(
|
|
|
|
| 121 |
)
|
| 122 |
|
| 123 |
submit_btn = gr.Button("Submit Test")
|
| 124 |
+
output_text = gr.Textbox(label="Result", lines=8)
|
| 125 |
download_pdf = gr.File(label="Download Result PDF")
|
| 126 |
|
| 127 |
+
# Connect submit button
|
| 128 |
submit_btn.click(
|
| 129 |
fn=run_test,
|
| 130 |
inputs=[name_input, father_input, roll_input] + question_components,
|