JM0410's picture
Create app.py
28ed227 verified
raw
history blame
1.34 kB
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()