Spaces:
Sleeping
Sleeping
File size: 3,221 Bytes
5976862 |
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 |
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("## ๐ฟ ์๊ธ๋ฆฌ์จ ๋๋ค ์๋จ์ด 2500 ๐ฟ")
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()
|