import gradio as gr import random import json from gtts import gTTS import os # Load vocabulary from JSON file VOCAB_FILE = "vocabulary_list.json" def load_vocabulary(): if not os.path.exists(VOCAB_FILE): return [ {"word": "apple", "meaning": "사과", "example": "I eat an apple every morning.", "example_meaning": "나는 매일 아침 사과를 먹는다."}, {"word": "book", "meaning": "책", "example": "She borrowed a book from the library.", "example_meaning": "그녀는 도서관에서 책을 빌렸다."} ] with open(VOCAB_FILE, "r", encoding="utf-8") as file: return json.load(file) vocabulary = load_vocabulary() # Function to generate speech using gTTS def speak_text(text, accent): lang = "en" # Default language for gTTS if accent == "British": tts = gTTS(text=text, lang=lang, tld="co.uk") # Use UK domain for British accent else: tts = gTTS(text=text, lang=lang, tld="com") # Use US domain for American accent file_path = f"temp_audio_{accent}.mp3" tts.save(file_path) return file_path # Function to get a random word def get_random_word(): return random.choice(vocabulary) def update_word(): new_word = get_random_word() return new_word["word"], new_word["meaning"], new_word["example"], new_word["example_meaning"] def play_pronunciation(word, example): british_audio = speak_text(word + ", " + example, "British") american_audio = speak_text(word + ", " + example, "American") return british_audio, american_audio with gr.Blocks(css="body { background-color: #f9f5f2; font-family: Arial, sans-serif; } .gradio-container { max-width: 600px; margin: auto; border-radius: 15px; padding: 20px; background: #fff3e6; box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);}") as app: gr.Markdown("## 🌿 잉글리씨 고등필수어휘 2501-4400 🌿") word_display = gr.Textbox(label="영어 단어") meaning_display = gr.Textbox(label="한글 뜻") example_display = gr.Textbox(label="예문") example_meaning_display = gr.Textbox(label="예문 해석") pronunciation_button = gr.Button("🔊 발음 듣기", elem_id="speak-btn") next_button = gr.Button("➡️ 다음 단어", elem_id="next-btn") reset_button = gr.Button("🔄 처음으로", elem_id="reset-btn") british_audio_output = gr.Audio(label="🎧 영국 발음") american_audio_output = gr.Audio(label="🎧 미국 발음") # 초기 단어 설정 initial_word, initial_meaning, initial_example, initial_example_meaning = update_word() word_display.value = initial_word meaning_display.value = initial_meaning example_display.value = initial_example example_meaning_display.value = initial_example_meaning pronunciation_button.click( play_pronunciation, inputs=[word_display, example_display], outputs=[british_audio_output, american_audio_output] ) next_button.click(update_word, outputs=[word_display, meaning_display, example_display, example_meaning_display]) reset_button.click(update_word, outputs=[word_display, meaning_display, example_display, example_meaning_display]) app.launch()