Update app.py
Browse files
app.py
CHANGED
@@ -19,54 +19,18 @@ from prompt_instructions import get_interview_initial_message
|
|
19 |
temp_mp3_files = []
|
20 |
initial_audio_path = None
|
21 |
|
22 |
-
def chat_function(message, history):
|
23 |
-
global question_count, temp_mp3_files
|
24 |
-
|
25 |
-
# If it's the first message, reset the interview
|
26 |
-
if not history:
|
27 |
-
reset_interview()
|
28 |
-
initial_message = get_interview_initial_message()
|
29 |
-
audio_path = convert_text_to_speech(initial_message, f"initial_{generate_random_string()}.mp3")
|
30 |
-
temp_mp3_files.append(audio_path)
|
31 |
-
return initial_message, audio_path
|
32 |
-
|
33 |
-
question_count += 1
|
34 |
-
print(f"Question count: {question_count}") # Logging
|
35 |
-
|
36 |
-
response, audio = respond(history, message)
|
37 |
-
|
38 |
-
if isinstance(audio, str) and audio.endswith('.mp3'):
|
39 |
-
# Clear previous temporary files
|
40 |
-
for mp3_file in temp_mp3_files:
|
41 |
-
if os.path.exists(mp3_file):
|
42 |
-
os.unlink(mp3_file)
|
43 |
-
print(f"Deleted temporary file: {mp3_file}") # Logging
|
44 |
-
temp_mp3_files.clear()
|
45 |
-
|
46 |
-
# Add new audio file
|
47 |
-
temp_mp3_files.append(audio)
|
48 |
-
print(f"Added new audio file: {audio}") # Logging
|
49 |
-
|
50 |
-
if question_count >= n_of_questions():
|
51 |
-
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!"
|
52 |
-
|
53 |
-
audio_path = convert_text_to_speech(conclusion_message, f"conclusion_{generate_random_string()}.mp3")
|
54 |
-
temp_mp3_files.append(audio_path)
|
55 |
-
|
56 |
-
# Generate report
|
57 |
-
report_content, _ = generate_interview_report(interview_history, language)
|
58 |
-
|
59 |
-
# Clean up temporary files
|
60 |
-
for mp3_file in temp_mp3_files:
|
61 |
-
if os.path.exists(mp3_file):
|
62 |
-
os.unlink(mp3_file)
|
63 |
-
temp_mp3_files.clear()
|
64 |
-
|
65 |
-
return f"{conclusion_message}\n\nInterview Report:\n\n{report_content}", audio_path
|
66 |
-
|
67 |
-
return response, audio
|
68 |
|
|
|
69 |
def create_app():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
with gr.Blocks(title="Clinical Psychologist Interviewer ๐ฟ") as demo:
|
71 |
gr.Markdown(
|
72 |
"""
|
@@ -79,28 +43,86 @@ def create_app():
|
|
79 |
with gr.Tab("Interview"):
|
80 |
audio_output = gr.Audio(
|
81 |
label="Sarah",
|
|
|
|
|
82 |
autoplay=True,
|
83 |
-
visible=
|
84 |
show_download_button=False,
|
85 |
)
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
)
|
97 |
-
chat_interface.load_event(lambda: (chat_function("", []), None))
|
98 |
|
99 |
with gr.Tab("Upload Document"):
|
100 |
file_input = gr.File(label="Upload a TXT, PDF, or DOCX file")
|
101 |
-
language_input = gr.Textbox(label="Preferred Language for Report",
|
|
|
102 |
generate_button = gr.Button("Generate Report")
|
103 |
-
report_output = gr.Textbox(label="Generated Report", lines=
|
104 |
pdf_output = gr.File(label="Download Report", visible=True)
|
105 |
|
106 |
def generate_report_and_pdf(file, language):
|
@@ -115,6 +137,7 @@ def create_app():
|
|
115 |
|
116 |
return demo
|
117 |
|
|
|
118 |
# Clean up function
|
119 |
def cleanup():
|
120 |
global temp_mp3_files, initial_audio_path
|
@@ -126,6 +149,11 @@ def cleanup():
|
|
126 |
if initial_audio_path and os.path.exists(initial_audio_path):
|
127 |
os.unlink(initial_audio_path)
|
128 |
|
|
|
|
|
|
|
|
|
|
|
129 |
if __name__ == "__main__":
|
130 |
app = create_app()
|
131 |
try:
|
|
|
19 |
temp_mp3_files = []
|
20 |
initial_audio_path = None
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
# Initialize Gradio interface
|
24 |
def create_app():
|
25 |
+
global initial_audio_path
|
26 |
+
initial_message = get_interview_initial_message()
|
27 |
+
|
28 |
+
# Generate and save the audio for the initial message in a temporary file
|
29 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_initial_audio:
|
30 |
+
initial_audio_path = temp_initial_audio.name
|
31 |
+
convert_text_to_speech(initial_message, initial_audio_path)
|
32 |
+
temp_mp3_files.append(initial_audio_path)
|
33 |
+
|
34 |
with gr.Blocks(title="Clinical Psychologist Interviewer ๐ฟ") as demo:
|
35 |
gr.Markdown(
|
36 |
"""
|
|
|
43 |
with gr.Tab("Interview"):
|
44 |
audio_output = gr.Audio(
|
45 |
label="Sarah",
|
46 |
+
scale=1,
|
47 |
+
value=initial_audio_path,
|
48 |
autoplay=True,
|
49 |
+
visible=False,
|
50 |
show_download_button=False,
|
51 |
)
|
52 |
+
chatbot = gr.Chatbot(value=[(None, f"{initial_message}")], label=f"Clinical Interview ๐ฟ๐")
|
53 |
+
msg = gr.Textbox(label="Type your message here...")
|
54 |
+
send_button = gr.Button("Send")
|
55 |
+
|
56 |
+
pdf_output = gr.File(label="Download Report", visible=False)
|
57 |
+
|
58 |
+
def user(user_message, history):
|
59 |
+
return "", history + [[user_message, None]]
|
60 |
+
|
61 |
+
def bot_response(chatbot, message):
|
62 |
+
global question_count, temp_mp3_files
|
63 |
+
question_count += 1
|
64 |
+
|
65 |
+
# Use the last user message from the chatbot history
|
66 |
+
last_user_message = chatbot[-1][0] if chatbot else message
|
67 |
+
|
68 |
+
response, audio = respond(chatbot, last_user_message)
|
69 |
+
|
70 |
+
# Add all bot responses to the chatbot history
|
71 |
+
for bot_message in response:
|
72 |
+
chatbot.append((None, bot_message[1]))
|
73 |
+
|
74 |
+
if isinstance(audio, str) and audio.endswith('.mp3'):
|
75 |
+
temp_mp3_files.append(audio)
|
76 |
+
|
77 |
+
if question_count >= n_of_questions():
|
78 |
+
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!"
|
79 |
+
chatbot.append((None, conclusion_message))
|
80 |
+
|
81 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_audio:
|
82 |
+
audio_path = temp_audio.name
|
83 |
+
convert_text_to_speech(conclusion_message, audio_path)
|
84 |
+
audio = audio_path
|
85 |
+
temp_mp3_files.append(audio_path)
|
86 |
+
|
87 |
+
# Generate report automatically
|
88 |
+
report_content, _ = generate_interview_report(interview_history, language)
|
89 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix=".txt", delete=False,
|
90 |
+
encoding='utf-8') as temp_report:
|
91 |
+
temp_report.write(report_content)
|
92 |
+
temp_report_path = temp_report.name
|
93 |
+
|
94 |
+
_, pdf_path = generate_report_from_file(temp_report_path, language)
|
95 |
+
|
96 |
+
# Add report to the chat
|
97 |
+
chatbot.append((None, f"Interview Report:\n\n{report_content}"))
|
98 |
+
|
99 |
+
# Clean up temporary files
|
100 |
+
os.unlink(temp_report_path)
|
101 |
+
|
102 |
+
# Clean up all MP3 files
|
103 |
+
for mp3_file in temp_mp3_files:
|
104 |
+
if os.path.exists(mp3_file):
|
105 |
+
os.unlink(mp3_file)
|
106 |
+
temp_mp3_files.clear()
|
107 |
+
|
108 |
+
return chatbot, audio, gr.File(visible=True, value=pdf_path), gr.Textbox(visible=False)
|
109 |
+
|
110 |
+
return chatbot, audio, gr.File(visible=False), gr.Textbox(visible=True)
|
111 |
+
|
112 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
113 |
+
bot_response, [chatbot, msg], [chatbot, audio_output, pdf_output, msg]
|
114 |
+
)
|
115 |
+
|
116 |
+
send_button.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
117 |
+
bot_response, [chatbot, msg], [chatbot, audio_output, pdf_output, msg]
|
118 |
)
|
|
|
119 |
|
120 |
with gr.Tab("Upload Document"):
|
121 |
file_input = gr.File(label="Upload a TXT, PDF, or DOCX file")
|
122 |
+
language_input = gr.Textbox(label="Preferred Language for Report",
|
123 |
+
placeholder="Enter language")
|
124 |
generate_button = gr.Button("Generate Report")
|
125 |
+
report_output = gr.Textbox(label="Generated Report", lines=100)
|
126 |
pdf_output = gr.File(label="Download Report", visible=True)
|
127 |
|
128 |
def generate_report_and_pdf(file, language):
|
|
|
137 |
|
138 |
return demo
|
139 |
|
140 |
+
|
141 |
# Clean up function
|
142 |
def cleanup():
|
143 |
global temp_mp3_files, initial_audio_path
|
|
|
149 |
if initial_audio_path and os.path.exists(initial_audio_path):
|
150 |
os.unlink(initial_audio_path)
|
151 |
|
152 |
+
def cleanup_audio(audio_path):
|
153 |
+
if audio_path and os.path.exists(audio_path):
|
154 |
+
os.remove(audio_path)
|
155 |
+
print(f"Removed audio file: {audio_path}")
|
156 |
+
|
157 |
if __name__ == "__main__":
|
158 |
app = create_app()
|
159 |
try:
|