pythontech9 commited on
Commit
db5efed
·
verified ·
1 Parent(s): 32536d6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ import gradio as gr
4
+ from gtts import gTTS
5
+ import tempfile
6
+
7
+ # Path to images folder
8
+ IMAGE_FOLDER = "IMAGES/"
9
+
10
+ # Dictionary of words associated with each letter
11
+ letter_words = {
12
+ "A": "Apple", "B": "Bat", "C": "Cat", "D": "Duck", "E": "Eyes"
13
+ }
14
+
15
+ # Load all alphabet images into a dictionary {Letter: Image Path}
16
+ alphabet_data = {f.split(".")[0].upper(): os.path.join(IMAGE_FOLDER, f)
17
+ for f in os.listdir(IMAGE_FOLDER) if f.endswith(('.png', '.jpg', '.jpeg'))}
18
+
19
+ # Function to get a random image and its associated word
20
+ def get_random_image():
21
+ letter = random.choice(list(alphabet_data.keys()))
22
+ image_path = alphabet_data[letter]
23
+ word = letter_words.get(letter, "Unknown") # Get the word (default to "Unknown" if not found)
24
+
25
+ # Generate speech (A for Apple)
26
+ sentence = f"{letter} for {word}"
27
+ tts = gTTS(sentence, lang="en")
28
+
29
+ # Save the audio to a temporary file
30
+ temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
31
+ tts.save(temp_audio.name)
32
+
33
+ return image_path, sentence, temp_audio.name # Returns image, sentence, and audio file path
34
+
35
+ # Gradio UI
36
+ with gr.Blocks() as demo:
37
+ gr.Markdown("# 🔤 Alphabet Learning Game with Audio")
38
+ gr.Markdown("See the image, hear the pronunciation, and learn!")
39
+
40
+ img_display = gr.Image()
41
+ text_display = gr.Textbox(label="What you hear")
42
+ audio_output = gr.Audio()
43
+
44
+ next_btn = gr.Button("Next Letter")
45
+
46
+ next_btn.click(get_random_image, inputs=[], outputs=[img_display, text_display, audio_output])
47
+
48
+ demo.launch()