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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -23
app.py CHANGED
@@ -18,7 +18,7 @@ df = load_data()
18
  user_sessions = {}
19
 
20
  # Function to generate a random question based on user selections
21
- def generate_question(segment_type, description_type, user_session):
22
  segment = "Consonant" if segment_type == "Consonant" else "Vowel"
23
  description_field = "Full_description" if description_type == "Full_description" else "Casual_description"
24
 
@@ -30,12 +30,12 @@ def generate_question(segment_type, description_type, user_session):
30
  random_row = filtered_df.sample(1).iloc[0] # Pick a random row
31
  description = random_row[description_field] # Select either 'Full_description' or 'Casual_description'
32
  correct_ipa = random_row["IPA"] # The correct IPA symbol
33
- user_sessions[user_session]['correct_ipa'] = correct_ipa # Save correct IPA in session data
34
  return description
35
 
36
  # Function to check the user's answer
37
- def submit_answer(user_ipa, user_session):
38
- session = user_sessions[user_session]
39
  trials = session['trials'] + 1
40
  session['trials'] = trials
41
 
@@ -47,8 +47,8 @@ def submit_answer(user_ipa, user_session):
47
  return "Try again", session['score'], session['trials']
48
 
49
  # Function to quit and show results
50
- def quit_quiz(user_session):
51
- session = user_sessions.pop(user_session, {"score": 0, "trials": 0}) # Default in case session is missing
52
  return f"Your final score is {session['score']}/{session['trials']}."
53
 
54
  # Gradio interface
@@ -70,36 +70,35 @@ def gradio_app():
70
 
71
  # Initialize a unique session ID for each user
72
  def init_session():
73
- user_session = random.randint(1000, 9999) # Use random number as session ID
74
- user_sessions[user_session] = {
75
  "score": 0,
76
  "trials": 0,
77
  "correct_ipa": ""
78
  }
79
- return user_session
80
 
81
  # Start quiz based on selections
82
- def start(segment_type, description_type, user_session):
83
- description = generate_question(segment_type, description_type, user_session)
84
- return description, "", gr.update(visible=True), user_sessions[user_session]['score'], user_sessions[user_session]['trials']
 
85
 
86
  # Submit the answer and check
87
- def submit(user_ipa, user_session):
88
- result, score, trials = submit_answer(user_ipa, user_session)
89
  return result, score, trials
90
 
91
  # Quit and show final results
92
- def quit(user_session):
93
- return quit_quiz(user_session)
94
 
95
  # Bind actions
96
- user_session = init_session() # Initialize a new session for each user
97
-
98
- start_button.click(fn=start, inputs=[segment_type, description_type, gr.State(user_session)],
99
- outputs=[description_output, ipa_input, submit_button, gr.State(user_session), gr.State(user_session)])
100
- submit_button.click(fn=submit, inputs=[ipa_input, gr.State(user_session)],
101
- outputs=[result_output, gr.State(user_session), gr.State(user_session)])
102
- quit_button.click(fn=quit, inputs=[gr.State(user_session)], outputs=[result_output])
103
 
104
  return app
105
 
 
18
  user_sessions = {}
19
 
20
  # Function to generate a random question based on user selections
21
+ def generate_question(segment_type, description_type, user_id):
22
  segment = "Consonant" if segment_type == "Consonant" else "Vowel"
23
  description_field = "Full_description" if description_type == "Full_description" else "Casual_description"
24
 
 
30
  random_row = filtered_df.sample(1).iloc[0] # Pick a random row
31
  description = random_row[description_field] # Select either 'Full_description' or 'Casual_description'
32
  correct_ipa = random_row["IPA"] # The correct IPA symbol
33
+ user_sessions[user_id]['correct_ipa'] = correct_ipa # Save correct IPA in session data
34
  return description
35
 
36
  # Function to check the user's answer
37
+ def submit_answer(user_ipa, user_id):
38
+ session = user_sessions[user_id]
39
  trials = session['trials'] + 1
40
  session['trials'] = trials
41
 
 
47
  return "Try again", session['score'], session['trials']
48
 
49
  # Function to quit and show results
50
+ def quit_quiz(user_id):
51
+ session = user_sessions.pop(user_id, {"score": 0, "trials": 0}) # Default in case session is missing
52
  return f"Your final score is {session['score']}/{session['trials']}."
53
 
54
  # Gradio interface
 
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
74
+ user_sessions[user_id] = {
75
  "score": 0,
76
  "trials": 0,
77
  "correct_ipa": ""
78
  }
79
+ return user_id
80
 
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):
94
+ return quit_quiz(user_id)
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