File size: 3,379 Bytes
0a45abf d61536a 0a45abf b625031 0a45abf d61536a 0a45abf b625031 0a45abf d971390 0a45abf d61536a d971390 d61536a 0a45abf d61536a d971390 d61536a d971390 d61536a d971390 d61536a |
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 |
import gradio as gr
from gtts import gTTS
from PIL import Image
import requests
from io import BytesIO
import random
# Manually add definitions
word_definitions = {
"quaint": "Having an old-fashioned or unusual quality or appearance that is usually attractive or appealing.",
"explore": "To travel over or through (a place) in order to learn more about it or to find something.",
"cheerful": "Feeling or showing happiness.",
"peddler": "Someone who sells things in small amounts often by traveling to different places.",
"curious": "Having a desire to learn or know more about something or someone.",
"enthusiasm": "Strong excitement about something; a strong feeling of active interest in something that you like or enjoy.",
"skeptical": "Having or expressing doubt about something.",
"whisper": "To speak very softly or quietly."
}
# Corresponding image URLs
image_urls = {
"quaint": "https://raw.githubusercontent.com/PAMUS-JP30/flashcard/main/001.png",
"explore": "https://raw.githubusercontent.com/PAMUS-JP30/flashcard/main/002.png",
"cheerful": "https://raw.githubusercontent.com/PAMUS-JP30/flashcard/main/003.png",
"peddler": "https://raw.githubusercontent.com/PAMUS-JP30/flashcard/main/004.png",
"curious": "https://raw.githubusercontent.com/PAMUS-JP30/flashcard/main/005.png",
"enthusiasm": "https://raw.githubusercontent.com/PAMUS-JP30/flashcard/main/006.png",
"skeptical": "https://raw.githubusercontent.com/PAMUS-JP30/flashcard/main/007.png",
"whisper": "https://raw.githubusercontent.com/PAMUS-JP30/flashcard/main/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 quiz function
def quiz(definition, selected_word):
correct_word = None
for word, defn in word_definitions.items():
if defn == definition:
correct_word = word
break
if selected_word == correct_word:
return "Correct!"
else:
return "Incorrect, try again."
# Create the Gradio interface
iface = gr.Blocks()
with iface:
gr.Markdown("# Vocabulary Builder with Quiz")
with gr.Tab("Learn Words"):
word_dropdown = gr.Dropdown(choices=list(word_definitions.keys()), label="Choose a word")
img_output = gr.Image(type="pil")
audio_output = gr.Audio(type="filepath", autoplay=True)
learn_button = gr.Button("Learn")
learn_button.click(generate_output, inputs=word_dropdown, outputs=[img_output, audio_output])
with gr.Tab("Quiz"):
definition_text = gr.Dropdown(choices=list(word_definitions.values()), label="Choose the correct word for this definition")
wordbank = gr.Dropdown(choices=list(word_definitions.keys()), label="Wordbank")
quiz_button = gr.Button("Submit")
quiz_result = gr.Textbox(label="Result")
quiz_button.click(quiz, inputs=[definition_text, wordbank], outputs=quiz_result)
iface.launch(debug=True)
|