MK-316 commited on
Commit
102b7ce
1 Parent(s): 55febca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -50
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import random
2
  import gradio as gr
3
  import pandas as pd
@@ -14,53 +16,51 @@ def load_data():
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_id):
22
  segment = "Consonant" if segment_type == "Consonant" else "Vowel"
23
  description_field = "Full_description" if description_type == "Full_description" else "Casual_description"
24
 
25
  filtered_df = df[df["Segment"] == segment] # Filter based on consonant/vowel selection
26
 
27
  if filtered_df.empty:
28
- return "No data available", "" # Handle empty filter case
29
 
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
-
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):
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
55
  def gradio_app():
56
  with gr.Blocks() as app:
 
 
 
57
  # Radio buttons to select Consonant or Vowel
58
  segment_type = gr.Radio(choices=["Consonant", "Vowel"], label="Select Consonant or Vowel", value="Consonant")
59
  # Radio buttons to select description type (Full or Casual)
60
  description_type = gr.Radio(choices=["Full_description", "Casual_description"], label="Select Description Type", value="Full_description")
61
 
62
- # Start button placed after the selections
63
- start_button = gr.Button("Show a phonetic description")
64
 
65
  description_output = gr.Textbox(label="Description", interactive=False)
66
  ipa_input = gr.Textbox(label="Enter IPA", placeholder="Type IPA symbol here")
@@ -68,45 +68,42 @@ def gradio_app():
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
77
- user_sessions[user_id] = {
78
  "score": 0,
79
  "trials": 0,
 
80
  "correct_ipa": ""
81
  }
82
- return user_id
83
-
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):
102
- return quit_quiz(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
 
 
1
+ #@markdown This app resolved variable issues (0923)
2
+
3
  import random
4
  import gradio as gr
5
  import pandas as pd
 
16
  # Load the data into a DataFrame
17
  df = load_data()
18
 
19
+ # Dictionary to store each user's session-specific data
20
  user_sessions = {}
21
 
22
  # Function to generate a random question based on user selections
23
+ def generate_question(segment_type, description_type, used_questions):
24
  segment = "Consonant" if segment_type == "Consonant" else "Vowel"
25
  description_field = "Full_description" if description_type == "Full_description" else "Casual_description"
26
 
27
  filtered_df = df[df["Segment"] == segment] # Filter based on consonant/vowel selection
28
 
29
  if filtered_df.empty:
30
+ return "No data available", "", used_questions # Handle empty filter case
31
 
32
  random_row = filtered_df.sample(1).iloc[0] # Pick a random row
33
  description = random_row[description_field] # Select either 'Full_description' or 'Casual_description'
34
  correct_ipa = random_row["IPA"] # The correct IPA symbol
35
+ used_questions.append(description) # Track used descriptions to avoid repetition
36
+ return description, correct_ipa, used_questions
37
 
38
  # Function to check the user's answer
39
+ def submit_answer(user_ipa, correct_ipa, score, trials):
40
+ trials += 1
41
+ if user_ipa.strip() == correct_ipa:
42
+ score += 1
43
+ return f"Correct! The answer was '{correct_ipa}'", score, trials
 
 
 
 
44
  else:
45
+ return f"Wrong! The correct answer was '{correct_ipa}'", score, trials
46
 
47
  # Function to quit and show results
48
+ def quit_quiz(score, trials, name):
49
+ return f"{name}! Your final score is {score}/{trials}."
 
50
 
51
  # Gradio interface
52
  def gradio_app():
53
  with gr.Blocks() as app:
54
+ # Name input
55
+ name_input = gr.Textbox(label="Enter your name", placeholder="Your name")
56
+
57
  # Radio buttons to select Consonant or Vowel
58
  segment_type = gr.Radio(choices=["Consonant", "Vowel"], label="Select Consonant or Vowel", value="Consonant")
59
  # Radio buttons to select description type (Full or Casual)
60
  description_type = gr.Radio(choices=["Full_description", "Casual_description"], label="Select Description Type", value="Full_description")
61
 
62
+ # Start button renamed to "Quiz to start"
63
+ start_button = gr.Button("Quiz to start")
64
 
65
  description_output = gr.Textbox(label="Description", interactive=False)
66
  ipa_input = gr.Textbox(label="Enter IPA", placeholder="Type IPA symbol here")
 
68
  result_output = gr.Textbox(label="Result", interactive=False)
69
  quit_button = gr.Button("Quit")
70
 
71
+ # Start quiz and reset session data for the user
72
+ def start(segment_type, description_type, name):
73
+ # Initialize/reset user session
74
+ user_sessions[name] = {
 
 
 
75
  "score": 0,
76
  "trials": 0,
77
+ "used_questions": [],
78
  "correct_ipa": ""
79
  }
80
+ description, correct_ipa, used_questions = generate_question(segment_type, description_type, user_sessions[name]["used_questions"])
81
+ user_sessions[name]["correct_ipa"] = correct_ipa
82
+ user_sessions[name]["used_questions"] = used_questions
83
+ return description, "", gr.update(visible=True), user_sessions[name]["score"], user_sessions[name]["trials"]
 
 
 
 
 
 
84
 
85
+ # Submit the answer and check
86
+ def submit(name, user_ipa):
87
+ session = user_sessions[name]
88
+ result, score, trials = submit_answer(user_ipa, session["correct_ipa"], session["score"], session["trials"])
89
+ session["score"] = score
90
+ session["trials"] = trials
91
+
92
+ # Generate new question after submitting the answer
93
+ description, correct_ipa, used_questions = generate_question(segment_type.value, description_type.value, session["used_questions"])
94
+ session["correct_ipa"] = correct_ipa
95
+ session["used_questions"] = used_questions
96
+ return description, "", result, gr.update(visible=True), result, score, trials
97
 
98
  # Quit and show final results
99
+ def quit(name):
100
+ session = user_sessions[name]
101
+ return quit_quiz(session["score"], session["trials"], name)
102
 
103
  # Bind actions
104
+ start_button.click(fn=start, inputs=[segment_type, description_type, name_input], outputs=[description_output, ipa_input, submit_button, result_output, result_output])
105
+ submit_button.click(fn=submit, inputs=[name_input, ipa_input], outputs=[description_output, ipa_input, result_output, result_output, result_output])
106
+ quit_button.click(fn=quit, inputs=[name_input], outputs=[result_output])
 
 
107
 
108
  return app
109