pythontech9's picture
Create app.py
db5efed verified
raw
history blame
1.59 kB
import os
import random
import gradio as gr
from gtts import gTTS
import tempfile
# Path to images folder
IMAGE_FOLDER = "IMAGES/"
# Dictionary of words associated with each letter
letter_words = {
"A": "Apple", "B": "Bat", "C": "Cat", "D": "Duck", "E": "Eyes"
}
# Load all alphabet images into a dictionary {Letter: Image Path}
alphabet_data = {f.split(".")[0].upper(): os.path.join(IMAGE_FOLDER, f)
for f in os.listdir(IMAGE_FOLDER) if f.endswith(('.png', '.jpg', '.jpeg'))}
# Function to get a random image and its associated word
def get_random_image():
letter = random.choice(list(alphabet_data.keys()))
image_path = alphabet_data[letter]
word = letter_words.get(letter, "Unknown") # Get the word (default to "Unknown" if not found)
# Generate speech (A for Apple)
sentence = f"{letter} for {word}"
tts = gTTS(sentence, lang="en")
# Save the audio to a temporary file
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
tts.save(temp_audio.name)
return image_path, sentence, temp_audio.name # Returns image, sentence, and audio file path
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# πŸ”€ Alphabet Learning Game with Audio")
gr.Markdown("See the image, hear the pronunciation, and learn!")
img_display = gr.Image()
text_display = gr.Textbox(label="What you hear")
audio_output = gr.Audio()
next_btn = gr.Button("Next Letter")
next_btn.click(get_random_image, inputs=[], outputs=[img_display, text_display, audio_output])
demo.launch()