ruslanmv commited on
Commit
29251f4
·
verified ·
1 Parent(s): b8ae48d

Update app.py

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