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)