amitagh commited on
Commit
205ac10
·
verified ·
1 Parent(s): 11fe53e
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+ from gradio_client import Client
4
+
5
+ MAX_ANS_COMP = 30
6
+
7
+ hf_key = os.environ['HF_API_KEY']
8
+ client_url = os.environ['BK_URL']
9
+
10
+ client = Client(client_url, hf_token=hf_key)
11
+ def gen_quiz_ques_local(grade, num_questions):
12
+ res = client.predict(
13
+ grade=grade, num_questions=num_questions,
14
+ api_name="/generate_quiz"
15
+ )
16
+ return res
17
+
18
+
19
+ def generate_quiz(grade, num_questions):
20
+ if not grade or not num_questions:
21
+ return [[], "Please select both grade and number of questions before generating the quiz."] + [gr.update(visible=False) for _ in range(5)]
22
+
23
+ grade = int(grade)
24
+ num_questions = int(num_questions)
25
+
26
+ quiz = gen_quiz_ques_local(grade, num_questions)
27
+ if quiz == None:
28
+ return [[], f"Error please generate again."] + [gr.update(visible=False) for _ in range(MAX_ANS_COMP)]
29
+ quiz_html = ""
30
+
31
+ radio_updates = [gr.update(visible=True, choices=q['options'], label=f"Question {i+1}: {q['question']}") for i, q in enumerate(quiz)]
32
+ radio_updates += [gr.update(visible=False) for _ in range(MAX_ANS_COMP - len(quiz))]
33
+
34
+ return [quiz, quiz_html] + radio_updates
35
+
36
+ def check_answers(quiz, *answers):
37
+ if not quiz:
38
+ return "Please generate a quiz first."
39
+
40
+ if any(ans is None for ans in answers[:len(quiz)]):
41
+ return "Please answer all questions before submitting."
42
+
43
+ results = []
44
+ correct_count = 0
45
+ for i, (q, ans) in enumerate(zip(quiz, answers), 1):
46
+ if ans == q['answer']:
47
+ results.append(f"{i}. ✅ Correct")
48
+ correct_count += 1
49
+ else:
50
+ results.append(f"{i}. ❌ Incorrect. You selected: {ans}. The correct answer is: {q['answer']}")
51
+
52
+ score = f"Score: {correct_count} out of {len(quiz)} correct"
53
+ per_cor = (correct_count / len(quiz)) * 100
54
+ if per_cor <= 20:
55
+ score = "Don't worry, everyone starts somewhere! Keep learning and trying, and you'll get better each time. You've got this!\n" + score
56
+ elif per_cor <= 40:
57
+ score = "Great effort! You're on the right track. Keep up the good work and continue to challenge yourself. You can do it!\n" + score
58
+ elif per_cor <= 60:
59
+ score = "Well done! You've got a solid understanding. Keep pushing forward and aiming higher. You're doing awesome!\n" + score
60
+ elif per_cor <= 80:
61
+ score = "Fantastic job! You're really getting the hang of this. Keep up the excellent work and keep striving for the top!\n" + score
62
+ elif per_cor <= 100:
63
+ score = "Amazing! You've nailed it! Your hard work and dedication have paid off. Keep up the incredible work!\n" + score
64
+
65
+ return f"{score}<br><br>" + "<br>".join(results)
66
+
67
+ def clear_quiz():
68
+ return [[], ""] + [gr.update(visible=False, value=None) for _ in range(MAX_ANS_COMP)] + [""]
69
+
70
+ with gr.Blocks() as app:
71
+ gr.Markdown("# QuizWhiz Quiz Generator")
72
+ with gr.Row():
73
+ grade = gr.Dropdown(choices=[6], label="Grade", value=6)
74
+ num_questions = gr.Dropdown(choices=[5, 10, 15, 20], label="Number of Questions", value=5)
75
+ #num_questions = gr.Slider(minimum=1, maximum=MAX_ANS_COMP, step=1, value=10, label="Number of Questions")
76
+ generate_btn = gr.Button("Generate Quiz")
77
+
78
+ quiz_output = gr.HTML()
79
+ quiz_state = gr.State([])
80
+ answer_components = [gr.Radio(visible=False, label=f"Question {i+1}") for i in range(MAX_ANS_COMP)]
81
+
82
+ submit_btn = gr.Button("Submit Answers")
83
+ results_output = gr.HTML()
84
+ clear_btn = gr.Button("Clear Quiz")
85
+
86
+
87
+ generate_btn.click(
88
+ generate_quiz,
89
+ inputs=[grade, num_questions],
90
+ outputs=[quiz_state, quiz_output] + answer_components
91
+ )
92
+
93
+ submit_btn.click(
94
+ check_answers,
95
+ inputs=[quiz_state] + answer_components,
96
+ outputs=results_output
97
+ )
98
+
99
+ clear_btn.click(
100
+ clear_quiz,
101
+ outputs=[quiz_state, quiz_output] + answer_components + [results_output]
102
+ )
103
+
104
+ app.launch(debug=True)