Pijush2023 commited on
Commit
0d20df9
·
verified ·
1 Parent(s): 5e00576

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -7
app.py CHANGED
@@ -203,18 +203,33 @@ def generate_audio_elevenlabs(text):
203
 
204
  def handle_mode_selection(mode, chat_history, question):
205
  if mode == "Normal Chatbot":
206
- # Normal chatbot mode: Show the response in the chatbot output
207
- response = get_response(question)
208
- chat_history.append((question, response))
209
- return chat_history, "", None
210
  elif mode == "Voice to Voice Conversation":
211
  # Voice to Voice mode: Generate the response using Eleven Labs and return audio without showing text
212
- response = get_response(question) # Get the response text (can be omitted if not needed for debugging)
213
- audio_path = generate_audio_elevenlabs(response) # Convert the response to audio
214
- chat_history.append((question, "[Voice Response]")) # Log that a voice response was generated (optional)
215
  return chat_history, "", audio_path
216
 
217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
 
219
  # Function to add a user's message to the chat history and clear the input box
220
  def add_message(history, message):
@@ -365,6 +380,7 @@ with gr.Blocks(theme="rawrsor1/Everforest") as demo:
365
  gr.Markdown("<h1 style='color: red;'>Example Prompts</h1>", elem_id="Example-Prompts")
366
  gr.Examples(examples=examples, fn=insert_prompt, inputs=question_input, outputs=question_input, api_name="api_insert_example")
367
 
 
368
  # Define interactions for the Get Response button
369
  get_response_btn.click(
370
  fn=handle_mode_selection,
@@ -379,6 +395,7 @@ with gr.Blocks(theme="rawrsor1/Everforest") as demo:
379
  outputs=[chatbot, question_input, audio_output],
380
  api_name="api_handle_response_on_enter"
381
  )
 
382
 
383
  submit_voice_btn.click(
384
  fn=handle_mode_selection,
 
203
 
204
  def handle_mode_selection(mode, chat_history, question):
205
  if mode == "Normal Chatbot":
206
+ # Add user's message to chat history with a placeholder for the bot's response
207
+ chat_history.append((question, ""))
208
+ return chat_with_streaming(chat_history, question)
 
209
  elif mode == "Voice to Voice Conversation":
210
  # Voice to Voice mode: Generate the response using Eleven Labs and return audio without showing text
211
+ response = get_response(question)
212
+ audio_path = generate_audio_elevenlabs(response)
213
+ chat_history.append((question, "[Voice Response]"))
214
  return chat_history, "", audio_path
215
 
216
 
217
+ def chat_with_streaming(chat_history, question):
218
+ # Get the response for the question
219
+ response = get_response(question)
220
+ user_message = question
221
+
222
+ # Iterate through each character of the response to simulate streaming
223
+ for i in range(1, len(response) + 1):
224
+ # Update the last entry in the chat history with the streaming response
225
+ chat_history[-1] = (user_message, response[:i])
226
+ yield chat_history, "", None # Yield updated chat history and reset input box
227
+
228
+ # Ensure the full response is shown at the end
229
+ yield chat_history, "", None
230
+
231
+
232
+
233
 
234
  # Function to add a user's message to the chat history and clear the input box
235
  def add_message(history, message):
 
380
  gr.Markdown("<h1 style='color: red;'>Example Prompts</h1>", elem_id="Example-Prompts")
381
  gr.Examples(examples=examples, fn=insert_prompt, inputs=question_input, outputs=question_input, api_name="api_insert_example")
382
 
383
+
384
  # Define interactions for the Get Response button
385
  get_response_btn.click(
386
  fn=handle_mode_selection,
 
395
  outputs=[chatbot, question_input, audio_output],
396
  api_name="api_handle_response_on_enter"
397
  )
398
+
399
 
400
  submit_voice_btn.click(
401
  fn=handle_mode_selection,