Hidayatmahar commited on
Commit
e4f35d3
·
verified ·
1 Parent(s): d4f25de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -18
app.py CHANGED
@@ -15,13 +15,12 @@ def get_random_questions():
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()
@@ -29,30 +28,34 @@ def evaluate_test(student_name, father_name, roll_no, answers):
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
@@ -67,7 +70,7 @@ def build_interface():
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")
@@ -82,6 +85,7 @@ def build_interface():
82
  )
83
  return demo
84
 
 
85
  demo = build_interface()
86
 
87
  if __name__ == "__main__":
 
15
  questions = get_random_questions()
16
 
17
  # Function to evaluate answers
18
+ def evaluate_test(student_name, father_name, roll_no, *answers):
19
+ answers = list(answers)
20
+
21
+ attempted = sum([1 for a in answers if a])
22
+ correct = sum([1 for q, ans in zip(questions, answers) if ans == q["answer"]])
23
+ percentage = (correct / len(questions)) * 100 if len(questions) > 0 else 0
 
24
 
25
  # Generate PDF report
26
  pdf = FPDF()
 
28
  pdf.set_font("Arial", size=12)
29
  pdf.cell(200, 10, txt="Test Report - Class 6 General Knowledge", ln=True, align="C")
30
  pdf.ln(10)
31
+
32
+ # Safe encoding for special characters
33
+ pdf.cell(200, 10, txt=f"Student Name: {student_name}".encode("latin-1", "replace").decode("latin-1"), ln=True)
34
+ pdf.cell(200, 10, txt=f"Father's Name: {father_name}".encode("latin-1", "replace").decode("latin-1"), ln=True)
35
+ pdf.cell(200, 10, txt=f"Roll No: {roll_no}".encode("latin-1", "replace").decode("latin-1"), ln=True)
36
  pdf.ln(10)
37
+
38
  pdf.cell(200, 10, txt=f"Total Questions: {len(questions)}", ln=True)
39
  pdf.cell(200, 10, txt=f"Attempted: {attempted}", ln=True)
40
  pdf.cell(200, 10, txt=f"Correct: {correct}", ln=True)
41
  pdf.cell(200, 10, txt=f"Percentage: {percentage:.2f}%", ln=True)
42
 
43
  file_name = f"{roll_no}_result.pdf"
44
+ pdf.output(file_name, "F")
45
 
46
  result_summary = f"""
47
+ Student: {student_name}
48
+ Father's Name: {father_name}
49
+ Roll No: {roll_no}
50
+
51
+ Attempted: {attempted}
52
+ Correct: {correct}
53
  Percentage: {percentage:.2f}%
54
  """
55
 
56
  return result_summary, file_name
57
 
58
+
59
  # Build Gradio interface dynamically
60
  def build_interface():
61
  global questions
 
70
  answer_inputs = []
71
  for i, q in enumerate(questions):
72
  answer_inputs.append(
73
+ gr.Radio(choices=q["options"], label=f"Q{i+1}: {q['question']}")
74
  )
75
 
76
  output_text = gr.Textbox(label="Result Summary")
 
85
  )
86
  return demo
87
 
88
+
89
  demo = build_interface()
90
 
91
  if __name__ == "__main__":