Pijush2023 commited on
Commit
f58d69a
·
verified ·
1 Parent(s): ce40f28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -191,22 +191,6 @@ def generate_audio_elevenlabs(text):
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,10 +213,32 @@ examples = [
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
235
- with gr.Blocks(theme="Pijush2023/pijush_theme_Everforest") as demo:
236
  chatbot = gr.Chatbot([], elem_id="RADAR", bubble_full_width=False)
237
  with gr.Row():
238
  with gr.Column():
@@ -257,7 +263,8 @@ with gr.Blocks(theme="Pijush2023/pijush_theme_Everforest") as demo:
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
 
 
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
  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
+ history.append((message, None)) # Add the user's message to the chat history
219
+ return history, gr.Textbox(value="", interactive=True, show_label=False) # Clear the input box
220
+
221
+
222
+ # Define the function to handle the conversation and get the response in a streaming way
223
+ def chat_with_bot(history, user_message):
224
+ # Add user message to the chat history
225
+ history.append((user_message, ""))
226
+
227
+ # Generate the response in a streaming manner
228
+ response = get_response(user_message)
229
+
230
+ # Simulate streaming by yielding each character of the response
231
+ for character in response:
232
+ history[-1] = (user_message, history[-1][1] + character)
233
+ yield history # Stream each character
234
+ time.sleep(0.05) # Adjust delay as needed for real-time effect
235
+
236
+ yield history # Final yield to ensure full response is displayed
237
+
238
 
239
 
240
  # Create the Gradio Blocks interface
241
+ with gr.Blocks() as demo:
242
  chatbot = gr.Chatbot([], elem_id="RADAR", bubble_full_width=False)
243
  with gr.Row():
244
  with gr.Column():
 
263
  gr.Examples(examples=examples, fn=insert_prompt, inputs=question_input, outputs=question_input)
264
 
265
  # Define interactions
266
+ get_response_btn.click(fn=add_message, inputs=[chatbot, question_input], outputs=[chatbot, question_input])\
267
+ .then(fn=chat_with_bot, inputs=[chatbot, question_input], outputs=chatbot)
268
  generate_audio_btn.click(fn=generate_audio_from_last_response, inputs=chatbot, outputs=audio_output)
269
  clean_btn.click(fn=clear_fields, inputs=[], outputs=[chatbot, question_input, audio_output])
270