Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -191,6 +191,22 @@ def generate_audio_elevenlabs(text):
|
|
191 |
logging.error(f"Error generating audio: {response.text}")
|
192 |
return None
|
193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
# Function to generate audio with Eleven Labs TTS from the last bot response
|
195 |
def generate_audio_from_last_response(history):
|
196 |
# Get the most recent bot response from the chat history
|
@@ -213,32 +229,6 @@ examples = [
|
|
213 |
def insert_prompt(current_text, prompt):
|
214 |
return prompt[0] if prompt else current_text
|
215 |
|
216 |
-
# Function to add a user's message to the chat history and clear the input box
|
217 |
-
def add_message(history, message):
|
218 |
-
if message.strip():
|
219 |
-
history.append((message, None)) # Add the user's message to the chat history only if it's not empty
|
220 |
-
return history, gr.Textbox(value="", interactive=True, show_label=False) # Clear the input box
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
# Define the function to handle the conversation and get the response in a streaming way
|
225 |
-
def chat_with_bot(history, user_message):
|
226 |
-
if not user_message.strip():
|
227 |
-
# If the user message is empty or whitespace, add an appropriate error message.
|
228 |
-
history.append(("It seems like your question is missing. Could you please provide more details or ask your question again?", None))
|
229 |
-
yield history
|
230 |
-
return
|
231 |
-
|
232 |
-
# Proceed with adding the user message and generating the bot response
|
233 |
-
history.append((user_message, ""))
|
234 |
-
response = get_response(user_message)
|
235 |
-
|
236 |
-
for character in response:
|
237 |
-
history[-1] = (user_message, history[-1][1] + character)
|
238 |
-
yield history # Stream each character
|
239 |
-
time.sleep(0.05) # Adjust delay as needed
|
240 |
-
|
241 |
-
yield history # Final yield to ensure full response is displayed
|
242 |
|
243 |
|
244 |
# Create the Gradio Blocks interface
|
@@ -267,18 +257,10 @@ with gr.Blocks(theme="rawrsor1/Everforest") as demo:
|
|
267 |
gr.Examples(examples=examples, fn=insert_prompt, inputs=question_input, outputs=question_input)
|
268 |
|
269 |
# Define interactions
|
270 |
-
get_response_btn.click(
|
271 |
-
fn=add_message,
|
272 |
-
inputs=[chatbot, question_input],
|
273 |
-
outputs=[chatbot, question_input]
|
274 |
-
).then(
|
275 |
-
fn=chat_with_bot,
|
276 |
-
inputs=[chatbot, question_input],
|
277 |
-
outputs=chatbot
|
278 |
-
)
|
279 |
-
|
280 |
generate_audio_btn.click(fn=generate_audio_from_last_response, inputs=chatbot, outputs=audio_output)
|
281 |
clean_btn.click(fn=clear_fields, inputs=[], outputs=[chatbot, question_input, audio_output])
|
282 |
|
283 |
# Launch the Gradio interface
|
284 |
demo.launch(show_error=True)
|
|
|
|
191 |
logging.error(f"Error generating audio: {response.text}")
|
192 |
return None
|
193 |
|
194 |
+
|
195 |
+
# Define function to generate a streaming response
|
196 |
+
def chat_with_bot(messages, user_message):
|
197 |
+
# Add user message to the chat history
|
198 |
+
messages.append((user_message, ""))
|
199 |
+
response = get_response(user_message)
|
200 |
+
|
201 |
+
# Simulate streaming response by iterating over each character in the response
|
202 |
+
for character in response:
|
203 |
+
messages[-1] = (user_message, messages[-1][1] + character)
|
204 |
+
yield messages # Stream each character
|
205 |
+
time.sleep(0.05) # Adjust delay as needed for real-time effect
|
206 |
+
|
207 |
+
yield messages # Final yield to ensure full response is displayed
|
208 |
+
|
209 |
+
|
210 |
# Function to generate audio with Eleven Labs TTS from the last bot response
|
211 |
def generate_audio_from_last_response(history):
|
212 |
# Get the most recent bot response from the chat history
|
|
|
229 |
def insert_prompt(current_text, prompt):
|
230 |
return prompt[0] if prompt else current_text
|
231 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
|
233 |
|
234 |
# Create the Gradio Blocks interface
|
|
|
257 |
gr.Examples(examples=examples, fn=insert_prompt, inputs=question_input, outputs=question_input)
|
258 |
|
259 |
# Define interactions
|
260 |
+
get_response_btn.click(fn=chat_with_bot, inputs=[chatbot, question_input], outputs=chatbot)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
generate_audio_btn.click(fn=generate_audio_from_last_response, inputs=chatbot, outputs=audio_output)
|
262 |
clean_btn.click(fn=clear_fields, inputs=[], outputs=[chatbot, question_input, audio_output])
|
263 |
|
264 |
# Launch the Gradio interface
|
265 |
demo.launch(show_error=True)
|
266 |
+
|