Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -87,7 +87,7 @@ def generate_response(messages, apikey):
|
|
87 |
)
|
88 |
if chat_completion.choices and hasattr(chat_completion.choices[0].message, "content"):
|
89 |
return chat_completion.choices[0].message.content
|
90 |
-
return
|
91 |
|
92 |
|
93 |
@gpu_decorator
|
@@ -96,7 +96,7 @@ def process_audio_input(audio_path, text, apikey, history, conv_state):
|
|
96 |
Process audio and/or text input from the user:
|
97 |
- If an audio file is provided, its transcript is obtained.
|
98 |
- The conversation state and history are updated.
|
99 |
-
|
100 |
Updated to construct the chat history as a list of dictionaries.
|
101 |
"""
|
102 |
if not audio_path and not text.strip():
|
@@ -109,7 +109,7 @@ def process_audio_input(audio_path, text, apikey, history, conv_state):
|
|
109 |
if not text.strip():
|
110 |
return history, conv_state, ""
|
111 |
|
112 |
-
#
|
113 |
user_msg = {"role": "user", "content": text}
|
114 |
conv_state.append(user_msg)
|
115 |
history.append(user_msg)
|
@@ -244,19 +244,19 @@ Have a conversation with an AI using your reference voice!
|
|
244 |
def generate_audio_response(history, ref_audio, ref_text, remove_silence):
|
245 |
"""
|
246 |
Generate an audio response from the last AI message in the conversation.
|
247 |
-
|
248 |
"""
|
249 |
if not history or not ref_audio:
|
250 |
-
return None, ref_text
|
251 |
|
252 |
-
# Find the last message
|
253 |
last_assistant = None
|
254 |
for message in reversed(history):
|
255 |
if message.get("role") == "assistant":
|
256 |
last_assistant = message
|
257 |
break
|
258 |
if last_assistant is None or not last_assistant.get("content", "").strip():
|
259 |
-
return None, ref_text
|
260 |
|
261 |
audio_result, _, ref_text_out = infer(
|
262 |
ref_audio,
|
@@ -267,7 +267,7 @@ Have a conversation with an AI using your reference voice!
|
|
267 |
speed=1.0,
|
268 |
show_info=print,
|
269 |
)
|
270 |
-
return audio_result, ref_text_out
|
271 |
|
272 |
def clear_conversation():
|
273 |
"""
|
@@ -299,7 +299,7 @@ Have a conversation with an AI using your reference voice!
|
|
299 |
).then(
|
300 |
generate_audio_response,
|
301 |
inputs=[chatbot_interface, ref_audio_chat, ref_text_chat, remove_silence_chat],
|
302 |
-
outputs=[audio_output_chat, ref_text_chat],
|
303 |
).then(lambda: None, None, audio_input_chat)
|
304 |
|
305 |
text_input_chat.submit(
|
@@ -309,7 +309,7 @@ Have a conversation with an AI using your reference voice!
|
|
309 |
).then(
|
310 |
generate_audio_response,
|
311 |
inputs=[chatbot_interface, ref_audio_chat, ref_text_chat, remove_silence_chat],
|
312 |
-
outputs=[audio_output_chat, ref_text_chat],
|
313 |
).then(lambda: None, None, text_input_chat)
|
314 |
|
315 |
send_btn_chat.click(
|
@@ -319,7 +319,7 @@ Have a conversation with an AI using your reference voice!
|
|
319 |
).then(
|
320 |
generate_audio_response,
|
321 |
inputs=[chatbot_interface, ref_audio_chat, ref_text_chat, remove_silence_chat],
|
322 |
-
outputs=[audio_output_chat, ref_text_chat],
|
323 |
).then(lambda: None, None, text_input_chat)
|
324 |
|
325 |
clear_btn_chat.click(clear_conversation, outputs=[chatbot_interface, conversation_state])
|
|
|
87 |
)
|
88 |
if chat_completion.choices and hasattr(chat_completion.choices[0].message, "content"):
|
89 |
return chat_completion.choices[0].message.content
|
90 |
+
return ""
|
91 |
|
92 |
|
93 |
@gpu_decorator
|
|
|
96 |
Process audio and/or text input from the user:
|
97 |
- If an audio file is provided, its transcript is obtained.
|
98 |
- The conversation state and history are updated.
|
99 |
+
|
100 |
Updated to construct the chat history as a list of dictionaries.
|
101 |
"""
|
102 |
if not audio_path and not text.strip():
|
|
|
109 |
if not text.strip():
|
110 |
return history, conv_state, ""
|
111 |
|
112 |
+
# Wrap the user input in a dict.
|
113 |
user_msg = {"role": "user", "content": text}
|
114 |
conv_state.append(user_msg)
|
115 |
history.append(user_msg)
|
|
|
244 |
def generate_audio_response(history, ref_audio, ref_text, remove_silence):
|
245 |
"""
|
246 |
Generate an audio response from the last AI message in the conversation.
|
247 |
+
Returns the generated audio, the (possibly updated) reference text, and the unchanged chat history.
|
248 |
"""
|
249 |
if not history or not ref_audio:
|
250 |
+
return None, ref_text, history
|
251 |
|
252 |
+
# Find the last assistant message in the history.
|
253 |
last_assistant = None
|
254 |
for message in reversed(history):
|
255 |
if message.get("role") == "assistant":
|
256 |
last_assistant = message
|
257 |
break
|
258 |
if last_assistant is None or not last_assistant.get("content", "").strip():
|
259 |
+
return None, ref_text, history
|
260 |
|
261 |
audio_result, _, ref_text_out = infer(
|
262 |
ref_audio,
|
|
|
267 |
speed=1.0,
|
268 |
show_info=print,
|
269 |
)
|
270 |
+
return audio_result, ref_text_out, history
|
271 |
|
272 |
def clear_conversation():
|
273 |
"""
|
|
|
299 |
).then(
|
300 |
generate_audio_response,
|
301 |
inputs=[chatbot_interface, ref_audio_chat, ref_text_chat, remove_silence_chat],
|
302 |
+
outputs=[audio_output_chat, ref_text_chat, chatbot_interface],
|
303 |
).then(lambda: None, None, audio_input_chat)
|
304 |
|
305 |
text_input_chat.submit(
|
|
|
309 |
).then(
|
310 |
generate_audio_response,
|
311 |
inputs=[chatbot_interface, ref_audio_chat, ref_text_chat, remove_silence_chat],
|
312 |
+
outputs=[audio_output_chat, ref_text_chat, chatbot_interface],
|
313 |
).then(lambda: None, None, text_input_chat)
|
314 |
|
315 |
send_btn_chat.click(
|
|
|
319 |
).then(
|
320 |
generate_audio_response,
|
321 |
inputs=[chatbot_interface, ref_audio_chat, ref_text_chat, remove_silence_chat],
|
322 |
+
outputs=[audio_output_chat, ref_text_chat, chatbot_interface],
|
323 |
).then(lambda: None, None, text_input_chat)
|
324 |
|
325 |
clear_btn_chat.click(clear_conversation, outputs=[chatbot_interface, conversation_state])
|