Hidayatmahar commited on
Commit
db2edfa
·
verified ·
1 Parent(s): d4ac588

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+ import json
4
+ from fpdf import FPDF
5
+
6
+ # Load question bank
7
+ with open("questions.json", "r", encoding="utf-8") as f:
8
+ question_bank = json.load(f)
9
+
10
+ # Pick 50 random questions
11
+ def get_random_questions():
12
+ return random.sample(question_bank, 50)
13
+
14
+ # Store state
15
+ questions = get_random_questions()
16
+
17
+ # Function to evaluate answers
18
+ def evaluate_test(student_name, father_name, roll_no, answers):
19
+ attempted = sum([1 for a in answers if a is not None and a != ""])
20
+ correct = 0
21
+ for q, ans in zip(questions, answers):
22
+ if ans == q["answer"]:
23
+ correct += 1
24
+ percentage = (correct / len(questions)) * 100
25
+
26
+ # Generate PDF report
27
+ pdf = FPDF()
28
+ pdf.add_page()
29
+ pdf.set_font("Arial", size=12)
30
+ pdf.cell(200, 10, txt="Test Report - Class 6 General Knowledge", ln=True, align="C")
31
+ pdf.ln(10)
32
+ pdf.cell(200, 10, txt=f"Student Name: {student_name}", ln=True)
33
+ pdf.cell(200, 10, txt=f"Father's Name: {father_name}", ln=True)
34
+ pdf.cell(200, 10, txt=f"Roll No: {roll_no}", ln=True)
35
+ pdf.ln(10)
36
+ pdf.cell(200, 10, txt=f"Total Questions: {len(questions)}", ln=True)
37
+ pdf.cell(200, 10, txt=f"Attempted: {attempted}", ln=True)
38
+ pdf.cell(200, 10, txt=f"Correct: {correct}", ln=True)
39
+ pdf.cell(200, 10, txt=f"Percentage: {percentage:.2f}%", ln=True)
40
+
41
+ file_name = f"{roll_no}_result.pdf"
42
+ pdf.output(file_name)
43
+
44
+ result_summary = f"""
45
+ Student: {student_name}
46
+ Father's Name: {father_name}
47
+ Roll No: {roll_no}
48
+
49
+ Attempted: {attempted}
50
+ Correct: {correct}
51
+ Percentage: {percentage:.2f}%
52
+ """
53
+
54
+ return result_summary, file_name
55
+
56
+ # Build Gradio interface dynamically
57
+ def build_interface():
58
+ global questions
59
+ questions = get_random_questions()
60
+
61
+ inputs = [
62
+ gr.Textbox(label="Student Name"),
63
+ gr.Textbox(label="Father's Name"),
64
+ gr.Textbox(label="Roll No")
65
+ ]
66
+
67
+ answer_inputs = []
68
+ for i, q in enumerate(questions):
69
+ answer_inputs.append(
70
+ gr.Radio(choices=q["options"], label=f"Q{i+1}: {q['question']}", value=None)
71
+ )
72
+
73
+ output_text = gr.Textbox(label="Result Summary")
74
+ output_file = gr.File(label="Download Result PDF")
75
+
76
+ demo = gr.Interface(
77
+ fn=evaluate_test,
78
+ inputs=inputs + answer_inputs,
79
+ outputs=[output_text, output_file],
80
+ title="Class 6 General Knowledge Test (GFT/O Exam)",
81
+ description="Answer 50 random questions and get your result in PDF."
82
+ )
83
+ return demo
84
+
85
+ demo = build_interface()
86
+
87
+ if __name__ == "__main__":
88
+ demo.launch()