Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,10 +9,11 @@ Select an exam from the dropdown menu below and start testing your skills. You c
|
|
9 |
|
10 |
# --- FUNCTION DEFINITIONS ---
|
11 |
|
12 |
-
def start_exam(
|
13 |
-
"""Starts the exam by selecting questions, setting up UI."""
|
14 |
-
|
15 |
-
|
|
|
16 |
|
17 |
question, options, audio_path = display_question(0, audio_enabled, selected_questions_state)
|
18 |
return (
|
@@ -36,19 +37,6 @@ def display_question(index, audio_enabled, selected_questions_state):
|
|
36 |
audio_path = text_to_speech(question_text_ + " " + " ".join(choices_options)) if audio_enabled else None
|
37 |
return question_text, choices_options, audio_path
|
38 |
|
39 |
-
def show_explanation(index, selected_questions_state):
|
40 |
-
"""Shows the explanation for the current question and hides previous results."""
|
41 |
-
selected_questions = selected_questions_state
|
42 |
-
if 0 <= index < len(selected_questions):
|
43 |
-
explanation = selected_questions[index].get('explanation', 'No explanation available for this question.')
|
44 |
-
return (
|
45 |
-
f"**Explanation:** {explanation}",
|
46 |
-
gr.update(visible=True), # Show explanation_text
|
47 |
-
gr.update(visible=True) # Show result_text
|
48 |
-
)
|
49 |
-
else:
|
50 |
-
return "No explanation available for this question.", gr.update(visible=False), gr.update(visible=False)
|
51 |
-
|
52 |
def check_answer(index, answer, selected_questions_state):
|
53 |
"""Checks the given answer against the correct answer."""
|
54 |
selected_questions = selected_questions_state
|
@@ -65,22 +53,19 @@ def update_question(index, audio_enabled, selected_questions_state):
|
|
65 |
|
66 |
def handle_answer(index, answer, audio_enabled, current_audio, selected_questions_state):
|
67 |
"""Handles answer submission, provides feedback, and generates audio."""
|
68 |
-
# Handle the case when no answer is selected
|
69 |
if answer is None:
|
70 |
return "Please select an option before submitting.", None, None
|
71 |
-
|
72 |
-
# Stop the current question audio before playing the answer audio
|
73 |
stop_audio = True if current_audio else False
|
74 |
result = check_answer(index, answer, selected_questions_state)
|
75 |
answer_audio_path = text_to_speech(result) if audio_enabled else None
|
76 |
return result, answer_audio_path, stop_audio
|
77 |
|
78 |
-
|
79 |
def handle_next(index, audio_enabled, selected_questions_state):
|
80 |
"""Moves to the next question and updates the UI."""
|
81 |
new_index = min(index + 1, len(selected_questions_state) - 1)
|
82 |
question, options, new_index, audio_path = update_question(new_index, audio_enabled, selected_questions_state)
|
83 |
-
return question, options, new_index, "", audio_path, gr.update(visible=False)
|
84 |
|
85 |
def handle_previous(index, audio_enabled, selected_questions_state):
|
86 |
"""Moves to the previous question and updates the UI."""
|
@@ -91,15 +76,24 @@ def handle_previous(index, audio_enabled, selected_questions_state):
|
|
91 |
def return_home():
|
92 |
"""Returns to the home screen."""
|
93 |
return (
|
94 |
-
# Show start screen elements
|
95 |
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
|
96 |
-
gr.update(visible=True),
|
97 |
-
# Hide quiz elements
|
98 |
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
|
99 |
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), "", "", gr.update(visible=False), gr.update(visible=False),
|
100 |
-
gr.update(visible=False)
|
101 |
)
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
with gr.Blocks() as demo:
|
104 |
# Home page elements
|
105 |
title = gr.Markdown(value="**AWS Exam Simulator (Quiz)**")
|
@@ -110,8 +104,9 @@ with gr.Blocks() as demo:
|
|
110 |
|
111 |
# Quiz elements (initially hidden)
|
112 |
question_state = gr.State(0)
|
113 |
-
current_audio_state = gr.State(None)
|
114 |
-
selected_questions_state = gr.State([]) #
|
|
|
115 |
question_text = gr.Markdown(visible=False, elem_id="question-text")
|
116 |
choices = gr.Radio(visible=False, label="Options")
|
117 |
result_text = gr.Markdown(visible=True)
|
@@ -150,16 +145,19 @@ with gr.Blocks() as demo:
|
|
150 |
with gr.Row():
|
151 |
gr.Column([home_button])
|
152 |
|
|
|
|
|
|
|
153 |
# Connect the start button to start the exam
|
154 |
start_button.click(
|
155 |
fn=start_exam,
|
156 |
-
inputs=[
|
157 |
outputs=[
|
158 |
title, description, exam_selector, start_button,
|
159 |
-
audio_checkbox,
|
160 |
question_text, question_text, choices, answer_button,
|
161 |
next_button, prev_button, home_button, question_state, result_text, question_audio,
|
162 |
-
explain_button, selected_questions_state, current_audio_state
|
163 |
]
|
164 |
)
|
165 |
|
@@ -172,10 +170,9 @@ with gr.Blocks() as demo:
|
|
172 |
|
173 |
home_button.click(fn=return_home, inputs=None, outputs=[
|
174 |
title, description, exam_selector, start_button,
|
175 |
-
audio_checkbox,
|
176 |
question_text, question_text, choices, answer_button,
|
177 |
next_button, prev_button, home_button, question_state, result_text, explanation_text, explain_button
|
178 |
])
|
179 |
|
180 |
-
|
181 |
demo.queue(max_size=100).launch(debug=True, max_threads=75)
|
|
|
9 |
|
10 |
# --- FUNCTION DEFINITIONS ---
|
11 |
|
12 |
+
def start_exam(audio_enabled, selected_exam_state, selected_questions_state):
|
13 |
+
"""Starts the exam by selecting questions based on the selected exam, setting up UI."""
|
14 |
+
selected_exam = selected_exam_state # Get the user-selected exam
|
15 |
+
selected_questions = select_exam_vce(selected_exam) # Fetch new questions for the selected exam
|
16 |
+
selected_questions_state = selected_questions # Store the selected questions in the session state
|
17 |
|
18 |
question, options, audio_path = display_question(0, audio_enabled, selected_questions_state)
|
19 |
return (
|
|
|
37 |
audio_path = text_to_speech(question_text_ + " " + " ".join(choices_options)) if audio_enabled else None
|
38 |
return question_text, choices_options, audio_path
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
def check_answer(index, answer, selected_questions_state):
|
41 |
"""Checks the given answer against the correct answer."""
|
42 |
selected_questions = selected_questions_state
|
|
|
53 |
|
54 |
def handle_answer(index, answer, audio_enabled, current_audio, selected_questions_state):
|
55 |
"""Handles answer submission, provides feedback, and generates audio."""
|
|
|
56 |
if answer is None:
|
57 |
return "Please select an option before submitting.", None, None
|
58 |
+
|
|
|
59 |
stop_audio = True if current_audio else False
|
60 |
result = check_answer(index, answer, selected_questions_state)
|
61 |
answer_audio_path = text_to_speech(result) if audio_enabled else None
|
62 |
return result, answer_audio_path, stop_audio
|
63 |
|
|
|
64 |
def handle_next(index, audio_enabled, selected_questions_state):
|
65 |
"""Moves to the next question and updates the UI."""
|
66 |
new_index = min(index + 1, len(selected_questions_state) - 1)
|
67 |
question, options, new_index, audio_path = update_question(new_index, audio_enabled, selected_questions_state)
|
68 |
+
return question, options, new_index, "", audio_path, gr.update(visible=False)
|
69 |
|
70 |
def handle_previous(index, audio_enabled, selected_questions_state):
|
71 |
"""Moves to the previous question and updates the UI."""
|
|
|
76 |
def return_home():
|
77 |
"""Returns to the home screen."""
|
78 |
return (
|
|
|
79 |
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
|
80 |
+
gr.update(visible=True), # Show the audio_checkbox
|
|
|
81 |
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
|
82 |
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), "", "", gr.update(visible=False), gr.update(visible=False),
|
83 |
+
gr.update(visible=False) # Hide explain button
|
84 |
)
|
85 |
+
def show_explanation(index, selected_questions_state):
|
86 |
+
"""Shows the explanation for the current question and hides previous results."""
|
87 |
+
selected_questions = selected_questions_state
|
88 |
+
if 0 <= index < len(selected_questions):
|
89 |
+
explanation = selected_questions[index].get('explanation', 'No explanation available for this question.')
|
90 |
+
return (
|
91 |
+
f"**Explanation:** {explanation}",
|
92 |
+
gr.update(visible=True), # Show explanation_text
|
93 |
+
gr.update(visible=True) # Show result_text
|
94 |
+
)
|
95 |
+
else:
|
96 |
+
return "No explanation available for this question.", gr.update(visible=False), gr.update(visible=False)
|
97 |
with gr.Blocks() as demo:
|
98 |
# Home page elements
|
99 |
title = gr.Markdown(value="**AWS Exam Simulator (Quiz)**")
|
|
|
104 |
|
105 |
# Quiz elements (initially hidden)
|
106 |
question_state = gr.State(0)
|
107 |
+
current_audio_state = gr.State(None)
|
108 |
+
selected_questions_state = gr.State([]) # Store the selected questions for each user session
|
109 |
+
selected_exam_state = gr.State("") # State to store the selected exam for each user session
|
110 |
question_text = gr.Markdown(visible=False, elem_id="question-text")
|
111 |
choices = gr.Radio(visible=False, label="Options")
|
112 |
result_text = gr.Markdown(visible=True)
|
|
|
145 |
with gr.Row():
|
146 |
gr.Column([home_button])
|
147 |
|
148 |
+
# Store the selected exam when chosen
|
149 |
+
exam_selector.change(fn=lambda exam: exam, inputs=[exam_selector], outputs=[selected_exam_state])
|
150 |
+
|
151 |
# Connect the start button to start the exam
|
152 |
start_button.click(
|
153 |
fn=start_exam,
|
154 |
+
inputs=[audio_checkbox, selected_exam_state, selected_questions_state],
|
155 |
outputs=[
|
156 |
title, description, exam_selector, start_button,
|
157 |
+
audio_checkbox,
|
158 |
question_text, question_text, choices, answer_button,
|
159 |
next_button, prev_button, home_button, question_state, result_text, question_audio,
|
160 |
+
explain_button, selected_questions_state, current_audio_state
|
161 |
]
|
162 |
)
|
163 |
|
|
|
170 |
|
171 |
home_button.click(fn=return_home, inputs=None, outputs=[
|
172 |
title, description, exam_selector, start_button,
|
173 |
+
audio_checkbox,
|
174 |
question_text, question_text, choices, answer_button,
|
175 |
next_button, prev_button, home_button, question_state, result_text, explanation_text, explain_button
|
176 |
])
|
177 |
|
|
|
178 |
demo.queue(max_size=100).launch(debug=True, max_threads=75)
|