Download app.py from pythontech9/ENGLISH-ALPHABET-LEARNING: direct link, hf CLI and curl.
- Browser
- Download file 2.61 kB
-
https://huggingface.co/spaces/pythontech9/ENGLISH-ALPHABET-LEARNING/resolve/main/app.py
- Command line
-
hf download hf://spaces/pythontech9/ENGLISH-ALPHABET-LEARNING/app.py
-
curl -L -o app.py https://huggingface.co/spaces/pythontech9/ENGLISH-ALPHABET-LEARNING/resolve/main/app.py
2.61 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", "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() | |