File size: 1,585 Bytes
db5efed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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()