Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import gradio as gr
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
# Load data from the provided URL
|
6 |
+
def load_data():
|
7 |
+
url = "https://raw.githubusercontent.com/MK316/Myapps/refs/heads/main/data/Phonetic-description.csv"
|
8 |
+
try:
|
9 |
+
data = pd.read_csv(url, encoding='utf-8') # Ensure correct encoding
|
10 |
+
return data
|
11 |
+
except Exception as e:
|
12 |
+
raise ValueError(f"Error loading data: {e}")
|
13 |
+
|
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 |
+
|
22 |
+
filtered_df = df[df["Segment"] == segment] # Filter based on consonant/vowel selection
|
23 |
+
|
24 |
+
if filtered_df.empty:
|
25 |
+
return "No data available", "" # Handle empty filter case
|
26 |
+
|
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():
|
47 |
+
with gr.Blocks() as app:
|
48 |
+
# Radio buttons to select Consonant or Vowel
|
49 |
+
segment_type = gr.Radio(choices=["Consonant", "Vowel"], label="Select Consonant or Vowel", value="Consonant")
|
50 |
+
# Radio buttons to select description type (Full or Casual)
|
51 |
+
description_type = gr.Radio(choices=["Full_description", "Casual_description"], label="Select Description Type", value="Full_description")
|
52 |
+
|
53 |
+
# Start button placed after the selections
|
54 |
+
start_button = gr.Button("Show a phonetic description")
|
55 |
+
|
56 |
+
description_output = gr.Textbox(label="Description", interactive=False)
|
57 |
+
ipa_input = gr.Textbox(label="Enter IPA", placeholder="Type IPA symbol here")
|
58 |
+
submit_button = gr.Button("Submit", visible=False) # Initially hidden until start is clicked
|
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 |
+
|
91 |
+
app = gradio_app()
|
92 |
+
app.launch()
|