reab5555 commited on
Commit
d4b9018
ยท
verified ยท
1 Parent(s): a831d89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -55
app.py CHANGED
@@ -14,22 +14,30 @@ from settings import (
14
  language,
15
  )
16
  from ai_config import convert_text_to_speech, transcribe_audio, n_of_questions
17
- from prompt_instructions import get_interview_initial_message
18
 
19
  # Global variables
20
  temp_audio_files = []
21
  initial_audio_path = None
 
22
 
23
-
24
- def reset_interview_action():
25
- global question_count, interview_history
26
  question_count = 0
27
  interview_history.clear()
28
- initial_message = get_interview_initial_message()
29
 
30
- # Generate new audio for the initial message
 
 
 
 
 
 
 
 
31
  initial_audio_buffer = BytesIO()
32
- convert_text_to_speech(initial_message, initial_audio_buffer)
33
  initial_audio_buffer.seek(0)
34
 
35
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
@@ -39,21 +47,17 @@ def reset_interview_action():
39
  temp_audio_files.append(temp_audio_path)
40
 
41
  return (
42
- [(None, initial_message)],
43
- temp_audio_path,
44
- gr.File(visible=False),
45
- gr.Textbox(visible=True),
46
- "Interview reset. You can start a new interview now."
47
  )
48
 
49
- # Initialize Gradio interface
50
  def create_app():
51
- global initial_audio_path
52
- initial_message = get_interview_initial_message()
53
 
54
- # Generate the audio for the initial message and save to a temporary file
55
  initial_audio_buffer = BytesIO()
56
- convert_text_to_speech(initial_message, initial_audio_buffer)
57
  initial_audio_buffer.seek(0)
58
 
59
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
@@ -63,53 +67,53 @@ def create_app():
63
  temp_audio_files.append(initial_audio_path)
64
 
65
  with gr.Blocks(title="Clinical Psychologist Interviewer ๐šฟ") as demo:
66
- gr.Image(value="appendix/icon.jpeg", label='icon', width=20, scale=1, show_label=False, show_download_button=False, show_share_button=False, show_fullscreen_button=False)
 
67
  gr.Markdown(
68
  """
69
  # Clinical Psychologist Interviewer ๐šฟ
70
  This chatbot conducts clinical interviews based on psychological knowledge.
71
  Please note that this is a simulation and should not be used as a substitute for professional medical advice.
72
-
73
  The interviewer will prepare a clinical report based on the interview.
74
  """
75
  )
76
 
77
  with gr.Tab("Interview"):
78
- audio_output = gr.Audio(
79
- label="Sarah",
80
- scale=1,
81
- value=initial_audio_path,
82
- autoplay=True,
83
- visible=True,
84
- show_download_button=False,
85
- )
 
 
 
86
 
87
- reset_button = gr.Button("Reset Interview", size='sm')
88
  chatbot = gr.Chatbot(value=[(None, f"{initial_message}")], label=f"Clinical Interview ๐šฟ๐Ÿ“‹")
89
  with gr.Row():
90
  msg = gr.Textbox(label="Type your message here...", scale=3)
91
- audio_input = gr.Audio(sources=(["microphone"]), label="Record your message", type="filepath", scale=2)
92
  send_button = gr.Button("Send")
93
  pdf_output = gr.File(label="Download Report", visible=False)
94
 
95
  def user(user_message, audio, history):
96
- print(audio)
97
  if audio is not None:
98
  user_message = transcribe_audio(audio)
99
- print(user_message)
100
-
101
  return "", None, history + [[user_message, None]]
102
 
103
- def bot_response(chatbot, message):
104
- global question_count, temp_audio_files
 
105
  question_count += 1
106
 
107
- # Use the last user message from the chatbot history
108
  last_user_message = chatbot[-1][0] if chatbot else message
109
 
110
- response, audio_buffer = respond(chatbot, last_user_message)
 
111
 
112
- # Add all bot responses to the chatbot history
113
  for bot_message in response:
114
  chatbot.append((None, bot_message[1]))
115
 
@@ -118,46 +122,43 @@ def create_app():
118
  temp_audio_path = temp_file.name
119
  temp_file.write(audio_buffer.getvalue())
120
  temp_audio_files.append(temp_audio_path)
121
- audio_output = temp_audio_path
122
  else:
123
- audio_output = audio_buffer
124
 
125
  if question_count >= n_of_questions():
126
  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!"
127
  chatbot.append((None, conclusion_message))
128
 
129
  conclusion_audio_buffer = BytesIO()
130
- convert_text_to_speech(conclusion_message, conclusion_audio_buffer)
131
  conclusion_audio_buffer.seek(0)
132
 
133
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
134
  temp_audio_path = temp_file.name
135
  temp_file.write(conclusion_audio_buffer.getvalue())
136
  temp_audio_files.append(temp_audio_path)
137
- audio_output = temp_audio_path
138
 
139
- # Generate report automatically
140
  report_content, pdf_path = generate_interview_report(interview_history, language)
141
-
142
- # Add report to the chat
143
  chatbot.append((None, f"Interview Report:\n\n{report_content}"))
144
 
145
- return chatbot, audio_output, gr.File(visible=True, value=pdf_path), gr.Textbox(visible=False)
146
 
147
- return chatbot, audio_output, gr.File(visible=False), gr.Textbox(visible=True)
148
 
149
  msg.submit(user, [msg, audio_input, chatbot], [msg, audio_input, chatbot], queue=False).then(
150
- bot_response, [chatbot, msg], [chatbot, audio_output, pdf_output, msg]
151
  )
152
 
153
  send_button.click(user, [msg, audio_input, chatbot], [msg, audio_input, chatbot], queue=False).then(
154
- bot_response, [chatbot, msg], [chatbot, audio_output, pdf_output, msg]
155
  )
156
 
157
  reset_button.click(
158
  reset_interview_action,
159
- inputs=[],
160
- outputs=[chatbot, audio_output, pdf_output, msg, audio_input]
161
  )
162
 
163
  with gr.Tab("Upload Document"):
@@ -178,14 +179,14 @@ def create_app():
178
  outputs=[report_output, pdf_output, pdf_output]
179
  )
180
  with gr.Tab("Description"):
181
- with open('appendix/description.txt', 'r') as file:
182
  description_txt = file.read()
183
  gr.Markdown(description_txt)
184
  gr.HTML("<div style='height: 15px;'></div>")
185
- gr.Image(value="appendix/diagram.png", label='diagram', width=700, scale=1, show_label=False, show_download_button=False, show_share_button=False)
186
-
187
- return demo
188
 
 
189
 
190
  # Clean up function
191
  def cleanup():
@@ -198,7 +199,6 @@ def cleanup():
198
  if initial_audio_path and os.path.exists(initial_audio_path):
199
  os.unlink(initial_audio_path)
200
 
201
-
202
  if __name__ == "__main__":
203
  app = create_app()
204
  try:
 
14
  language,
15
  )
16
  from ai_config import convert_text_to_speech, transcribe_audio, n_of_questions
17
+ from prompt_instructions import get_interview_initial_message_sarah, get_interview_initial_message_aaron
18
 
19
  # Global variables
20
  temp_audio_files = []
21
  initial_audio_path = None
22
+ selected_interviewer = "Sarah"
23
 
24
+ def reset_interview_action(voice):
25
+ global question_count, interview_history, selected_interviewer
26
+ selected_interviewer = voice
27
  question_count = 0
28
  interview_history.clear()
 
29
 
30
+ if voice == "Sarah":
31
+ initial_message = get_interview_initial_message_sarah()
32
+ voice_setting = "alloy"
33
+ else:
34
+ initial_message = get_interview_initial_message_aaron()
35
+ voice_setting = "onyx"
36
+
37
+ initial_message = str(initial_message)
38
+
39
  initial_audio_buffer = BytesIO()
40
+ convert_text_to_speech(initial_message, initial_audio_buffer, voice_setting)
41
  initial_audio_buffer.seek(0)
42
 
43
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
 
47
  temp_audio_files.append(temp_audio_path)
48
 
49
  return (
50
+ [(None, initial_message[0] if isinstance(initial_message, tuple) else initial_message)],
51
+ gr.Audio(value=temp_audio_path, label=voice, autoplay=True),
52
+ gr.Textbox(value="")
 
 
53
  )
54
 
 
55
  def create_app():
56
+ global initial_audio_path, selected_interviewer
57
+ initial_message = get_interview_initial_message_sarah()
58
 
 
59
  initial_audio_buffer = BytesIO()
60
+ convert_text_to_speech(initial_message, initial_audio_buffer, "alloy")
61
  initial_audio_buffer.seek(0)
62
 
63
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
 
67
  temp_audio_files.append(initial_audio_path)
68
 
69
  with gr.Blocks(title="Clinical Psychologist Interviewer ๐šฟ") as demo:
70
+ gr.Image(value="appendix/icon.jpeg", label='icon', width=20, scale=1, show_label=False,
71
+ show_download_button=False, show_share_button=False)
72
  gr.Markdown(
73
  """
74
  # Clinical Psychologist Interviewer ๐šฟ
75
  This chatbot conducts clinical interviews based on psychological knowledge.
76
  Please note that this is a simulation and should not be used as a substitute for professional medical advice.
77
+
78
  The interviewer will prepare a clinical report based on the interview.
79
  """
80
  )
81
 
82
  with gr.Tab("Interview"):
83
+ with gr.Row():
84
+ reset_button = gr.Button("Select Interviewer", size='sm', scale=1, icon='appendix/psi.png')
85
+ voice_radio = gr.Radio(["Sarah", "Aaron"], label="Select Interviewer", value="Sarah", scale=1, info='Each interviewer has a unique approach and a different professional background.')
86
+ audio_output = gr.Audio(
87
+ label="Sarah",
88
+ scale=3,
89
+ value=initial_audio_path,
90
+ autoplay=True,
91
+ visible=True,
92
+ show_download_button=False,
93
+ )
94
 
 
95
  chatbot = gr.Chatbot(value=[(None, f"{initial_message}")], label=f"Clinical Interview ๐šฟ๐Ÿ“‹")
96
  with gr.Row():
97
  msg = gr.Textbox(label="Type your message here...", scale=3)
98
+ audio_input = gr.Audio(sources=(["microphone"]), label="Record your message", type="filepath", scale=1)
99
  send_button = gr.Button("Send")
100
  pdf_output = gr.File(label="Download Report", visible=False)
101
 
102
  def user(user_message, audio, history):
 
103
  if audio is not None:
104
  user_message = transcribe_audio(audio)
 
 
105
  return "", None, history + [[user_message, None]]
106
 
107
+ def bot_response(chatbot, message, voice_selection):
108
+ global question_count, temp_audio_files, selected_interviewer
109
+ selected_interviewer = voice_selection
110
  question_count += 1
111
 
 
112
  last_user_message = chatbot[-1][0] if chatbot else message
113
 
114
+ voice = "alloy" if selected_interviewer == "Sarah" else "onyx"
115
+ response, audio_buffer = respond(chatbot, last_user_message, voice, selected_interviewer)
116
 
 
117
  for bot_message in response:
118
  chatbot.append((None, bot_message[1]))
119
 
 
122
  temp_audio_path = temp_file.name
123
  temp_file.write(audio_buffer.getvalue())
124
  temp_audio_files.append(temp_audio_path)
125
+ audio_output = gr.Audio(value=temp_audio_path, label=voice_selection, autoplay=True)
126
  else:
127
+ audio_output = gr.Audio(value=audio_buffer, label=voice_selection, autoplay=True)
128
 
129
  if question_count >= n_of_questions():
130
  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!"
131
  chatbot.append((None, conclusion_message))
132
 
133
  conclusion_audio_buffer = BytesIO()
134
+ convert_text_to_speech(conclusion_message, conclusion_audio_buffer, voice)
135
  conclusion_audio_buffer.seek(0)
136
 
137
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
138
  temp_audio_path = temp_file.name
139
  temp_file.write(conclusion_audio_buffer.getvalue())
140
  temp_audio_files.append(temp_audio_path)
141
+ audio_output = gr.Audio(value=temp_audio_path, label=voice_selection, autoplay=True)
142
 
 
143
  report_content, pdf_path = generate_interview_report(interview_history, language)
 
 
144
  chatbot.append((None, f"Interview Report:\n\n{report_content}"))
145
 
146
+ return chatbot, audio_output, gr.File(visible=True, value=pdf_path)
147
 
148
+ return chatbot, audio_output, gr.File(visible=False)
149
 
150
  msg.submit(user, [msg, audio_input, chatbot], [msg, audio_input, chatbot], queue=False).then(
151
+ bot_response, [chatbot, msg, voice_radio], [chatbot, audio_output, pdf_output]
152
  )
153
 
154
  send_button.click(user, [msg, audio_input, chatbot], [msg, audio_input, chatbot], queue=False).then(
155
+ bot_response, [chatbot, msg, voice_radio], [chatbot, audio_output, pdf_output]
156
  )
157
 
158
  reset_button.click(
159
  reset_interview_action,
160
+ inputs=[voice_radio],
161
+ outputs=[chatbot, audio_output, msg]
162
  )
163
 
164
  with gr.Tab("Upload Document"):
 
179
  outputs=[report_output, pdf_output, pdf_output]
180
  )
181
  with gr.Tab("Description"):
182
+ with open('appendix/description.txt', 'r', encoding='utf-8') as file:
183
  description_txt = file.read()
184
  gr.Markdown(description_txt)
185
  gr.HTML("<div style='height: 15px;'></div>")
186
+ gr.Image(value="appendix/diagram.png", label='diagram', width=700, scale=1, show_label=False,
187
+ show_download_button=False, show_share_button=False)
 
188
 
189
+ return demo
190
 
191
  # Clean up function
192
  def cleanup():
 
199
  if initial_audio_path and os.path.exists(initial_audio_path):
200
  os.unlink(initial_audio_path)
201
 
 
202
  if __name__ == "__main__":
203
  app = create_app()
204
  try: