File size: 2,187 Bytes
9bb1205 |
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 |
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)
|