ruslanmv commited on
Commit
01d833c
·
1 Parent(s): 0114251

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -36
app.py CHANGED
@@ -1,16 +1,7 @@
1
- '''
2
- AWS Exam Simulator v.05
3
- Program Developed by Ruslan Magana Vsevolovna
4
- The purpose of this program is help to practice the questions of AWS Exams.
5
- '''
6
  import gradio as gr
7
  from tool import * # Assuming this module contains your exam data and text-to-speech functionality
8
  from backend1 import *
9
 
10
-
11
- # Global variable to store the currently selected set of exam questions
12
- selected_questions = []
13
-
14
  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.
15
  For more information about the developer, please visit [ruslanmv.com](https://ruslanmv.com/).
16
  **Get Started with Your Quiz**
@@ -18,23 +9,25 @@ Select an exam from the dropdown menu below and start testing your skills. You c
18
 
19
  # --- FUNCTION DEFINITIONS ---
20
 
21
- def start_exam(exam_choice, audio_enabled):
22
  """Starts the exam by selecting questions, setting up UI."""
23
- global selected_questions
24
- selected_questions = select_exam_vce(exam_choice)
25
- question, options, audio_path = display_question(0, audio_enabled)
 
26
  return (
27
  # Hide start screen elements
28
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
29
- gr.update(visible=False), # Hide the audio_checkbox
30
  # Show quiz elements
31
  gr.update(visible=True), question, gr.update(choices=options, visible=True), gr.update(visible=True),
32
  gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), 0, "", audio_path, gr.update(visible=True),
33
- gr.update(visible=True), None # None for the audio stop signal
34
  )
35
 
36
- def display_question(index, audio_enabled):
37
  """Displays a question with options and generates audio (if enabled)."""
 
38
  if index < 0 or index >= len(selected_questions):
39
  return "No more questions.", [], None
40
  question_text_ = selected_questions[index].get('question', 'No question text available.')
@@ -43,8 +36,9 @@ def display_question(index, audio_enabled):
43
  audio_path = text_to_speech(question_text_ + " " + " ".join(choices_options)) if audio_enabled else None
44
  return question_text, choices_options, audio_path
45
 
46
- def show_explanation(index):
47
  """Shows the explanation for the current question and hides previous results."""
 
48
  if 0 <= index < len(selected_questions):
49
  explanation = selected_questions[index].get('explanation', 'No explanation available for this question.')
50
  return (
@@ -55,20 +49,21 @@ def show_explanation(index):
55
  else:
56
  return "No explanation available for this question.", gr.update(visible=False), gr.update(visible=False)
57
 
58
- def check_answer(index, answer):
59
  """Checks the given answer against the correct answer."""
 
60
  correct_answer = selected_questions[index].get('correct', 'No correct answer provided.')
61
  if answer == correct_answer:
62
  return f"Correct! The answer is: {correct_answer}"
63
  else:
64
  return f"Incorrect. The correct answer is: {correct_answer}"
65
 
66
- def update_question(index, audio_enabled):
67
  """Updates the displayed question when the index changes."""
68
- question, options, audio_path = display_question(index, audio_enabled)
69
  return question, gr.update(choices=options), index, audio_path
70
 
71
- def handle_answer(index, answer, audio_enabled, current_audio):
72
  """Handles answer submission, provides feedback, and generates audio."""
73
  # Handle the case when no answer is selected
74
  if answer is None:
@@ -76,22 +71,22 @@ def handle_answer(index, answer, audio_enabled, current_audio):
76
 
77
  # Stop the current question audio before playing the answer audio
78
  stop_audio = True if current_audio else False
79
- result = check_answer(index, answer)
80
  answer_audio_path = text_to_speech(result) if audio_enabled else None
81
  return result, answer_audio_path, stop_audio
82
 
83
 
84
- def handle_next(index, audio_enabled):
85
  """Moves to the next question and updates the UI."""
86
- new_index = min(index + 1, len(selected_questions) - 1)
87
- question, options, new_index, audio_path = update_question(new_index, audio_enabled)
88
  return question, options, new_index, "", audio_path, gr.update(visible=False) # Hide explanation
89
 
90
- def handle_previous(index, audio_enabled):
91
  """Moves to the previous question and updates the UI."""
92
  new_index = max(index - 1, 0)
93
- question, options, new_index, audio_path = update_question(new_index, audio_enabled)
94
- return question, options, new_index, "", audio_path, gr.update(visible=False) # Hide explanation
95
 
96
  def return_home():
97
  """Returns to the home screen."""
@@ -102,7 +97,7 @@ def return_home():
102
  # Hide quiz elements
103
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
104
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), "", "", gr.update(visible=False), gr.update(visible=False),
105
- gr.update(visible=False) # Hide explain button
106
  )
107
 
108
  with gr.Blocks() as demo:
@@ -116,6 +111,7 @@ with gr.Blocks() as demo:
116
  # Quiz elements (initially hidden)
117
  question_state = gr.State(0)
118
  current_audio_state = gr.State(None) # State to track the current audio playing
 
119
  question_text = gr.Markdown(visible=False, elem_id="question-text")
120
  choices = gr.Radio(visible=False, label="Options")
121
  result_text = gr.Markdown(visible=True)
@@ -157,22 +153,22 @@ with gr.Blocks() as demo:
157
  # Connect the start button to start the exam
158
  start_button.click(
159
  fn=start_exam,
160
- inputs=[exam_selector, audio_checkbox],
161
  outputs=[
162
  title, description, exam_selector, start_button,
163
  audio_checkbox, # Ensure the checkbox visibility is updated
164
  question_text, question_text, choices, answer_button,
165
  next_button, prev_button, home_button, question_state, result_text, question_audio,
166
- explain_button, current_audio_state # Add current_audio_state to the outputs
167
  ]
168
  )
169
 
170
  # Connect the quiz buttons to their functions
171
- answer_button.click(fn=handle_answer, inputs=[question_state, choices, audio_checkbox, current_audio_state], outputs=[result_text, answer_audio, current_audio_state])
172
- next_button.click(fn=handle_next, inputs=[question_state, audio_checkbox], outputs=[question_text, choices, question_state, result_text, question_audio, explanation_text])
173
- prev_button.click(fn=handle_previous, inputs=[question_state, audio_checkbox], outputs=[question_text, choices, question_state, result_text, question_audio, explanation_text])
174
 
175
- explain_button.click(fn=show_explanation, inputs=[question_state], outputs=[explanation_text, result_text, explanation_text]) # Output to both to toggle visibility
176
 
177
  home_button.click(fn=return_home, inputs=None, outputs=[
178
  title, description, exam_selector, start_button,
@@ -182,4 +178,4 @@ with gr.Blocks() as demo:
182
  ])
183
 
184
 
185
- demo.queue(max_size=100).launch(debug=True, max_threads=75)
 
 
 
 
 
 
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
  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.
6
  For more information about the developer, please visit [ruslanmv.com](https://ruslanmv.com/).
7
  **Get Started with Your Quiz**
 
9
 
10
  # --- FUNCTION DEFINITIONS ---
11
 
12
+ def start_exam(exam_choice, audio_enabled, selected_questions_state):
13
  """Starts the exam by selecting questions, setting up UI."""
14
+ selected_questions = select_exam_vce(exam_choice) # Fetch new questions specific to the exam choice
15
+ selected_questions_state = selected_questions # Store the selected questions in the state
16
+
17
+ question, options, audio_path = display_question(0, audio_enabled, selected_questions_state)
18
  return (
19
  # Hide start screen elements
20
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
21
+ gr.update(visible=False), # Hide the audio_checkbox
22
  # Show quiz elements
23
  gr.update(visible=True), question, gr.update(choices=options, visible=True), gr.update(visible=True),
24
  gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), 0, "", audio_path, gr.update(visible=True),
25
+ gr.update(visible=True), selected_questions_state, None # None for the audio stop signal
26
  )
27
 
28
+ def display_question(index, audio_enabled, selected_questions_state):
29
  """Displays a question with options and generates audio (if enabled)."""
30
+ selected_questions = selected_questions_state
31
  if index < 0 or index >= len(selected_questions):
32
  return "No more questions.", [], None
33
  question_text_ = selected_questions[index].get('question', 'No question text available.')
 
36
  audio_path = text_to_speech(question_text_ + " " + " ".join(choices_options)) if audio_enabled else None
37
  return question_text, choices_options, audio_path
38
 
39
+ def show_explanation(index, selected_questions_state):
40
  """Shows the explanation for the current question and hides previous results."""
41
+ selected_questions = selected_questions_state
42
  if 0 <= index < len(selected_questions):
43
  explanation = selected_questions[index].get('explanation', 'No explanation available for this question.')
44
  return (
 
49
  else:
50
  return "No explanation available for this question.", gr.update(visible=False), gr.update(visible=False)
51
 
52
+ def check_answer(index, answer, selected_questions_state):
53
  """Checks the given answer against the correct answer."""
54
+ selected_questions = selected_questions_state
55
  correct_answer = selected_questions[index].get('correct', 'No correct answer provided.')
56
  if answer == correct_answer:
57
  return f"Correct! The answer is: {correct_answer}"
58
  else:
59
  return f"Incorrect. The correct answer is: {correct_answer}"
60
 
61
+ def update_question(index, audio_enabled, selected_questions_state):
62
  """Updates the displayed question when the index changes."""
63
+ question, options, audio_path = display_question(index, audio_enabled, selected_questions_state)
64
  return question, gr.update(choices=options), index, audio_path
65
 
66
+ def handle_answer(index, answer, audio_enabled, current_audio, selected_questions_state):
67
  """Handles answer submission, provides feedback, and generates audio."""
68
  # Handle the case when no answer is selected
69
  if answer is None:
 
71
 
72
  # Stop the current question audio before playing the answer audio
73
  stop_audio = True if current_audio else False
74
+ result = check_answer(index, answer, selected_questions_state)
75
  answer_audio_path = text_to_speech(result) if audio_enabled else None
76
  return result, answer_audio_path, stop_audio
77
 
78
 
79
+ def handle_next(index, audio_enabled, selected_questions_state):
80
  """Moves to the next question and updates the UI."""
81
+ new_index = min(index + 1, len(selected_questions_state) - 1)
82
+ question, options, new_index, audio_path = update_question(new_index, audio_enabled, selected_questions_state)
83
  return question, options, new_index, "", audio_path, gr.update(visible=False) # Hide explanation
84
 
85
+ def handle_previous(index, audio_enabled, selected_questions_state):
86
  """Moves to the previous question and updates the UI."""
87
  new_index = max(index - 1, 0)
88
+ question, options, new_index, audio_path = update_question(new_index, audio_enabled, selected_questions_state)
89
+ return question, options, new_index, "", audio_path, gr.update(visible=False)
90
 
91
  def return_home():
92
  """Returns to the home screen."""
 
97
  # Hide quiz elements
98
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
99
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), "", "", gr.update(visible=False), gr.update(visible=False),
100
+ gr.update(visible=False) # Hide explain button
101
  )
102
 
103
  with gr.Blocks() as demo:
 
111
  # Quiz elements (initially hidden)
112
  question_state = gr.State(0)
113
  current_audio_state = gr.State(None) # State to track the current audio playing
114
+ selected_questions_state = gr.State([]) # State to store selected questions for the user session
115
  question_text = gr.Markdown(visible=False, elem_id="question-text")
116
  choices = gr.Radio(visible=False, label="Options")
117
  result_text = gr.Markdown(visible=True)
 
153
  # Connect the start button to start the exam
154
  start_button.click(
155
  fn=start_exam,
156
+ inputs=[exam_selector, audio_checkbox, selected_questions_state],
157
  outputs=[
158
  title, description, exam_selector, start_button,
159
  audio_checkbox, # Ensure the checkbox visibility is updated
160
  question_text, question_text, choices, answer_button,
161
  next_button, prev_button, home_button, question_state, result_text, question_audio,
162
+ explain_button, selected_questions_state, current_audio_state # Add selected_questions_state to store per-user questions
163
  ]
164
  )
165
 
166
  # Connect the quiz buttons to their functions
167
+ answer_button.click(fn=handle_answer, inputs=[question_state, choices, audio_checkbox, current_audio_state, selected_questions_state], outputs=[result_text, answer_audio, current_audio_state])
168
+ next_button.click(fn=handle_next, inputs=[question_state, audio_checkbox, selected_questions_state], outputs=[question_text, choices, question_state, result_text, question_audio, explanation_text])
169
+ prev_button.click(fn=handle_previous, inputs=[question_state, audio_checkbox, selected_questions_state], outputs=[question_text, choices, question_state, result_text, question_audio, explanation_text])
170
 
171
+ explain_button.click(fn=show_explanation, inputs=[question_state, selected_questions_state], outputs=[explanation_text, result_text, explanation_text]) # Output to both to toggle visibility
172
 
173
  home_button.click(fn=return_home, inputs=None, outputs=[
174
  title, description, exam_selector, start_button,
 
178
  ])
179
 
180
 
181
+ demo.queue(max_size=100).launch(debug=True, max_threads=75)