ruslanmv commited on
Commit
1cb973f
·
1 Parent(s): 6a7bf0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -53
app.py CHANGED
@@ -1,7 +1,16 @@
 
 
 
 
 
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,26 +18,23 @@ Select an exam from the dropdown menu below and start testing your skills. You c
9
 
10
  # --- FUNCTION DEFINITIONS ---
11
 
12
- def start_exam(audio_enabled, selected_exam_state, selected_questions_state):
13
- """Starts the exam by selecting questions based on the selected exam, setting up UI."""
14
- selected_exam = selected_exam_state # Get the user-selected exam
15
- selected_questions = select_exam_vce(selected_exam) # Fetch new questions for the selected exam
16
- selected_questions_state = selected_questions # Store the selected questions in the session state
17
-
18
- question, options, audio_path = display_question(0, audio_enabled, selected_questions_state)
19
  return (
20
  # Hide start screen elements
21
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
22
- gr.update(visible=False), # Hide the audio_checkbox
23
  # Show quiz elements
24
  gr.update(visible=True), question, gr.update(choices=options, visible=True), gr.update(visible=True),
25
  gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), 0, "", audio_path, gr.update(visible=True),
26
- gr.update(visible=True), selected_questions_state, None # None for the audio stop signal
27
  )
28
 
29
- def display_question(index, audio_enabled, selected_questions_state):
30
  """Displays a question with options and generates audio (if enabled)."""
31
- selected_questions = selected_questions_state
32
  if index < 0 or index >= len(selected_questions):
33
  return "No more questions.", [], None
34
  question_text_ = selected_questions[index].get('question', 'No question text available.')
@@ -37,63 +43,68 @@ def display_question(index, audio_enabled, selected_questions_state):
37
  audio_path = text_to_speech(question_text_ + " " + " ".join(choices_options)) if audio_enabled else None
38
  return question_text, choices_options, audio_path
39
 
40
- def check_answer(index, answer, selected_questions_state):
 
 
 
 
 
 
 
 
 
 
 
 
41
  """Checks the given answer against the correct answer."""
42
- selected_questions = selected_questions_state
43
  correct_answer = selected_questions[index].get('correct', 'No correct answer provided.')
44
  if answer == correct_answer:
45
  return f"Correct! The answer is: {correct_answer}"
46
  else:
47
  return f"Incorrect. The correct answer is: {correct_answer}"
48
 
49
- def update_question(index, audio_enabled, selected_questions_state):
50
  """Updates the displayed question when the index changes."""
51
- question, options, audio_path = display_question(index, audio_enabled, selected_questions_state)
52
  return question, gr.update(choices=options), index, audio_path
53
 
54
- def handle_answer(index, answer, audio_enabled, current_audio, selected_questions_state):
55
  """Handles answer submission, provides feedback, and generates audio."""
 
56
  if answer is None:
57
  return "Please select an option before submitting.", None, None
58
-
 
59
  stop_audio = True if current_audio else False
60
- result = check_answer(index, answer, selected_questions_state)
61
  answer_audio_path = text_to_speech(result) if audio_enabled else None
62
  return result, answer_audio_path, stop_audio
63
 
64
- def handle_next(index, audio_enabled, selected_questions_state):
 
65
  """Moves to the next question and updates the UI."""
66
- new_index = min(index + 1, len(selected_questions_state) - 1)
67
- question, options, new_index, audio_path = update_question(new_index, audio_enabled, selected_questions_state)
68
- return question, options, new_index, "", audio_path, gr.update(visible=False)
69
 
70
- def handle_previous(index, audio_enabled, selected_questions_state):
71
  """Moves to the previous question and updates the UI."""
72
  new_index = max(index - 1, 0)
73
- question, options, new_index, audio_path = update_question(new_index, audio_enabled, selected_questions_state)
74
- return question, options, new_index, "", audio_path, gr.update(visible=False)
75
 
76
  def return_home():
77
  """Returns to the home screen."""
78
  return (
 
79
  gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
80
- gr.update(visible=True), # Show the audio_checkbox
 
81
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
82
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), "", "", gr.update(visible=False), gr.update(visible=False),
83
  gr.update(visible=False) # Hide explain button
84
  )
85
- def show_explanation(index, selected_questions_state):
86
- """Shows the explanation for the current question and hides previous results."""
87
- selected_questions = selected_questions_state
88
- if 0 <= index < len(selected_questions):
89
- explanation = selected_questions[index].get('explanation', 'No explanation available for this question.')
90
- return (
91
- f"**Explanation:** {explanation}",
92
- gr.update(visible=True), # Show explanation_text
93
- gr.update(visible=True) # Show result_text
94
- )
95
- else:
96
- return "No explanation available for this question.", gr.update(visible=False), gr.update(visible=False)
97
  with gr.Blocks() as demo:
98
  # Home page elements
99
  title = gr.Markdown(value="**AWS Exam Simulator (Quiz)**")
@@ -104,9 +115,7 @@ with gr.Blocks() as demo:
104
 
105
  # Quiz elements (initially hidden)
106
  question_state = gr.State(0)
107
- current_audio_state = gr.State(None)
108
- selected_questions_state = gr.State([]) # Store the selected questions for each user session
109
- selected_exam_state = gr.State("") # State to store the selected exam for each user session
110
  question_text = gr.Markdown(visible=False, elem_id="question-text")
111
  choices = gr.Radio(visible=False, label="Options")
112
  result_text = gr.Markdown(visible=True)
@@ -145,34 +154,31 @@ with gr.Blocks() as demo:
145
  with gr.Row():
146
  gr.Column([home_button])
147
 
148
- # Store the selected exam when chosen
149
- exam_selector.change(fn=lambda exam: exam, inputs=[exam_selector], outputs=[selected_exam_state])
150
-
151
  # Connect the start button to start the exam
152
  start_button.click(
153
  fn=start_exam,
154
- inputs=[audio_checkbox, selected_exam_state, selected_questions_state],
155
  outputs=[
156
  title, description, exam_selector, start_button,
157
- audio_checkbox,
158
  question_text, question_text, choices, answer_button,
159
  next_button, prev_button, home_button, question_state, result_text, question_audio,
160
- explain_button, selected_questions_state, current_audio_state
161
  ]
162
  )
163
 
164
  # Connect the quiz buttons to their functions
165
- 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])
166
- 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])
167
- 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])
168
 
169
- 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
170
 
171
  home_button.click(fn=return_home, inputs=None, outputs=[
172
  title, description, exam_selector, start_button,
173
- audio_checkbox,
174
  question_text, question_text, choices, answer_button,
175
  next_button, prev_button, home_button, question_state, result_text, explanation_text, explain_button
176
  ])
177
 
178
- demo.queue(max_size=100).launch(debug=True, max_threads=75)
 
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
 
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
  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 (
51
+ f"**Explanation:** {explanation}",
52
+ gr.update(visible=True), # Show explanation_text
53
+ gr.update(visible=True) # Show result_text
54
+ )
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:
75
  return "Please select an option before submitting.", None, None
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."""
98
  return (
99
+ # Show start screen elements
100
  gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
101
+ gr.update(visible=True), # Show the audio_checkbox
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:
109
  # Home page elements
110
  title = gr.Markdown(value="**AWS Exam Simulator (Quiz)**")
 
115
 
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)
 
154
  with gr.Row():
155
  gr.Column([home_button])
156
 
 
 
 
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,
179
+ audio_checkbox, # Ensure the checkbox visibility is updated
180
  question_text, question_text, choices, answer_button,
181
  next_button, prev_button, home_button, question_state, result_text, explanation_text, explain_button
182
  ])
183
 
184
+ demo.launch()