MK-316 commited on
Commit
55febca
1 Parent(s): 9d422a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -42,9 +42,9 @@ def submit_answer(user_ipa, user_id):
42
  if user_ipa.strip() == session['correct_ipa']:
43
  score = session['score'] + 1
44
  session['score'] = score
45
- return "Correct!", session['score'], session['trials']
46
  else:
47
- return "Try again", session['score'], session['trials']
48
 
49
  # Function to quit and show results
50
  def quit_quiz(user_id):
@@ -68,6 +68,9 @@ def gradio_app():
68
  result_output = gr.Textbox(label="Result", interactive=False)
69
  quit_button = gr.Button("Quit")
70
 
 
 
 
71
  # Initialize a unique session ID for each user
72
  def init_session():
73
  user_id = random.randint(1000, 9999) # Use random number as session ID
@@ -81,13 +84,18 @@ def gradio_app():
81
  # Start quiz based on selections
82
  def start(segment_type, description_type):
83
  user_id = init_session() # Create session for this user
 
 
 
84
  description = generate_question(segment_type, description_type, user_id)
85
  return description, "", gr.update(visible=True), user_id, user_sessions[user_id]['score'], user_sessions[user_id]['trials']
86
 
87
- # Submit the answer and check
88
- def submit(user_ipa, user_id):
89
  result, score, trials = submit_answer(user_ipa, user_id)
90
- return result, score, trials
 
 
91
 
92
  # Quit and show final results
93
  def quit(user_id):
@@ -95,10 +103,10 @@ def gradio_app():
95
 
96
  # Bind actions
97
  start_button.click(fn=start, inputs=[segment_type, description_type],
98
- outputs=[description_output, ipa_input, submit_button, gr.State(), gr.State(), gr.State()])
99
- submit_button.click(fn=submit, inputs=[ipa_input, gr.State()],
100
- outputs=[result_output, gr.State(), gr.State()])
101
- quit_button.click(fn=quit, inputs=[gr.State()], outputs=[result_output])
102
 
103
  return app
104
 
 
42
  if user_ipa.strip() == session['correct_ipa']:
43
  score = session['score'] + 1
44
  session['score'] = score
45
+ return f"Correct! The answer was '{session['correct_ipa']}'.", session['score'], session['trials']
46
  else:
47
+ return f"Try again. The correct answer was '{session['correct_ipa']}'.", session['score'], session['trials']
48
 
49
  # Function to quit and show results
50
  def quit_quiz(user_id):
 
68
  result_output = gr.Textbox(label="Result", interactive=False)
69
  quit_button = gr.Button("Quit")
70
 
71
+ # State variables for user ID
72
+ user_id_state = gr.State()
73
+
74
  # Initialize a unique session ID for each user
75
  def init_session():
76
  user_id = random.randint(1000, 9999) # Use random number as session ID
 
84
  # Start quiz based on selections
85
  def start(segment_type, description_type):
86
  user_id = init_session() # Create session for this user
87
+ # Reset score and trials to 0 when the quiz starts again
88
+ user_sessions[user_id]["score"] = 0
89
+ user_sessions[user_id]["trials"] = 0
90
  description = generate_question(segment_type, description_type, user_id)
91
  return description, "", gr.update(visible=True), user_id, user_sessions[user_id]['score'], user_sessions[user_id]['trials']
92
 
93
+ # Submit the answer and check, and generate new question
94
+ def submit(user_ipa, segment_type, description_type, user_id):
95
  result, score, trials = submit_answer(user_ipa, user_id)
96
+ # After checking, generate a new description automatically
97
+ new_description = generate_question(segment_type, description_type, user_id)
98
+ return new_description, "", result, score, trials
99
 
100
  # Quit and show final results
101
  def quit(user_id):
 
103
 
104
  # Bind actions
105
  start_button.click(fn=start, inputs=[segment_type, description_type],
106
+ outputs=[description_output, ipa_input, submit_button, user_id_state, gr.State(), gr.State()])
107
+ submit_button.click(fn=submit, inputs=[ipa_input, segment_type, description_type, user_id_state],
108
+ outputs=[description_output, ipa_input, result_output, gr.State(), gr.State()])
109
+ quit_button.click(fn=quit, inputs=[user_id_state], outputs=[result_output])
110
 
111
  return app
112