ruslanmv commited on
Commit
828c2eb
·
verified ·
1 Parent(s): 220840d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -42
app.py CHANGED
@@ -1,7 +1,7 @@
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, all functionality now in tool2.py
5
 
6
  # Global variable to store the currently selected set of exam questions
7
  selected_questions = []
@@ -44,10 +44,10 @@ def start_exam(exam_choice, start_question, audio_enabled):
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: # prevent negative start question values
48
  start_question = 0
49
 
50
- question, options, audio_path = display_question(start_question, audio_enabled, selected_questions) # Corrected index here to be 0-based
51
 
52
  return (
53
  # Hide start screen elements
@@ -73,76 +73,77 @@ def start_exam(exam_choice, start_question, audio_enabled):
73
  )
74
 
75
 
76
- def display_question_ui(index, audio_enabled): # Changed function name and parameters
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 and hides previous results."""
84
- explanation, correct_answer = get_explanation_and_answer(index, selected_questions) # Get both explanation and answer
85
- if 0 <= index < len(selected_questions):
 
 
 
 
 
 
86
  return (
87
  f"**Explanation:** {explanation}",
88
  gr.update(visible=True), # Show explanation_text
89
- gr.update(visible=True) # Show result_text - ensure result_text is shown when explanation is shown
90
  )
91
- else:
92
- return "No explanation available for this question.", gr.update(visible=False), gr.update(visible=False)
93
-
94
 
95
  def check_answer(index, answer):
96
- """Checks the given answer against the correct answer."""
97
- explanation, correct_answer = get_explanation_and_answer(index, selected_questions) # Get correct answer
98
  if answer == correct_answer:
99
  return f"Correct! The answer is: {correct_answer}"
100
  else:
101
  return f"Incorrect. The correct answer is: {correct_answer}"
102
 
103
 
 
104
  def update_question(index, audio_enabled):
105
  """Updates the displayed question when the index changes."""
106
- return display_question_ui(index, audio_enabled) # Use display_question_ui for UI updates
107
 
108
 
109
  def handle_answer(index, answer, audio_enabled, current_audio):
110
  """Handles answer submission, provides feedback, and generates audio."""
111
- # Handle the case when no answer is selected
112
  if answer is None:
113
  return "Please select an option before submitting.", None, None
114
- # Stop the current question audio before playing the answer audio
115
- stop_audio = True if current_audio else False # This line isn't actually used. It's better to handle the audio stopping directly in the UI.
116
- result = check_answer(index, answer)
117
  answer_audio_path = text_to_speech(result) if audio_enabled else None
118
- return result, answer_audio_path, None # Return None for stop_audio as Gradio doesn't easily support two audios playing at once and stopping.
119
 
120
 
121
  def handle_next(index, audio_enabled):
122
  """Moves to the next question and updates the UI."""
123
  new_index = min(index + 1, len(selected_questions) - 1)
124
  question, options, audio_path = update_question(new_index, audio_enabled)
125
- return question, options, new_index, "", audio_path, gr.update(visible=False), gr.update(value="") # Hide explanation and clear result text
126
 
127
 
128
  def handle_previous(index, audio_enabled):
129
  """Moves to the previous question and updates the UI."""
130
  new_index = max(index - 1, 0)
131
  question, options, audio_path = update_question(new_index, audio_enabled)
132
- return question, options, new_index, "", audio_path, gr.update(visible=False), gr.update(value="") # Hide explanation and clear result text
133
 
134
 
135
  def return_home():
136
  """Returns to the home screen."""
137
  return (
138
  # Show start screen elements
139
- 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
140
  gr.update(visible=True), # Show the audio_checkbox
141
- gr.update(visible=True), # Show start_question_slider - show slider on home return
142
  # Hide quiz elements
143
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
144
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), 0, "", gr.update(visible=False), gr.update(visible=False),
145
- gr.update(visible=False), gr.update(value="") # Hide explain button and clear result text
146
  )
147
 
148
 
@@ -152,15 +153,15 @@ with gr.Blocks() as demo:
152
  description = gr.Markdown(value=description_str)
153
  exam_selector = gr.Dropdown(label="Select an exam", choices=exams, value=None)
154
  audio_checkbox = gr.Checkbox(label="Enable Audio", value=True, visible=False)
155
- start_question_slider = gr.Slider(minimum=1, maximum=50, step=1, label="Select starting question", value = 1, visible=False) # Slider for selecting the starting question, minimum is now 1, default value = 1
156
  start_button = gr.Button("Start Exam", visible=False)
157
 
158
  # Quiz elements (initially hidden)
159
  question_state = gr.State(0)
160
- current_audio_state = gr.State(None) # State to track the current audio playing. This isn't strictly needed if we don't try to stop the audio.
161
  question_text = gr.Markdown(visible=False, elem_id="question-text")
162
  choices = gr.Radio(visible=False, label="Options")
163
- result_text = gr.Markdown(visible=False) # Initially hidden, shown when answer is submitted or explanation is shown
164
  explanation_text = gr.Markdown(visible=False)
165
  answer_button = gr.Button("Submit Answer", visible=False)
166
  next_button = gr.Button("Next Question", visible=False)
@@ -170,7 +171,7 @@ with gr.Blocks() as demo:
170
  question_audio = gr.Audio(visible=False, label="Question Audio", autoplay=True)
171
  answer_audio = gr.Audio(visible=False, label="Answer Audio", autoplay=True)
172
 
173
- # Layout for the home page
174
  with gr.Row():
175
  gr.Column([title])
176
  with gr.Row():
@@ -181,8 +182,6 @@ with gr.Blocks() as demo:
181
  gr.Column([audio_checkbox, start_question_slider])
182
  with gr.Row():
183
  gr.Column([start_button])
184
-
185
- # Layout for the quiz
186
  with gr.Row():
187
  gr.Column([question_text, question_audio])
188
  with gr.Row():
@@ -198,36 +197,29 @@ with gr.Blocks() as demo:
198
  with gr.Row():
199
  gr.Column([home_button])
200
 
201
- # Show settings after exam selection
202
  def show_settings(exam_choice):
203
  return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
204
 
205
- # Connect exam selection to display settings section
206
  exam_selector.change(fn=show_settings, inputs=[exam_selector], outputs=[audio_checkbox, start_question_slider, start_button])
207
-
208
- # Connect the start button to start the exam
209
  start_button.click(
210
  fn=start_exam,
211
  inputs=[exam_selector, start_question_slider, audio_checkbox],
212
  outputs=[
213
  title, description, exam_selector, start_button,
214
- audio_checkbox, # Ensure the checkbox visibility is updated
215
- start_question_slider, # Ensure the slider is hidden
216
  question_text, question_text, choices, answer_button,
217
  next_button, prev_button, home_button, question_state, result_text, question_audio,
218
- explain_button, explanation_text, current_audio_state # Add explanation_text to outputs
219
  ]
220
  )
221
-
222
- # Connect the quiz buttons to their functions
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])
227
  home_button.click(fn=return_home, inputs=None, outputs=[
228
  title, description, exam_selector, start_button,
229
- audio_checkbox, # Ensure the checkbox visibility is updated
230
- start_question_slider, # Ensure the slider is shown
231
  question_text, question_text, choices, answer_button,
232
  next_button, prev_button, home_button, question_state, result_text, explanation_text, explain_button, result_text
233
  ])
 
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 = []
 
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
 
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)
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
 
 
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
  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
  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():
 
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
  ])