Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
import datetime
|
| 5 |
+
|
| 6 |
+
# Load questions from JSON file
|
| 7 |
+
with open("questions.json", "r", encoding="utf-8") as f:
|
| 8 |
+
all_questions = json.load(f)
|
| 9 |
+
|
| 10 |
+
# Global storage for session
|
| 11 |
+
sessions = {}
|
| 12 |
+
|
| 13 |
+
def start_test(name, father_name, roll_number):
|
| 14 |
+
# Pick 50 random questions
|
| 15 |
+
questions = random.sample(all_questions, 50)
|
| 16 |
+
session_id = roll_number + "_" + str(datetime.datetime.now().timestamp())
|
| 17 |
+
|
| 18 |
+
# Save session data
|
| 19 |
+
sessions[session_id] = {
|
| 20 |
+
"name": name,
|
| 21 |
+
"father_name": father_name,
|
| 22 |
+
"roll_number": roll_number,
|
| 23 |
+
"questions": questions,
|
| 24 |
+
"current": 0,
|
| 25 |
+
"score": 0,
|
| 26 |
+
"answers": []
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
q = questions[0]
|
| 30 |
+
return session_id, f"Q1: {q['question']}", gr.Radio(choices=q["options"], value=None, label="Choose your answer"), f"Question 1 of 50"
|
| 31 |
+
|
| 32 |
+
def next_question(session_id, answer):
|
| 33 |
+
data = sessions[session_id]
|
| 34 |
+
|
| 35 |
+
# Record answer
|
| 36 |
+
current_q = data["questions"][data["current"]]
|
| 37 |
+
data["answers"].append({"question": current_q["question"], "your_answer": answer, "correct": current_q["answer"]})
|
| 38 |
+
if answer == current_q["answer"]:
|
| 39 |
+
data["score"] += 1
|
| 40 |
+
|
| 41 |
+
data["current"] += 1
|
| 42 |
+
|
| 43 |
+
if data["current"] >= len(data["questions"]):
|
| 44 |
+
# Test finished
|
| 45 |
+
result_text = (
|
| 46 |
+
f"Result Card\n"
|
| 47 |
+
f"Name: {data['name']}\n"
|
| 48 |
+
f"Father's Name: {data['father_name']}\n"
|
| 49 |
+
f"Roll No: {data['roll_number']}\n"
|
| 50 |
+
f"Total Questions: 50\n"
|
| 51 |
+
f"Correct: {data['score']}\n"
|
| 52 |
+
f"Wrong: {50 - data['score']}\n"
|
| 53 |
+
f"Percentage: {data['score'] * 100 / 50:.2f}%\n"
|
| 54 |
+
)
|
| 55 |
+
file_name = f"result_{data['roll_number']}.txt"
|
| 56 |
+
with open(file_name, "w", encoding="utf-8") as f:
|
| 57 |
+
f.write(result_text)
|
| 58 |
+
|
| 59 |
+
return "Test Completed!", None, None, result_text, file_name
|
| 60 |
+
|
| 61 |
+
# Show next question
|
| 62 |
+
q = data["questions"][data["current"]]
|
| 63 |
+
return (
|
| 64 |
+
session_id,
|
| 65 |
+
f"Q{data['current']+1}: {q['question']}",
|
| 66 |
+
gr.Radio(choices=q["options"], value=None, label="Choose your answer"),
|
| 67 |
+
f"Question {data['current']+1} of 50",
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
with gr.Blocks() as demo:
|
| 72 |
+
gr.Markdown("## 📝 5th Class Intelligence Test (50 Questions)")
|
| 73 |
+
|
| 74 |
+
with gr.Tab("Start Test"):
|
| 75 |
+
name = gr.Textbox(label="Your Name")
|
| 76 |
+
father_name = gr.Textbox(label="Father's Name")
|
| 77 |
+
roll_number = gr.Textbox(label="Roll Number")
|
| 78 |
+
start_btn = gr.Button("Start Test")
|
| 79 |
+
|
| 80 |
+
session_id = gr.State()
|
| 81 |
+
question_text = gr.Textbox(label="Question", interactive=False)
|
| 82 |
+
options = gr.Radio(label="Options")
|
| 83 |
+
q_count = gr.Textbox(label="Progress", interactive=False)
|
| 84 |
+
next_btn = gr.Button("Next")
|
| 85 |
+
result_box = gr.Textbox(label="Result", interactive=False)
|
| 86 |
+
result_file = gr.File(label="Download Result")
|
| 87 |
+
|
| 88 |
+
start_btn.click(
|
| 89 |
+
start_test,
|
| 90 |
+
inputs=[name, father_name, roll_number],
|
| 91 |
+
outputs=[session_id, question_text, options, q_count]
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
next_btn.click(
|
| 95 |
+
next_question,
|
| 96 |
+
inputs=[session_id, options],
|
| 97 |
+
outputs=[session_id, question_text, options, q_count, result_box, result_file]
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
demo.launch()
|