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 인터페이스 정의 question_interface = gr.Interface( fn=teacher_question, inputs=gr.Textbox(lines=2, placeholder="Enter your question here...", label="Teacher's Question"), outputs="text", title="Teacher's Question Input" ) record_interface = gr.Interface( fn=record_student_voice, inputs=[gr.Textbox(placeholder="Your name"), gr.Textbox(placeholder="Question"), gr.Audio(type="filepath")], outputs="text", title="Record Your Voice" ) list_interface = gr.Interface( fn=get_recorded_list, inputs=gr.Textbox(placeholder="Question"), outputs=gr.Dropdown(label="Select a recording to play"), title="List Recorded Voices" ) play_interface = gr.Interface( fn=lambda file_path: file_path, inputs=gr.Dropdown(label="Select a recording to play"), outputs=gr.Audio(type="filepath"), title="Play Selected Voice" ) comment_interface = gr.Interface( fn=write_comment, inputs=[gr.Textbox(placeholder="Question"), gr.Textbox(placeholder="Your comment")], outputs="text", title="Write Comment" ) # Gradio 앱 실행 gr.TabbedInterface([question_interface, record_interface, list_interface, play_interface, comment_interface], ["Teacher's Question", "Record Voice", "List Voices", "Play Voice", "Write Comment"]).launch()