File size: 2,608 Bytes
db5efed
 
 
 
 
 
 
 
 
 
 
dd61407
c872a5e
db5efed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c872a5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db5efed
c872a5e
 
 
 
db5efed
 
c872a5e
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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", "F": "Fish"
}

# 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 Animations & Styling
with gr.Blocks(css="""
    body {
        background-color: #f8f9fa;
    }
    .title {
        text-align: center;
        font-size: 2.5rem;
        font-weight: bold;
        color: #333;
        padding-bottom: 10px;
    }
    .sub-title {
        text-align: center;
        font-size: 1.2rem;
        color: #666;
        margin-bottom: 20px;
    }
    .container {
        max-width: 500px;
        margin: auto;
        text-align: center;
    }
    .gradio-image img {
        border-radius: 20px;
        box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.2);
    }
    .gradio-button {
        font-size: 1.2rem;
        padding: 10px;
        border-radius: 15px;
        background: linear-gradient(135deg, #ff7eb3, #ff758c);
        color: white;
        border: none;
        cursor: pointer;
        transition: transform 0.2s ease;
    }
    .gradio-button:hover {
        transform: scale(1.05);
    }
""") as demo:
    gr.Markdown("<div class='title'>πŸ”€ Alphabet Learning Game with Audio</div>")
    

    with gr.Row():
        img_display = gr.Image(label="Letter Image", elem_classes="gradio-image")
    
    text_display = gr.Textbox(label="What you hear", interactive=False)
    audio_output = gr.Audio()

    next_btn = gr.Button("🎡 Next Letter", elem_classes="gradio-button")

    next_btn.click(get_random_image, inputs=[], outputs=[img_display, text_display, audio_output])

demo.launch()