Spaces:
Sleeping
Sleeping
import gradio as gr | |
from gtts import gTTS | |
import tempfile | |
# ν΄μ¦ λ¬Έμ μ μ νμ§λ₯Ό μ€μ | |
quiz = { | |
"text": "NUmber 1 : What did Tom buy from the peddler?", | |
"options": ["A toy", "Pots and pans", "A cloth", "A packet of magic seeds"], | |
"answer": 3 | |
} | |
# ν μ€νΈλ₯Ό μμ±μΌλ‘ λ³ννμ¬ μμ νμΌμ μ μ₯ | |
def text_to_speech(text): | |
tts = gTTS(text) | |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") | |
tts.save(temp_file.name) | |
return temp_file.name | |
# ν΄μ¦ ν¨μλ₯Ό μ μν©λλ€ | |
def quiz_function(selected_option): | |
correct_index = quiz["answer"] | |
selected_index = quiz["options"].index(selected_option) | |
if selected_index == correct_index: | |
return "μ λ΅μ λλ€!" | |
else: | |
return "μ€λ΅μ λλ€. λ€μ μλν΄λ³΄μΈμ." | |
# μ€λμ€ νμΌ μμ± | |
audio_path = text_to_speech(quiz["text"]) | |
# Gradio μΈν°νμ΄μ€λ₯Ό μ€μ ν©λλ€ | |
with gr.Blocks() as demo: | |
gr.Markdown("### Listening Test") | |
audio = gr.Audio(value=audio_path, autoplay=False, label="μ€λμ€λ₯Ό λ€μ΄λ³΄μΈμ") | |
options = gr.Radio(choices=quiz["options"], label="") | |
submit = gr.Button("μ μΆ") | |
result = gr.Textbox(label="κ²°κ³Ό") | |
submit.click(fn=quiz_function, inputs=options, outputs=result) | |
# μΈν°νμ΄μ€λ₯Ό μ€νν©λλ€ | |
demo.launch() | |