ruslanmv commited on
Commit
1f3ea08
·
verified ·
1 Parent(s): 9b9e882

Create app2.py

Browse files
Files changed (1) hide show
  1. app2.py +207 -0
app2.py ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from tool import * # Assuming this module contains your exam data and text-to-speech functionality
3
+ from backend1 import *
4
+
5
+ # Global variable to store the currently selected set of exam questions
6
+ selected_questions = []
7
+
8
+ 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.
9
+ For more information about the developer, please visit [ruslanmv.com](https://ruslanmv.com/).
10
+ **Get Started with Your Quiz**
11
+ 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."""
12
+
13
+ # --- FUNCTION DEFINITIONS ---
14
+
15
+ def start_exam(exam_choice, start_question, audio_enabled):
16
+ """Starts the exam by selecting questions, setting up UI."""
17
+ global selected_questions
18
+ selected_questions = select_exam_vce(exam_choice)
19
+
20
+ if start_question >= len(selected_questions):
21
+ start_question = 0 # Default to the first question if the input exceeds available questions
22
+
23
+ question, options, audio_path = display_question(start_question, audio_enabled)
24
+
25
+ return (
26
+ # Hide start screen elements
27
+ gr.update(visible=False), # Hide title
28
+ gr.update(visible=False), # Hide description
29
+ gr.update(visible=False), # Hide exam_selector
30
+ gr.update(visible=False), # Hide start_button
31
+ gr.update(visible=False), # Hide the audio_checkbox
32
+ gr.update(visible=False), # Hide start_question_slider
33
+ # Show quiz elements
34
+ gr.update(visible=True), # Show question_text
35
+ question, # Question to display
36
+ gr.update(choices=options, visible=True), # Update Radio choices and make visible
37
+ gr.update(visible=True), # Show answer_button
38
+ gr.update(visible=True),# Show next_button
39
+ gr.update(visible=True), # Show prev_button
40
+ gr.update(visible=True), # Show home_button
41
+ start_question, "", # Update the question state
42
+ audio_path, # Provide the audio_path
43
+ gr.update(visible=True), # Show explain_button
44
+ gr.update(visible=True),
45
+ None # None for the audio stop signal
46
+ )
47
+
48
+ def display_question(index, audio_enabled):
49
+ """Displays a question with options and generates audio (if enabled)."""
50
+ if index < 0 or index >= len(selected_questions):
51
+ return "No more questions.", [], None
52
+ question_text_ = selected_questions[index].get('question', 'No question text available.')
53
+ question_text = f"**Question {index }:** {question_text_}"
54
+ choices_options = selected_questions[index].get('options', [])
55
+ audio_path = text_to_speech(question_text_ + " " + " ".join(choices_options)) if audio_enabled else None
56
+ return question_text, choices_options, audio_path
57
+
58
+ def show_explanation(index):
59
+ """Shows the explanation for the current question and hides previous results."""
60
+ if 0 <= index < len(selected_questions):
61
+ explanation = selected_questions[index].get('explanation', 'No explanation available for this question.')
62
+ return (
63
+ f"**Explanation:** {explanation}",
64
+ gr.update(visible=True), # Show explanation_text
65
+ gr.update(visible=True) # Show result_text
66
+ )
67
+ else:
68
+ return "No explanation available for this question.", gr.update(visible=False), gr.update(visible=False)
69
+
70
+ def check_answer(index, answer):
71
+ """Checks the given answer against the correct answer."""
72
+ correct_answer = selected_questions[index].get('correct', 'No correct answer provided.')
73
+ if answer == correct_answer:
74
+ return f"Correct! The answer is: {correct_answer}"
75
+ else:
76
+ return f"Incorrect. The correct answer is: {correct_answer}"
77
+
78
+ def update_question(index, audio_enabled):
79
+ """Updates the displayed question when the index changes."""
80
+ question, options, audio_path = display_question(index, audio_enabled)
81
+ return question, gr.update(choices=options), index, audio_path
82
+
83
+ def handle_answer(index, answer, audio_enabled, current_audio):
84
+ """Handles answer submission, provides feedback, and generates audio."""
85
+ # Handle the case when no answer is selected
86
+ if answer is None:
87
+ return "Please select an option before submitting.", None, None
88
+
89
+ # Stop the current question audio before playing the answer audio
90
+ stop_audio = True if current_audio else False
91
+ result = check_answer(index, answer)
92
+ answer_audio_path = text_to_speech(result) if audio_enabled else None
93
+ return result, answer_audio_path, stop_audio
94
+
95
+
96
+ def handle_next(index, audio_enabled):
97
+ """Moves to the next question and updates the UI."""
98
+ new_index = min(index + 1, len(selected_questions) - 1)
99
+ question, options, new_index, audio_path = update_question(new_index, audio_enabled)
100
+ return question, options, new_index, "", audio_path, gr.update(visible=False) # Hide explanation
101
+
102
+ def handle_previous(index, audio_enabled):
103
+ """Moves to the previous question and updates the UI."""
104
+ new_index = max(index - 1, 0)
105
+ question, options, new_index, audio_path = update_question(new_index, audio_enabled)
106
+ return question, options, new_index, "", audio_path, gr.update(visible=False) # Hide explanation
107
+
108
+ def return_home():
109
+ """Returns to the home screen."""
110
+ return (
111
+ # Show start screen elements
112
+ gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
113
+ gr.update(visible=True), # Show the audio_checkbox
114
+ gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
115
+ gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), "", "", gr.update(visible=False), gr.update(visible=False),
116
+ gr.update(visible=False) # Hide explain button
117
+ )
118
+
119
+ with gr.Blocks() as demo:
120
+ # Home page elements
121
+ title = gr.Markdown(value="**AWS Exam Simulator (Quiz)**")
122
+ description = gr.Markdown(value=description_str)
123
+ exam_selector = gr.Dropdown(label="Select an exam", choices=exams, value=None)
124
+ audio_checkbox = gr.Checkbox(label="Enable Audio", value=True, visible=False)
125
+ start_question_slider = gr.Slider(minimum=0, maximum=50, step=1, label="Select starting question", visible=False) # Slider for selecting the starting question
126
+ start_button = gr.Button("Start Exam", visible=False)
127
+
128
+ # Quiz elements (initially hidden)
129
+ question_state = gr.State(0)
130
+ current_audio_state = gr.State(None) # State to track the current audio playing
131
+ question_text = gr.Markdown(visible=False, elem_id="question-text")
132
+ choices = gr.Radio(visible=False, label="Options")
133
+ result_text = gr.Markdown(visible=True)
134
+ explanation_text = gr.Markdown(visible=False)
135
+ answer_button = gr.Button("Submit Answer", visible=False)
136
+ next_button = gr.Button("Next Question", visible=False)
137
+ prev_button = gr.Button("Previous Question", visible=False)
138
+ home_button = gr.Button("Return to Home", visible=False)
139
+ explain_button = gr.Button("Explain", visible=False)
140
+ question_audio = gr.Audio(visible=False, label="Question Audio", autoplay=True)
141
+ answer_audio = gr.Audio(visible=False, label="Answer Audio", autoplay=True)
142
+
143
+ # Layout for the home page
144
+ with gr.Row():
145
+ gr.Column([title])
146
+ with gr.Row():
147
+ gr.Column([description])
148
+ with gr.Row():
149
+ gr.Column([exam_selector])
150
+ with gr.Row():
151
+ gr.Column([audio_checkbox, start_question_slider])
152
+ with gr.Row():
153
+ gr.Column([start_button])
154
+
155
+ # Layout for the quiz
156
+ with gr.Row():
157
+ gr.Column([question_text, question_audio])
158
+ with gr.Row():
159
+ gr.Column([choices])
160
+ with gr.Row():
161
+ gr.Column([result_text, explanation_text, answer_audio])
162
+ with gr.Row():
163
+ gr.Column([prev_button], scale=1)
164
+ gr.Column([], scale=8)
165
+ gr.Column([next_button], scale=1)
166
+ with gr.Row():
167
+ gr.Column([answer_button, explain_button])
168
+ with gr.Row():
169
+ gr.Column([home_button])
170
+
171
+ # Show settings after exam selection
172
+ def show_settings(exam_choice):
173
+ return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
174
+
175
+ # Connect exam selection to display settings section
176
+ exam_selector.change(fn=show_settings, inputs=[exam_selector], outputs=[audio_checkbox, start_question_slider, start_button])
177
+
178
+ # Connect the start button to start the exam
179
+ start_button.click(
180
+ fn=start_exam,
181
+ inputs=[exam_selector, start_question_slider, audio_checkbox],
182
+ outputs=[
183
+ title, description, exam_selector, start_button,
184
+ audio_checkbox, # Ensure the checkbox visibility is updated
185
+ start_question_slider, # Ensure the slider is hidden
186
+ question_text, question_text, choices, answer_button,
187
+ next_button, prev_button, home_button, question_state, result_text, question_audio,
188
+ explain_button, result_text, current_audio_state # Add current_audio_state to the outputs
189
+ ]
190
+ )
191
+
192
+ # Connect the quiz buttons to their functions
193
+ answer_button.click(fn=handle_answer, inputs=[question_state, choices, audio_checkbox, current_audio_state], outputs=[result_text, answer_audio, current_audio_state])
194
+ next_button.click(fn=handle_next, inputs=[question_state, audio_checkbox], outputs=[question_text, choices, question_state, result_text, question_audio, explanation_text])
195
+ prev_button.click(fn=handle_previous, inputs=[question_state, audio_checkbox], outputs=[question_text, choices, question_state, result_text, question_audio, explanation_text])
196
+
197
+ explain_button.click(fn=show_explanation, inputs=[question_state], outputs=[explanation_text, result_text, explanation_text])
198
+
199
+ home_button.click(fn=return_home, inputs=None, outputs=[
200
+ title, description, exam_selector, start_button,
201
+ audio_checkbox, # Ensure the checkbox visibility is updated
202
+ #start_question_slider, # Ensure the slider is shown
203
+ question_text, question_text, choices, answer_button,
204
+ next_button, prev_button, home_button, question_state, result_text, explanation_text, explain_button
205
+ ])
206
+
207
+ demo.launch()