import gradio as gr from gtts import gTTS from PIL import Image import requests from io import BytesIO # Manually add definitions word_definitions = { "vibrant": "Full of energy, colorful", "dismissed": "Sent away or rejected", "depicting": "Showing or representing", "admired": "Regarded with respect.", "anxious": "worried or nervous.", "skeptics": "people who question or doubt.", "achievement": "success.", "thrilled": "excitement and happiness." } # Corresponding image URLs (example URLs, replace with actual GitHub links) image_urls = { "vibrant": "https://github.com/MK316/Spring2024/raw/main/DLTESOL/project/flashcard.001.png", "dismissed": "https://github.com/MK316/Spring2024/raw/main/DLTESOL/project/flashcard.002.png", "depicting": "https://github.com/MK316/Spring2024/raw/main/DLTESOL/project/flashcard.003.png", "admired": "https://github.com/MK316/Spring2024/raw/main/DLTESOL/project/flashcard.004.png", "anxious": "https://github.com/MK316/Spring2024/raw/main/DLTESOL/project/flashcard.005.png", "skeptics": "https://github.com/MK316/Spring2024/raw/main/DLTESOL/project/flashcard.006.png", "achievement": "https://github.com/MK316/Spring2024/raw/main/DLTESOL/project/flashcard.007.png", "thrilled": "https://github.com/MK316/Spring2024/raw/main/DLTESOL/project/flashcard.008.png" } def generate_output(word): definition = word + "." + "It means" + word_definitions[word] # Get the image image_url = image_urls[word] response = requests.get(image_url) img = Image.open(BytesIO(response.content)) img = img.resize((400, 250)) # Resize to half size # Generate the audio tts = gTTS(text=definition, lang='en', tld='co.uk', slow=False) audio_file = f"{word}.mp3" tts.save(audio_file) return img, audio_file # Create the Gradio interface iface = gr.Interface( fn=generate_output, inputs=gr.Dropdown(choices=list(word_definitions.keys()), label="Choose a word"), outputs=[gr.Image(type="pil"), gr.Audio(type="filepath", autoplay=True)], title="Word Definition with Image and Audio" ) # Launch the interface iface.launch(debug=True)