Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
import datetime
|
| 5 |
+
|
| 6 |
+
# Load questions
|
| 7 |
+
with open("questions.json", "r") as f:
|
| 8 |
+
questions = json.load(f)
|
| 9 |
+
|
| 10 |
+
# Pick 50 random questions
|
| 11 |
+
def get_random_questions():
|
| 12 |
+
return random.sample(questions, 50)
|
| 13 |
+
|
| 14 |
+
def run_test(name, father_name, roll_number, *answers):
|
| 15 |
+
selected_questions = gr.get_state("questions")
|
| 16 |
+
correct = 0
|
| 17 |
+
attempted = 0
|
| 18 |
+
|
| 19 |
+
for i, ans in enumerate(answers):
|
| 20 |
+
if ans:
|
| 21 |
+
attempted += 1
|
| 22 |
+
if ans == selected_questions[i]["answer"]:
|
| 23 |
+
correct += 1
|
| 24 |
+
|
| 25 |
+
percentage = round((correct / 50) * 100, 2)
|
| 26 |
+
|
| 27 |
+
# Prepare result text
|
| 28 |
+
result_text = (
|
| 29 |
+
f"Name: {name}\n"
|
| 30 |
+
f"Father's Name: {father_name}\n"
|
| 31 |
+
f"Roll Number: {roll_number}\n"
|
| 32 |
+
f"Attempted: {attempted}/50\n"
|
| 33 |
+
f"Correct: {correct}\n"
|
| 34 |
+
f"Percentage: {percentage}%\n"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Save result file
|
| 38 |
+
filename = f"result_{roll_number}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.txt"
|
| 39 |
+
with open(filename, "w") as f:
|
| 40 |
+
f.write(result_text)
|
| 41 |
+
|
| 42 |
+
return result_text, filename
|
| 43 |
+
|
| 44 |
+
def build_interface():
|
| 45 |
+
random_questions = get_random_questions()
|
| 46 |
+
gr.set_state("questions", random_questions)
|
| 47 |
+
|
| 48 |
+
with gr.Blocks() as demo:
|
| 49 |
+
with gr.Row():
|
| 50 |
+
name = gr.Textbox(label="Student Name", placeholder="Enter your name")
|
| 51 |
+
father_name = gr.Textbox(label="Father's Name", placeholder="Enter your father's name")
|
| 52 |
+
roll_number = gr.Textbox(label="Roll Number", placeholder="Enter roll number")
|
| 53 |
+
|
| 54 |
+
answers = []
|
| 55 |
+
for i, q in enumerate(random_questions):
|
| 56 |
+
with gr.Row():
|
| 57 |
+
answers.append(
|
| 58 |
+
gr.Radio(q["options"], label=f"Q{i+1}. {q['question']}", type="value")
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
submit = gr.Button("Submit Test")
|
| 62 |
+
result_box = gr.Textbox(label="Result")
|
| 63 |
+
download_file = gr.File(label="Download Result")
|
| 64 |
+
|
| 65 |
+
submit.click(
|
| 66 |
+
fn=run_test,
|
| 67 |
+
inputs=[name, father_name, roll_number] + answers,
|
| 68 |
+
outputs=[result_box, download_file],
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
return demo
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
app = build_interface()
|
| 75 |
+
app.launch()
|