ruslanmv commited on
Commit
9d812fb
·
verified ·
1 Parent(s): 7743d64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -17
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 *
5
 
6
  # Global variable to store the currently selected set of exam questions
7
  selected_questions = []
@@ -44,14 +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
 
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
@@ -116,10 +112,10 @@ def handle_answer(index, answer, audio_enabled, current_audio):
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)
121
  answer_audio_path = text_to_speech(result) if audio_enabled else None
122
- return result, answer_audio_path, stop_audio
123
 
124
 
125
  def handle_next(index, audio_enabled):
@@ -156,12 +152,12 @@ with gr.Blocks() as demo:
156
  description = gr.Markdown(value=description_str)
157
  exam_selector = gr.Dropdown(label="Select an exam", choices=exams, value=None)
158
  audio_checkbox = gr.Checkbox(label="Enable Audio", value=True, visible=False)
159
- start_question_slider = gr.Slider(minimum=1, maximum=50, step=1, label="Select starting question", visible=False) # Slider for selecting the starting question, minimum is now 1
160
  start_button = gr.Button("Start Exam", visible=False)
161
 
162
  # Quiz elements (initially hidden)
163
  question_state = gr.State(0)
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
@@ -219,21 +215,21 @@ with gr.Blocks() as demo:
219
  start_question_slider, # Ensure the slider is hidden
220
  question_text, question_text, choices, answer_button,
221
  next_button, prev_button, home_button, question_state, result_text, question_audio,
222
- explain_button, explanation_text, current_audio_state # Add explanation_text to outputs, corrected from result_text, and use explanation_text for explanation
223
  ]
224
  )
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()
 
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
 
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
 
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):
 
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
 
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
  ])
234
 
235
  demo.launch()