import gradio as gr import tempfile import os from pathlib import Path from settings import ( respond, generate_random_string, reset_interview, generate_interview_report, generate_report_from_file, interview_history, question_count, language, ) from ai_config import convert_text_to_speech, n_of_questions from prompt_instructions import get_interview_initial_message # Global variables temp_mp3_files = [] initial_audio_path = None # Initialize Gradio interface def create_app(): global initial_audio_path initial_message = get_interview_initial_message() # Generate and save the audio for the initial message in a temporary file with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_initial_audio: initial_audio_path = temp_initial_audio.name convert_text_to_speech(initial_message, initial_audio_path) temp_mp3_files.append(initial_audio_path) with gr.Blocks(title="Clinical Psychologist Interviewer 𝚿") as demo: gr.Markdown( """ # Clinical Psychologist Interviewer 𝚿 This chatbot conducts clinical interviews based on psychological knowledge. Please note that this is a simulation and should not be used as a substitute for professional medical advice. """ ) with gr.Tab("Interview"): audio_output = gr.Audio( label="Sarah", scale=1, value=initial_audio_path, autoplay=True, visible=False, show_download_button=False, ) chatbot = gr.Chatbot(value=[("", f"{initial_message}")], label=f"Clinical Interview πšΏπŸ“‹") msg = gr.Textbox(label="Type your message here...") send_button = gr.Button("Send") pdf_output = gr.File(label="Download Report", visible=False) def user(user_message, history): if not isinstance(history, list): history = [] return "", history + [[user_message, None]] def bot_response(chatbot, message): global question_count, temp_mp3_files question_count += 1 response, audio = respond(chatbot, message) if isinstance(audio, str) and audio.endswith('.mp3'): temp_mp3_files.append(audio) if question_count >= n_of_questions(): conclusion_message = "Thank you for participating in this interview. We have reached the end of our session. I hope this conversation has been helpful. Take care!" response.append((message, conclusion_message)) with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_audio: audio_path = temp_audio.name convert_text_to_speech(conclusion_message, audio_path) audio = audio_path temp_mp3_files.append(audio_path) # Generate report automatically report_content, _ = generate_interview_report(interview_history, language) with tempfile.NamedTemporaryFile(mode='w', suffix=".txt", delete=False, encoding='utf-8') as temp_report: temp_report.write(report_content) temp_report_path = temp_report.name _, pdf_path = generate_report_from_file(temp_report_path, language) # Add report to the chat response.append(("", f"Interview Report:\n\n{report_content}")) # Clean up temporary files os.unlink(temp_report_path) # Clean up all MP3 files for mp3_file in temp_mp3_files: if os.path.exists(mp3_file): os.unlink(mp3_file) temp_mp3_files.clear() return response, audio, gr.File(visible=True, value=pdf_path), gr.Textbox(visible=False) return response, audio, gr.File(visible=False), gr.Textbox(visible=True) msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then( bot_response, [chatbot, msg], [chatbot, audio_output, pdf_output, msg] ) send_button.click(user, [msg, chatbot], [msg, chatbot], queue=False).then( bot_response, [chatbot, msg], [chatbot, audio_output, pdf_output, msg] ) with gr.Tab("Upload Document"): file_input = gr.File(label="Upload a TXT, PDF, or DOCX file") language_input = gr.Textbox(label="Preferred Language for Report", placeholder="Enter language") generate_button = gr.Button("Generate Report") report_output = gr.Textbox(label="Generated Report", lines=100) pdf_output = gr.File(label="Download Report", visible=True) def generate_report_and_pdf(file, language): report_content, pdf_path = generate_report_from_file(file, language) return report_content, pdf_path, gr.File(visible=True) generate_button.click( generate_report_and_pdf, inputs=[file_input, language_input], outputs=[report_output, pdf_output, pdf_output] ) return demo # Clean up function def cleanup(): global temp_mp3_files, initial_audio_path for mp3_file in temp_mp3_files: if os.path.exists(mp3_file): os.unlink(mp3_file) temp_mp3_files.clear() if initial_audio_path and os.path.exists(initial_audio_path): os.unlink(initial_audio_path) if __name__ == "__main__": app = create_app() try: app.launch() finally: cleanup()