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4 |
+
blame
|
5 |
+
edit
|
6 |
+
delete
|
7 |
+
|
8 |
+
11 kB
|
9 |
+
# app.py
|
10 |
+
import gradio as gr
|
11 |
+
from tool2 import * # Ensure you are using the updated tool2.py
|
12 |
+
# from backend1 import * # Not needed
|
13 |
+
|
14 |
+
# Global variable to store the currently selected set of exam questions
|
15 |
+
selected_questions = []
|
16 |
+
|
17 |
+
description_str = """Developed by Ruslan Magana, this interactive quiz platform is designed to help you prepare and assess your knowledge in a variety of exams.
|
18 |
+
For more information about the developer, please visit [ruslanmv.com](https://ruslanmv.com/).
|
19 |
+
**Get Started with Your Quiz**
|
20 |
+
Select an exam from the dropdown menu below and start testing your skills. You can also choose to enable audio feedback to enhance your learning experience. Simply toggle the "Enable Audio" checkbox to turn it on or off."""
|
21 |
+
|
22 |
+
|
23 |
+
# --- FUNCTION DEFINITIONS ---
|
24 |
+
def start_exam(exam_choice, start_question, audio_enabled):
|
25 |
+
"""Starts the exam by selecting questions, setting up UI."""
|
26 |
+
global selected_questions
|
27 |
+
selected_questions = select_exam_vce(exam_choice)
|
28 |
+
|
29 |
+
if not selected_questions: # Handle case where no questions are loaded for selected exam
|
30 |
+
return (
|
31 |
+
gr.update(visible=True), # Show title
|
32 |
+
gr.update(value="**Error: No Questions Found for this Exam**", visible=True), # Update description to error message
|
33 |
+
gr.update(visible=True), # Show exam_selector
|
34 |
+
gr.update(visible=True), # Show start_button
|
35 |
+
gr.update(visible=True), # Show the audio_checkbox
|
36 |
+
gr.update(visible=True), # Show start_question_slider
|
37 |
+
# Hide quiz elements
|
38 |
+
gr.update(visible=False), # Hide question_text
|
39 |
+
"", # Question to display
|
40 |
+
gr.update(choices=[], visible=False), # Hide Radio choices
|
41 |
+
gr.update(visible=False), # Hide answer_button
|
42 |
+
gr.update(visible=False), # Hide next_button
|
43 |
+
gr.update(visible=False), # Hide prev_button
|
44 |
+
gr.update(visible=False), # Hide home_button
|
45 |
+
0, "", # Update the question state
|
46 |
+
None, # Provide the audio_path
|
47 |
+
gr.update(visible=False), # Hide explain_button
|
48 |
+
gr.update(visible=False),
|
49 |
+
None # None for audio stop
|
50 |
+
)
|
51 |
+
|
52 |
+
if start_question >= len(selected_questions):
|
53 |
+
start_question = 0 # Default to the first question if the input exceeds available questions
|
54 |
+
elif start_question < 0:
|
55 |
+
start_question = 0
|
56 |
+
|
57 |
+
question, options, audio_path = display_question(start_question, audio_enabled, selected_questions)
|
58 |
+
|
59 |
+
return (
|
60 |
+
# Hide start screen elements
|
61 |
+
gr.update(visible=False), # Hide title
|
62 |
+
gr.update(visible=False), # Hide description
|
63 |
+
gr.update(visible=False), # Hide exam_selector
|
64 |
+
gr.update(visible=False), # Hide start_button
|
65 |
+
gr.update(visible=False), # Hide the audio_checkbox
|
66 |
+
gr.update(visible=False), # Hide start_question_slider
|
67 |
+
# Show quiz elements
|
68 |
+
gr.update(visible=True), # Show question_text
|
69 |
+
question, # Question to display
|
70 |
+
gr.update(choices=options, visible=True), # Update Radio choices and make visible
|
71 |
+
gr.update(visible=True), # Show answer_button
|
72 |
+
gr.update(visible=True), # Show next_button
|
73 |
+
gr.update(visible=True), # Show prev_button
|
74 |
+
gr.update(visible=True), # Show home_button
|
75 |
+
start_question, "", # Update the question state
|
76 |
+
audio_path, # Provide the audio_path
|
77 |
+
gr.update(visible=True), # Show explain_button
|
78 |
+
gr.update(visible=True),
|
79 |
+
None # None for the audio stop signal
|
80 |
+
)
|
81 |
+
|
82 |
+
|
83 |
+
def display_question_ui(index, audio_enabled):
|
84 |
+
"""Displays a question with options and generates audio (if enabled) and updates UI elements."""
|
85 |
+
question, options, audio_path = display_question(index, audio_enabled, selected_questions)
|
86 |
+
return question, gr.update(choices=options), audio_path
|
87 |
+
|
88 |
+
|
89 |
+
def show_explanation(index):
|
90 |
+
"""Shows the explanation for the current question, handling potential missing explanations."""
|
91 |
+
explanation, correct_answer = get_explanation_and_answer(index, selected_questions)
|
92 |
+
if explanation == "No explanation available.":
|
93 |
+
return (
|
94 |
+
f"**Explanation:** {explanation}",
|
95 |
+
gr.update(visible=True), # Show explanation_text
|
96 |
+
gr.update(visible=False) # dont show result when clicking explain
|
97 |
+
)
|
98 |
+
else:
|
99 |
+
return (
|
100 |
+
f"**Explanation:** {explanation}",
|
101 |
+
gr.update(visible=True), # Show explanation_text
|
102 |
+
gr.update(visible=False) # dont show result when clicking explain
|
103 |
+
)
|
104 |
+
|
105 |
+
def check_answer(index, answer):
|
106 |
+
"""Checks the given answer, always returns the correct answer."""
|
107 |
+
explanation, correct_answer = get_explanation_and_answer(index, selected_questions)
|
108 |
+
if answer == correct_answer:
|
109 |
+
return f"Correct! The answer is: {correct_answer}"
|
110 |
+
else:
|
111 |
+
return f"Incorrect. The correct answer is: {correct_answer}"
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
+
def update_question(index, audio_enabled):
|
116 |
+
"""Updates the displayed question when the index changes."""
|
117 |
+
return display_question_ui(index, audio_enabled) # Use display_question_ui for UI updates
|
118 |
+
|
119 |
+
|
120 |
+
def handle_answer(index, answer, audio_enabled, current_audio):
|
121 |
+
"""Handles answer submission, provides feedback, and generates audio."""
|
122 |
+
if answer is None:
|
123 |
+
return "Please select an option before submitting.", None, None
|
124 |
+
result = check_answer(index, answer) # Always show correct answer
|
125 |
+
answer_audio_path = text_to_speech(result) if audio_enabled else None
|
126 |
+
return result, answer_audio_path, None
|
127 |
+
|
128 |
+
|
129 |
+
def handle_next(index, audio_enabled):
|
130 |
+
"""Moves to the next question and updates the UI."""
|
131 |
+
new_index = min(index + 1, len(selected_questions) - 1)
|
132 |
+
question, options, audio_path = update_question(new_index, audio_enabled)
|
133 |
+
return question, options, new_index, "", audio_path, gr.update(visible=False), gr.update(value="") # Hide explanation and clear result
|
134 |
+
|
135 |
+
|
136 |
+
def handle_previous(index, audio_enabled):
|
137 |
+
"""Moves to the previous question and updates the UI."""
|
138 |
+
new_index = max(index - 1, 0)
|
139 |
+
question, options, audio_path = update_question(new_index, audio_enabled)
|
140 |
+
return question, options, new_index, "", audio_path, gr.update(visible=False), gr.update(value="") # Hide explanation and clear result
|
141 |
+
|
142 |
+
|
143 |
+
def return_home():
|
144 |
+
"""Returns to the home screen."""
|
145 |
+
return (
|
146 |
+
# Show start screen elements
|
147 |
+
gr.update(visible=True), gr.update(value="**AWS Exam Simulator (Quiz)**", visible=True), gr.update(visible=True), gr.update(visible=True),
|
148 |
+
gr.update(visible=True), # Show the audio_checkbox
|
149 |
+
gr.update(visible=True), # Show start_question_slider
|
150 |
+
# Hide quiz elements
|
151 |
+
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
|
152 |
+
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), 0, "", gr.update(visible=False), gr.update(visible=False),
|
153 |
+
gr.update(visible=False), gr.update(value="") # Hide explain button and clear result
|
154 |
+
)
|
155 |
+
|
156 |
+
|
157 |
+
with gr.Blocks() as demo:
|
158 |
+
# Home page elements
|
159 |
+
title = gr.Markdown(value="**AWS Exam Simulator (Quiz)**")
|
160 |
+
description = gr.Markdown(value=description_str)
|
161 |
+
exam_selector = gr.Dropdown(label="Select an exam", choices=exams, value=None)
|
162 |
+
audio_checkbox = gr.Checkbox(label="Enable Audio", value=True, visible=False)
|
163 |
+
start_question_slider = gr.Slider(minimum=1, maximum=50, step=1, label="Select starting question", value=1, visible=False)
|
164 |
+
start_button = gr.Button("Start Exam", visible=False)
|
165 |
+
|
166 |
+
# Quiz elements (initially hidden)
|
167 |
+
question_state = gr.State(0)
|
168 |
+
current_audio_state = gr.State(None)
|
169 |
+
question_text = gr.Markdown(visible=False, elem_id="question-text")
|
170 |
+
choices = gr.Radio(visible=False, label="Options")
|
171 |
+
result_text = gr.Markdown(visible=False)
|
172 |
+
explanation_text = gr.Markdown(visible=False)
|
173 |
+
answer_button = gr.Button("Submit Answer", visible=False)
|
174 |
+
next_button = gr.Button("Next Question", visible=False)
|
175 |
+
prev_button = gr.Button("Previous Question", visible=False)
|
176 |
+
home_button = gr.Button("Return to Home", visible=False)
|
177 |
+
explain_button = gr.Button("Explain", visible=False)
|
178 |
+
question_audio = gr.Audio(visible=False, label="Question Audio", autoplay=True)
|
179 |
+
answer_audio = gr.Audio(visible=False, label="Answer Audio", autoplay=True)
|
180 |
+
|
181 |
+
# Layout
|
182 |
+
with gr.Row():
|
183 |
+
gr.Column([title])
|
184 |
+
with gr.Row():
|
185 |
+
gr.Column([description])
|
186 |
+
with gr.Row():
|
187 |
+
gr.Column([exam_selector])
|
188 |
+
with gr.Row():
|
189 |
+
gr.Column([audio_checkbox, start_question_slider])
|
190 |
+
with gr.Row():
|
191 |
+
gr.Column([start_button])
|
192 |
+
with gr.Row():
|
193 |
+
gr.Column([question_text, question_audio])
|
194 |
+
with gr.Row():
|
195 |
+
gr.Column([choices])
|
196 |
+
with gr.Row():
|
197 |
+
gr.Column([result_text, explanation_text, answer_audio])
|
198 |
+
with gr.Row():
|
199 |
+
gr.Column([prev_button], scale=1)
|
200 |
+
gr.Column([], scale=8)
|
201 |
+
gr.Column([next_button], scale=1)
|
202 |
+
with gr.Row():
|
203 |
+
gr.Column([answer_button, explain_button])
|
204 |
+
with gr.Row():
|
205 |
+
gr.Column([home_button])
|
206 |
+
|
207 |
+
# Show settings
|
208 |
+
def show_settings(exam_choice):
|
209 |
+
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
210 |
+
|
211 |
+
exam_selector.change(fn=show_settings, inputs=[exam_selector], outputs=[audio_checkbox, start_question_slider, start_button])
|
212 |
+
start_button.click(
|
213 |
+
fn=start_exam,
|
214 |
+
inputs=[exam_selector, start_question_slider, audio_checkbox],
|
215 |
+
outputs=[
|
216 |
+
title, description, exam_selector, start_button,
|
217 |
+
audio_checkbox, start_question_slider,
|
218 |
+
question_text, question_text, choices, answer_button,
|
219 |
+
next_button, prev_button, home_button, question_state, result_text, question_audio,
|
220 |
+
explain_button, explanation_text, current_audio_state
|
221 |
+
]
|
222 |
+
)
|
223 |
+
answer_button.click(fn=handle_answer, inputs=[question_state, choices, audio_checkbox, current_audio_state], outputs=[result_text, answer_audio, current_audio_state])
|
224 |
+
next_button.click(fn=handle_next, inputs=[question_state, audio_checkbox], outputs=[question_text, choices, question_state, result_text, question_audio, explanation_text, result_text])
|
225 |
+
prev_button.click(fn=handle_previous, inputs=[question_state, audio_checkbox], outputs=[question_text, choices, question_state, result_text, question_audio, explanation_text, result_text])
|
226 |
+
explain_button.click(fn=show_explanation, inputs=[question_state], outputs=[explanation_text, result_text, explanation_text]) #result text set to not visible
|
227 |
+
home_button.click(fn=return_home, inputs=None, outputs=[
|
228 |
+
title, description, exam_selector, start_button,
|
229 |
+
audio_checkbox, start_question_slider,
|
230 |
+
question_text, question_text, choices, answer_button,
|
231 |
+
next_button, prev_button, home_button, question_state, result_text, explanation_text, explain_button, result_text
|
232 |
+
])
|
233 |
+
|
234 |
+
demo.launch()
|