ridfati56's picture
Update app.py
8bf3244 verified
Raw
History Blame Contribute Delete
3.41 kB
import gradio as gr
import random
# Sample learning data
data = {
"Vocabulary": [
{"word": "Hello", "translation": "Hola", "pronunciation": "ho-la"},
{"word": "Thank you", "translation": "Gracias", "pronunciation": "gra-thi-as"},
{"word": "Goodbye", "translation": "Adiós", "pronunciation": "a-dios"},
],
"Grammar": [
{"word": "I am", "translation": "Yo soy", "pronunciation": "yo soy"},
{"word": "You are", "translation": "Tú eres", "pronunciation": "tu eh-res"},
{"word": "We go", "translation": "Nosotros vamos", "pronunciation": "no-so-tros va-mos"},
]
}
current_category = "Vocabulary"
current_index = 0
# Flashcard functions
def show_flashcard(category):
global current_category, current_index
current_category = category
current_index = 0
item = data[category][current_index]
return item["word"], "", ""
def show_answer():
item = data[current_category][current_index]
return item["word"], item["translation"], item["pronunciation"]
def next_card():
global current_index
current_index = (current_index + 1) % len(data[current_category])
item = data[current_category][current_index]
return item["word"], "", ""
# Quiz functions
def generate_question(category):
item = random.choice(data[category])
correct = item["translation"]
options = [correct]
all_translations = [d["translation"] for d in data[category]]
while len(options) < 4 and len(all_translations) > 1:
choice = random.choice(all_translations)
if choice not in options:
options.append(choice)
random.shuffle(options)
return item["word"], options, correct
def check_answer(selected, correct):
if selected == correct:
return "✅ Correct!"
else:
return f"❌ Wrong! Correct answer: {correct}"
# UI
with gr.Blocks() as app:
gr.Markdown("# 🌍 Language Learning App")
category = gr.Dropdown(choices=list(data.keys()), value="Vocabulary", label="Select Category")
gr.Markdown("## 📚 Flashcards")
word = gr.Textbox(label="Word / Phrase", interactive=False)
translation = gr.Textbox(label="Translation", interactive=False)
pronunciation = gr.Textbox(label="Pronunciation", interactive=False)
with gr.Row():
show_btn = gr.Button("Show Answer")
next_btn = gr.Button("Next")
gr.Markdown("## 📝 Quiz")
quiz_word = gr.Textbox(label="Translate this", interactive=False)
options = gr.Radio(choices=[], label="Choose correct answer")
result = gr.Textbox(label="Result", interactive=False)
start_quiz_btn = gr.Button("Start Quiz")
submit_btn = gr.Button("Submit Answer")
correct_answer = gr.State()
# Flashcard actions
category.change(show_flashcard, inputs=category, outputs=[word, translation, pronunciation])
show_btn.click(show_answer, outputs=[word, translation, pronunciation])
next_btn.click(next_card, outputs=[word, translation, pronunciation])
# Quiz actions
start_quiz_btn.click(
generate_question,
inputs=category,
outputs=[quiz_word, options, correct_answer]
)
submit_btn.click(
check_answer,
inputs=[options, correct_answer],
outputs=result
)
# Load first card
app.load(show_flashcard, inputs=category, outputs=[word, translation, pronunciation])
app.launch()