Spaces:
Runtime error
Runtime error
import gradio as gr | |
from utils.helpers import load_content | |
from .translation_model import TranslationModel | |
def phonics_component(): | |
content = load_content() | |
translator = TranslationModel() | |
def practice_phonics(sound, user_input): | |
if not user_input.strip(): | |
return "Please enter a word" | |
for level in ["beginner", "intermediate"]: | |
examples = next((p["examples"] for p in content["phonics"][level] | |
if p["sound"] == sound), []) | |
if examples: | |
# Translate user input to English to verify meaning | |
translation = translator.translate(user_input.lower().strip(), "af", "en") | |
# Check if the word contains the sound | |
has_sound = sound.lower() in user_input.lower() | |
# Check if it's a valid Afrikaans word by translating back | |
back_translation = translator.translate(translation, "en", "af") | |
is_valid = back_translation.lower().strip() == user_input.lower().strip() | |
if user_input.lower().strip() in [ex.lower() for ex in examples]: | |
return f"Correct! β '{user_input}' contains the sound '{sound}'" | |
elif has_sound and is_valid: | |
return f"Good job! '{user_input}' is a valid word with the '{sound}' sound" | |
else: | |
return f"Try again. Examples: {', '.join(examples)}" | |
return "Sound not found" | |
with gr.Column() as phonics: | |
for level in ["beginner", "intermediate"]: | |
with gr.Tab(f"{level.capitalize()} Phonics"): | |
for phoneme in content["phonics"][level]: | |
with gr.Accordion(f"{phoneme['sound']} - {phoneme['pronunciation']}", open=False): | |
sound_input = gr.Textbox(value=phoneme["sound"], visible=False) | |
examples = gr.Markdown(", ".join(phoneme["examples"])) | |
user_input = gr.Textbox(label="Type a word with this sound") | |
check_btn = gr.Button("Check") | |
result = gr.Textbox(label="Result") | |
check_btn.click( | |
fn=practice_phonics, | |
inputs=[sound_input, user_input], | |
outputs=result | |
) | |
return phonics |