import random import gradio as gr import pandas as pd # Load data from the provided URL def load_data(): url = "https://raw.githubusercontent.com/MK316/Myapps/refs/heads/main/data/Phonetic-description.csv" try: data = pd.read_csv(url, encoding='utf-8') # Ensure correct encoding return data except Exception as e: raise ValueError(f"Error loading data: {e}") # Load the data into a DataFrame df = load_data() # Store session data for each user user_sessions = {} # Function to generate a random question based on user selections def generate_question(segment_type, description_type, user_session): segment = "Consonant" if segment_type == "Consonant" else "Vowel" description_field = "Full_description" if description_type == "Full_description" else "Casual_description" filtered_df = df[df["Segment"] == segment] # Filter based on consonant/vowel selection if filtered_df.empty: return "No data available", "" # Handle empty filter case random_row = filtered_df.sample(1).iloc[0] # Pick a random row description = random_row[description_field] # Select either 'Full_description' or 'Casual_description' correct_ipa = random_row["IPA"] # The correct IPA symbol user_sessions[user_session]['correct_ipa'] = correct_ipa # Save correct IPA in session data return description # Function to check the user's answer def submit_answer(user_ipa, user_session): session = user_sessions[user_session] trials = session['trials'] + 1 session['trials'] = trials if user_ipa.strip() == session['correct_ipa']: score = session['score'] + 1 session['score'] = score return "Correct!", session['score'], session['trials'] else: return "Try again", session['score'], session['trials'] # Function to quit and show results def quit_quiz(user_session): session = user_sessions.pop(user_session, {"score": 0, "trials": 0}) # Default in case session is missing return f"Your final score is {session['score']}/{session['trials']}." # Gradio interface def gradio_app(): with gr.Blocks() as app: # Radio buttons to select Consonant or Vowel segment_type = gr.Radio(choices=["Consonant", "Vowel"], label="Select Consonant or Vowel", value="Consonant") # Radio buttons to select description type (Full or Casual) description_type = gr.Radio(choices=["Full_description", "Casual_description"], label="Select Description Type", value="Full_description") # Start button placed after the selections start_button = gr.Button("Show a phonetic description") description_output = gr.Textbox(label="Description", interactive=False) ipa_input = gr.Textbox(label="Enter IPA", placeholder="Type IPA symbol here") submit_button = gr.Button("Submit", visible=False) # Initially hidden until start is clicked result_output = gr.Textbox(label="Result", interactive=False) quit_button = gr.Button("Quit") # Initialize a unique session ID for each user def init_session(): user_session = random.randint(1000, 9999) # Use random number as session ID user_sessions[user_session] = { "score": 0, "trials": 0, "correct_ipa": "" } return user_session # Start quiz based on selections def start(segment_type, description_type, user_session): description = generate_question(segment_type, description_type, user_session) return description, "", gr.update(visible=True), user_sessions[user_session]['score'], user_sessions[user_session]['trials'] # Submit the answer and check def submit(user_ipa, user_session): result, score, trials = submit_answer(user_ipa, user_session) return result, score, trials # Quit and show final results def quit(user_session): return quit_quiz(user_session) # Bind actions user_session = init_session() # Initialize a new session for each user start_button.click(fn=start, inputs=[segment_type, description_type, gr.State(user_session)], outputs=[description_output, ipa_input, submit_button, gr.State(user_session), gr.State(user_session)]) submit_button.click(fn=submit, inputs=[ipa_input, gr.State(user_session)], outputs=[result_output, gr.State(user_session), gr.State(user_session)]) quit_button.click(fn=quit, inputs=[gr.State(user_session)], outputs=[result_output]) return app app = gradio_app() app.launch()