import gradio as gr from gtts import gTTS import tempfile # 퀴즈 문제와 선택지를 설정 quiz = { "text": "NUmber 2 : Prosper", "options": ["To fail", "To grow and succeed", "To disappear"], "answer": 1 } # 텍스트를 음성으로 변환하여 임시 파일에 저장 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()