import gradio as gr import os # 저장된 음성 파일과 코멘트를 관리하는 딕셔너리 recordings = {} comments = {} # 선생님의 질문 입력 받기 def teacher_question(question): return question # 학생들의 음성 녹음 및 저장 def record_student_voice(student_name, question, voice): file_path = f"recordings/{student_name}_{question}.wav" os.makedirs("recordings", exist_ok=True) with open(file_path, "wb") as f: f.write(voice) if question in recordings: recordings[question].append(file_path) else: recordings[question] = [file_path] return "Voice recorded successfully!" # 저장된 음성 목록 제공 def get_recorded_list(question): if question in recordings: return recordings[question] else: return [] # 음성에 대한 코멘트 작성 def write_comment(question, comment): if question in comments: comments[question].append(comment) else: comments[question] = [comment] return f"Comment '{comment}' added successfully for question '{question}'." # Gradio 인터페이스 정의 with gr.Blocks() as demo: state_question = gr.State("") with gr.Tab("Teacher's Question"): question_input = gr.Textbox(lines=2, placeholder="Enter your question here...", label="Teacher's Question") submit_question = gr.Button("Submit") output_question = gr.Textbox(label="Submitted Question") submit_question.click(teacher_question, inputs=question_input, outputs=[output_question, state_question]) with gr.Tab("Record Voice"): student_name_input = gr.Textbox(placeholder="Your name", label="Your name") question_display = gr.Textbox(label="Question", interactive=False) voice_input = gr.Audio(type="file", label="Record your voice") submit_voice = gr.Button("Submit Voice") output_voice = gr.Textbox(label="Status") submit_voice.click(record_student_voice, inputs=[student_name_input, state_question, voice_input], outputs=output_voice) demo.load(lambda question: gr.update(value=question), inputs=state_question, outputs=question_display) with gr.Tab("List Voices"): question_list_input = gr.Textbox(placeholder="Question", label="Enter question to list recordings") submit_list = gr.Button("Get Recordings") recordings_list = gr.Dropdown(label="Select a recording to play") submit_list.click(get_recorded_list, inputs=question_list_input, outputs=recordings_list) with gr.Tab("Play Voice"): recording_select = gr.Dropdown(label="Select a recording to play") play_audio = gr.Audio(type="file", label="Recorded Voice") recording_select.change(lambda x: x, inputs=recording_select, outputs=play_audio) with gr.Tab("Write Comment"): comment_question_input = gr.Textbox(placeholder="Question", label="Question") comment_input = gr.Textbox(placeholder="Your comment", label="Comment") submit_comment = gr.Button("Submit Comment") output_comment = gr.Textbox(label="Status") submit_comment.click(write_comment, inputs=[comment_question_input, comment_input], outputs=output_comment) demo.launch()