ruslanmv commited on
Commit
7e00835
·
verified ·
1 Parent(s): 9fa5293

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -59
app.py CHANGED
@@ -1,6 +1,6 @@
1
  # app.py
2
  import gradio as gr
3
- from tool2 import * # Ensure you are using the updated tool2.py
4
  from backend1 import *
5
 
6
  # Global variable to store the currently selected set of exam questions
@@ -8,22 +8,23 @@ 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
  **Get Started with Your Quiz**
12
  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."""
13
 
14
- # --- FUNCTION DEFINITIONS ---
15
 
 
16
  def start_exam(exam_choice, start_question, audio_enabled):
17
  """Starts the exam by selecting questions, setting up UI."""
18
  global selected_questions
19
  selected_questions = select_exam_vce(exam_choice)
20
 
21
- if not selected_questions: # Handle case where no questions are loaded for selected exam
22
  return (
23
  gr.update(visible=True), # Show title
24
- gr.update(value="**Error: No Questions Found for this Exam**", visible=True), # Update description to error message
25
- gr.update(visible=True), # Show exam_selector
26
- gr.update(visible=True), # Show start_button
27
  gr.update(visible=True), # Show the audio_checkbox
28
  gr.update(visible=True), # Show start_question_slider
29
  # Hide quiz elements
@@ -35,49 +36,29 @@ def start_exam(exam_choice, start_question, audio_enabled):
35
  gr.update(visible=False), # Hide prev_button
36
  gr.update(visible=False), # Hide home_button
37
  0, "", # Update the question state
38
- None, # Provide the audio_path
39
  gr.update(visible=False), # Hide explain_button
40
  gr.update(visible=False),
41
- None # None for audio stop
42
  )
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
 
48
- question_list = select_exam_vce(exam_choice) # Get the question list here
49
- if not question_list: # handle case where select_exam_vce returns None or empty list
50
- return (
51
- gr.update(visible=True), # Show title
52
- gr.update(value="**Error: No Questions Found for this Exam**", visible=True), # Update description to error message
53
- gr.update(visible=True), # Show exam_selector
54
- gr.update(visible=True), # Show start_button
55
- gr.update(visible=True), # Show the audio_checkbox
56
- gr.update(visible=True), # Show start_question_slider
57
- # Hide quiz elements
58
- gr.update(visible=False), # Hide question_text
59
- "", # Question to display
60
- gr.update(choices=[], visible=False), # Hide Radio choices
61
- gr.update(visible=False), # Hide answer_button
62
- gr.update(visible=False), # Hide next_button
63
- gr.update(visible=False), # Hide prev_button
64
- gr.update(visible=False), # Hide home_button
65
- 0, "", # Update the question state
66
- None, # Provide the audio_path
67
- gr.update(visible=False), # Hide explain_button
68
- gr.update(visible=False),
69
- None # None for audio stop
70
- )
71
-
72
-
73
- question, options, audio_path = display_question(start_question -1 if start_question > 0 else 0, audio_enabled, selected_questions) # Corrected index here to be 0-based, using global selected_questions
74
 
75
  return (
76
  # Hide start screen elements
77
  gr.update(visible=False), # Hide title
78
- gr.update(visible=False), # Hide description
79
- gr.update(visible=False), # Hide exam_selector
80
- gr.update(visible=False), # Hide start_button
81
  gr.update(visible=False), # Hide the audio_checkbox
82
  gr.update(visible=False), # Hide start_question_slider
83
  # Show quiz elements
@@ -85,18 +66,18 @@ def start_exam(exam_choice, start_question, audio_enabled):
85
  question, # Question to display
86
  gr.update(choices=options, visible=True), # Update Radio choices and make visible
87
  gr.update(visible=True), # Show answer_button
88
- gr.update(visible=True),# Show next_button
89
- gr.update(visible=True), # Show prev_button
90
- gr.update(visible=True), # Show home_button
91
  start_question, "", # Update the question state
92
- audio_path, # Provide the audio_path
93
  gr.update(visible=True), # Show explain_button
94
  gr.update(visible=True),
95
  None # None for the audio stop signal
96
  )
97
 
98
 
99
- def display_question_ui(index, audio_enabled): # Changed function name and parameters
100
  """Displays a question with options and generates audio (if enabled) and updates UI elements."""
101
  question, options, audio_path = display_question(index, audio_enabled, selected_questions)
102
  return question, gr.update(choices=options), audio_path
@@ -104,27 +85,29 @@ def display_question_ui(index, audio_enabled): # Changed function name and param
104
 
105
  def show_explanation(index):
106
  """Shows the explanation for the current question and hides previous results."""
107
- explanation, correct_answer = get_explanation_and_answer(index, selected_questions) # Get both explanation and answer
108
  if 0 <= index < len(selected_questions):
109
  return (
110
  f"**Explanation:** {explanation}",
111
  gr.update(visible=True), # Show explanation_text
112
- gr.update(visible=True) # Show result_text - ensure result_text is shown when explanation is shown
113
  )
114
  else:
115
  return "No explanation available for this question.", gr.update(visible=False), gr.update(visible=False)
116
 
 
117
  def check_answer(index, answer):
118
  """Checks the given answer against the correct answer."""
119
- explanation, correct_answer = get_explanation_and_answer(index, selected_questions) # Get correct answer
120
  if answer == correct_answer:
121
  return f"Correct! The answer is: {correct_answer}"
122
  else:
123
  return f"Incorrect. The correct answer is: {correct_answer}"
124
 
 
125
  def update_question(index, audio_enabled):
126
  """Updates the displayed question when the index changes."""
127
- return display_question_ui(index, audio_enabled) # Use display_question_ui for UI updates
128
 
129
 
130
  def handle_answer(index, answer, audio_enabled, current_audio):
@@ -132,7 +115,6 @@ def handle_answer(index, answer, audio_enabled, current_audio):
132
  # Handle the case when no answer is selected
133
  if answer is None:
134
  return "Please select an option before submitting.", None, None
135
-
136
  # Stop the current question audio before playing the answer audio
137
  stop_audio = True if current_audio else False
138
  result = check_answer(index, answer)
@@ -144,28 +126,30 @@ def handle_next(index, audio_enabled):
144
  """Moves to the next question and updates the UI."""
145
  new_index = min(index + 1, len(selected_questions) - 1)
146
  question, options, audio_path = update_question(new_index, audio_enabled)
147
- return question, options, new_index, "", audio_path, gr.update(visible=False), gr.update(value="") # Hide explanation and clear result text
 
148
 
149
  def handle_previous(index, audio_enabled):
150
  """Moves to the previous question and updates the UI."""
151
  new_index = max(index - 1, 0)
152
  question, options, audio_path = update_question(new_index, audio_enabled)
153
- return question, options, new_index, "", audio_path, gr.update(visible=False), gr.update(value="") # Hide explanation and clear result text
154
 
155
 
156
  def return_home():
157
  """Returns to the home screen."""
158
  return (
159
  # Show start screen elements
160
- gr.update(visible=True), gr.update(value="**AWS Exam Simulator (Quiz)**", visible=True), gr.update(visible=True), gr.update(visible=True), # Reset title and description value
161
  gr.update(visible=True), # Show the audio_checkbox
162
  gr.update(visible=True), # Show start_question_slider - show slider on home return
163
  # Hide quiz elements
164
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
165
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), 0, "", gr.update(visible=False), gr.update(visible=False),
166
- gr.update(visible=False), gr.update(value="") # Hide explain button and clear result text
167
  )
168
 
 
169
  with gr.Blocks() as demo:
170
  # Home page elements
171
  title = gr.Markdown(value="**AWS Exam Simulator (Quiz)**")
@@ -180,7 +164,7 @@ with gr.Blocks() as demo:
180
  current_audio_state = gr.State(None) # State to track the current audio playing
181
  question_text = gr.Markdown(visible=False, elem_id="question-text")
182
  choices = gr.Radio(visible=False, label="Options")
183
- result_text = gr.Markdown(visible=False) # Initially hidden, shown when answer is submitted or explanation is shown
184
  explanation_text = gr.Markdown(visible=False)
185
  answer_button = gr.Button("Submit Answer", visible=False)
186
  next_button = gr.Button("Next Question", visible=False)
@@ -241,17 +225,15 @@ with gr.Blocks() as demo:
241
 
242
  # Connect the quiz buttons to their functions
243
  answer_button.click(fn=handle_answer, inputs=[question_state, choices, audio_checkbox, current_audio_state], outputs=[result_text, answer_audio, current_audio_state])
244
- 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]) # Corrected input to audio_checkbox
245
- 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]) # Corrected input to audio_checkbox
246
-
247
- explain_button.click(fn=show_explanation, inputs=[question_state], outputs=[explanation_text, result_text, explanation_text]) # Corrected output to explanation_text for the last element which was incorrectly result_text
248
-
249
  home_button.click(fn=return_home, inputs=None, outputs=[
250
  title, description, exam_selector, start_button,
251
  audio_checkbox, # Ensure the checkbox visibility is updated
252
  start_question_slider, # Ensure the slider is shown
253
  question_text, question_text, choices, answer_button,
254
- next_button, prev_button, home_button, question_state, result_text, explanation_text, explain_button, result_text # Added result_text to clear on home, and corrected outputs to include explanation_text
255
  ])
256
 
257
  demo.launch()
 
1
  # app.py
2
  import gradio as gr
3
+ from tool2 import * # Ensure you are using the updated tool2.py
4
  from backend1 import *
5
 
6
  # Global variable to store the currently selected set of exam 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
 
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
 
48
+ # The error occurred here. The `start_exam` function called `display_question` with three arguments:
49
+ # `start_question - 1 if start_question > 0 else 0`, `audio_enabled`, and `selected_questions`.
50
+ # However, the `display_question` function (presumably defined in tool2.py, though not shown) was
51
+ # defined to accept only two arguments.
52
+ # The traceback clearly states: `TypeError: display_question() takes 2 positional arguments but 3 were given`.
53
+ # I fixed this by modifying tool2.py to include selected_question as a parameter
54
+ question, options, audio_path = display_question(start_question - 1 if start_question > 0 else 0, audio_enabled, selected_questions) # Corrected index here to be 0-based
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  return (
57
  # Hide start screen elements
58
  gr.update(visible=False), # Hide title
59
+ gr.update(visible=False), # Hide description
60
+ gr.update(visible=False), # Hide exam_selector
61
+ gr.update(visible=False), # Hide start_button
62
  gr.update(visible=False), # Hide the audio_checkbox
63
  gr.update(visible=False), # Hide start_question_slider
64
  # Show quiz elements
 
66
  question, # Question to display
67
  gr.update(choices=options, visible=True), # Update Radio choices and make visible
68
  gr.update(visible=True), # Show answer_button
69
+ gr.update(visible=True), # Show next_button
70
+ gr.update(visible=True), # Show prev_button
71
+ gr.update(visible=True), # Show home_button
72
  start_question, "", # Update the question state
73
+ audio_path, # Provide the audio_path
74
  gr.update(visible=True), # Show explain_button
75
  gr.update(visible=True),
76
  None # None for the audio stop signal
77
  )
78
 
79
 
80
+ def display_question_ui(index, audio_enabled): # Changed function name and parameters
81
  """Displays a question with options and generates audio (if enabled) and updates UI elements."""
82
  question, options, audio_path = display_question(index, audio_enabled, selected_questions)
83
  return question, gr.update(choices=options), audio_path
 
85
 
86
  def show_explanation(index):
87
  """Shows the explanation for the current question and hides previous results."""
88
+ explanation, correct_answer = get_explanation_and_answer(index, selected_questions) # Get both explanation and answer
89
  if 0 <= index < len(selected_questions):
90
  return (
91
  f"**Explanation:** {explanation}",
92
  gr.update(visible=True), # Show explanation_text
93
+ gr.update(visible=True) # Show result_text - ensure result_text is shown when explanation is shown
94
  )
95
  else:
96
  return "No explanation available for this question.", gr.update(visible=False), gr.update(visible=False)
97
 
98
+
99
  def check_answer(index, answer):
100
  """Checks the given answer against the correct answer."""
101
+ explanation, correct_answer = get_explanation_and_answer(index, selected_questions) # Get correct answer
102
  if answer == correct_answer:
103
  return f"Correct! The answer is: {correct_answer}"
104
  else:
105
  return f"Incorrect. The correct answer is: {correct_answer}"
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):
 
115
  # Handle the case when no answer is selected
116
  if answer is None:
117
  return "Please select an option before submitting.", None, None
 
118
  # Stop the current question audio before playing the answer audio
119
  stop_audio = True if current_audio else False
120
  result = check_answer(index, answer)
 
126
  """Moves to the next question and updates the UI."""
127
  new_index = min(index + 1, len(selected_questions) - 1)
128
  question, options, audio_path = update_question(new_index, audio_enabled)
129
+ return question, options, new_index, "", audio_path, gr.update(visible=False), gr.update(value="") # Hide explanation and clear result text
130
+
131
 
132
  def handle_previous(index, audio_enabled):
133
  """Moves to the previous question and updates the UI."""
134
  new_index = max(index - 1, 0)
135
  question, options, audio_path = update_question(new_index, audio_enabled)
136
+ return question, options, new_index, "", audio_path, gr.update(visible=False), gr.update(value="") # Hide explanation and clear result text
137
 
138
 
139
  def return_home():
140
  """Returns to the home screen."""
141
  return (
142
  # Show start screen elements
143
+ gr.update(visible=True), gr.update(value="**AWS Exam Simulator (Quiz)**", visible=True), gr.update(visible=True), gr.update(visible=True), # Reset title and description value
144
  gr.update(visible=True), # Show the audio_checkbox
145
  gr.update(visible=True), # Show start_question_slider - show slider on home return
146
  # Hide quiz elements
147
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
148
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), 0, "", gr.update(visible=False), gr.update(visible=False),
149
+ gr.update(visible=False), gr.update(value="") # Hide explain button and clear result text
150
  )
151
 
152
+
153
  with gr.Blocks() as demo:
154
  # Home page elements
155
  title = gr.Markdown(value="**AWS Exam Simulator (Quiz)**")
 
164
  current_audio_state = gr.State(None) # State to track the current audio playing
165
  question_text = gr.Markdown(visible=False, elem_id="question-text")
166
  choices = gr.Radio(visible=False, label="Options")
167
+ result_text = gr.Markdown(visible=False) # Initially hidden, shown when answer is submitted or explanation is shown
168
  explanation_text = gr.Markdown(visible=False)
169
  answer_button = gr.Button("Submit Answer", visible=False)
170
  next_button = gr.Button("Next Question", visible=False)
 
225
 
226
  # Connect the quiz buttons to their functions
227
  answer_button.click(fn=handle_answer, inputs=[question_state, choices, audio_checkbox, current_audio_state], outputs=[result_text, answer_audio, current_audio_state])
228
+ 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]) # Corrected input to audio_checkbox
229
+ 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]) # Corrected input to audio_checkbox
230
+ explain_button.click(fn=show_explanation, inputs=[question_state], outputs=[explanation_text, result_text, explanation_text]) # Corrected output to explanation_text for the last element which was incorrectly result_text
 
 
231
  home_button.click(fn=return_home, inputs=None, outputs=[
232
  title, description, exam_selector, start_button,
233
  audio_checkbox, # Ensure the checkbox visibility is updated
234
  start_question_slider, # Ensure the slider is shown
235
  question_text, question_text, choices, answer_button,
236
+ next_button, prev_button, home_button, question_state, result_text, explanation_text, explain_button, result_text # Added result_text to clear on home, and corrected outputs to include explanation_text
237
  ])
238
 
239
  demo.launch()