MK-316 commited on
Commit
55a178d
1 Parent(s): 6ac6001

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -28
app.py CHANGED
@@ -14,8 +14,11 @@ def load_data():
14
  # Load the data into a DataFrame
15
  df = load_data()
16
 
 
 
 
17
  # Function to generate a random question based on user selections
18
- def generate_question(segment_type, description_type):
19
  segment = "Consonant" if segment_type == "Consonant" else "Vowel"
20
  description_field = "Full_description" if description_type == "Full_description" else "Casual_description"
21
 
@@ -27,20 +30,26 @@ def generate_question(segment_type, description_type):
27
  random_row = filtered_df.sample(1).iloc[0] # Pick a random row
28
  description = random_row[description_field] # Select either 'Full_description' or 'Casual_description'
29
  correct_ipa = random_row["IPA"] # The correct IPA symbol
30
- return description, correct_ipa
 
31
 
32
  # Function to check the user's answer
33
- def submit_answer(user_ipa, correct_ipa, score, trials):
34
- trials += 1
35
- if user_ipa.strip() == correct_ipa:
36
- score += 1
37
- return "Correct!", score, trials
 
 
 
 
38
  else:
39
- return "Try again", score, trials
40
 
41
  # Function to quit and show results
42
- def quit_quiz(score, trials):
43
- return f"Your final score is {score}/{trials}."
 
44
 
45
  # Gradio interface
46
  def gradio_app():
@@ -59,32 +68,38 @@ def gradio_app():
59
  result_output = gr.Textbox(label="Result", interactive=False)
60
  quit_button = gr.Button("Quit")
61
 
62
- # State variables for score and trials
63
- score_state = gr.State(0)
64
- trials_state = gr.State(0)
65
- correct_ipa_state = gr.State("")
66
-
 
 
 
 
 
67
  # Start quiz based on selections
68
- def start(segment_type, description_type):
69
- description, correct_ipa = generate_question(segment_type, description_type)
70
- correct_ipa_state.value = correct_ipa # Save the correct IPA symbol in state
71
- return description, "", gr.update(visible=True), score_state.value, trials_state.value
72
 
73
  # Submit the answer and check
74
- def submit(user_ipa):
75
- result, score, trials = submit_answer(user_ipa, correct_ipa_state.value, score_state.value, trials_state.value)
76
- score_state.value = score
77
- trials_state.value = trials
78
  return result, score, trials
79
 
80
  # Quit and show final results
81
- def quit():
82
- return quit_quiz(score_state.value, trials_state.value)
83
 
84
  # Bind actions
85
- start_button.click(fn=start, inputs=[segment_type, description_type], outputs=[description_output, ipa_input, submit_button, score_state, trials_state])
86
- submit_button.click(fn=submit, inputs=[ipa_input], outputs=[result_output, score_state, trials_state])
87
- quit_button.click(fn=quit, outputs=[result_output])
 
 
 
 
88
 
89
  return app
90
 
 
14
  # Load the data into a DataFrame
15
  df = load_data()
16
 
17
+ # Store session data for each user
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
  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
+
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_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
55
  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_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