import gradio as gr # 전역 변수 recordings = {} # 음성 녹음 및 저장 def record_and_submit_voice(student_name, voice): if not student_name: return "Please enter your name." if student_name not in recordings: recordings[student_name] = [] recordings[student_name].append({"voice": voice, "comments": []}) return f"Voice recorded and submitted successfully by {student_name}!" # 학생 목록 가져오기 def get_student_recordings(): return list(recordings.keys()) # 녹음된 음성 재생 및 댓글 가져오기 def play_recording(selected_student): if selected_student not in recordings: return [], "No recordings found for this student." voices = [rec["voice"] for rec in recordings[selected_student]] comments = "\n".join( [f"{c[0]}: {c[1]}" for rec in recordings[selected_student] for c in rec["comments"]] ) return voices[0] if voices else None, comments # 댓글 작성 def write_comment(selected_student, commenter_name, comment): if not selected_student or selected_student not in recordings: return "Selected student's recording not found." if not commenter_name: return "Please enter your name." if not comment: return "Please enter a comment." recordings[selected_student][0]["comments"].append((commenter_name, comment)) return f"Comment added successfully by {commenter_name}!" # Gradio 인터페이스 정의 with gr.Blocks() as app: with gr.Tab("Record Voice"): student_name_input = gr.Textbox(placeholder="Enter your name", label="Your Name") voice_input = gr.Audio(type="filepath", label="Record your voice") submit_voice_button = gr.Button("Submit Voice") voice_output = gr.Textbox(label="Status") submit_voice_button.click( record_and_submit_voice, inputs=[student_name_input, voice_input], outputs=voice_output ) with gr.Tab("Listen and Comment"): student_selector = gr.Dropdown(choices=get_student_recordings(), label="Select a student to listen", interactive=True) play_voice_button = gr.Button("Play Voice") recording_output = gr.Audio(label="Selected Recording", interactive=False) comment_input = gr.Textbox(placeholder="Write your comment here...", label="Write Comment") commenter_name_input = gr.Textbox(placeholder="Enter your name", label="Your Name") submit_comment_button = gr.Button("Submit Comment") comment_status_output = gr.Textbox(label="Comment Status") comments_display = gr.Textbox(label="Comments", interactive=False) play_voice_button.click( play_recording, inputs=student_selector, outputs=[recording_output, comments_display] ) submit_comment_button.click( write_comment, inputs=[student_selector, commenter_name_input, comment_input], outputs=comment_status_output ) # 인터페이스 실행 app.launch()