Pijush2023 commited on
Commit
a51d4fa
·
verified ·
1 Parent(s): e77602c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -6
app.py CHANGED
@@ -201,6 +201,14 @@ def generate_audio_elevenlabs(text):
201
  logging.error(f"Error generating audio: {response.text}")
202
  return None
203
 
 
 
 
 
 
 
 
 
204
  # Function to add a user's message to the chat history and clear the input box
205
  def add_message(history, message):
206
  if message.strip():
@@ -322,9 +330,15 @@ with gr.Blocks(theme="rawrsor1/Everforest") as demo:
322
  chatbot = gr.Chatbot([], elem_id="RADAR", bubble_full_width=False)
323
  with gr.Row():
324
  with gr.Column():
 
 
 
 
 
 
325
  question_input = gr.Textbox(label="Ask a Question", placeholder="Type your question here...")
326
  audio_input = gr.Audio(sources=["microphone"],streaming=True,type='numpy',every=0.1,label="Speak to Ask")
327
-
328
 
329
 
330
  with gr.Column():
@@ -347,11 +361,34 @@ with gr.Blocks(theme="rawrsor1/Everforest") as demo:
347
 
348
  # Define interactions
349
  # Define interactions for clicking the button
350
- get_response_btn.click(fn=add_message, inputs=[chatbot, question_input], outputs=[chatbot, question_input],api_name="api_add_message_on_button_click")\
351
- .then(fn=chat_with_bot, inputs=[chatbot], outputs=chatbot,api_name="api_get response_on_button")
352
- # Define interaction for hitting the Enter key
353
- question_input.submit(fn=add_message, inputs=[chatbot, question_input], outputs=[chatbot, question_input],api_name="api_add_message_on _enter")\
354
- .then(fn=chat_with_bot, inputs=[chatbot], outputs=chatbot,api_name="api_get response_on_enter")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355
 
356
  # Speech-to-Text functionality
357
  state = gr.State()
 
201
  logging.error(f"Error generating audio: {response.text}")
202
  return None
203
 
204
+ def handle_mode_selection(mode, chat_history, question):
205
+ if mode == "Normal Chatbot":
206
+ response = get_response(question)
207
+ return add_message(chat_history, question), "", None
208
+ elif mode == "Voice to Voice Conversation":
209
+ audio_path = generate_audio_elevenlabs(question)
210
+ return chat_history, "", audio_path
211
+
212
  # Function to add a user's message to the chat history and clear the input box
213
  def add_message(history, message):
214
  if message.strip():
 
330
  chatbot = gr.Chatbot([], elem_id="RADAR", bubble_full_width=False)
331
  with gr.Row():
332
  with gr.Column():
333
+ mode_selection = gr.Radio(
334
+ choices=["Normal Chatbot", "Voice to Voice Conversation"],
335
+ label="Mode Selection",
336
+ value="Normal Chatbot"
337
+ )
338
+ with gr.Row():
339
  question_input = gr.Textbox(label="Ask a Question", placeholder="Type your question here...")
340
  audio_input = gr.Audio(sources=["microphone"],streaming=True,type='numpy',every=0.1,label="Speak to Ask")
341
+ submit_voice_btn = gr.Button("Submit Voice")
342
 
343
 
344
  with gr.Column():
 
361
 
362
  # Define interactions
363
  # Define interactions for clicking the button
364
+ #get_response_btn.click(fn=add_message, inputs=[chatbot, question_input], outputs=[chatbot, question_input],api_name="api_add_message_on_button_click")\
365
+ # .then(fn=chat_with_bot, inputs=[chatbot], outputs=chatbot,api_name="api_get response_on_button")
366
+
367
+
368
+ # Define interaction for hitting the Enter key
369
+ #question_input.submit(fn=add_message, inputs=[chatbot, question_input], outputs=[chatbot, question_input],api_name="api_add_message_on _enter")\
370
+ #.then(fn=chat_with_bot, inputs=[chatbot], outputs=chatbot,api_name="api_get response_on_enter")
371
+
372
+ get_response_btn.click(
373
+ fn=handle_mode_selection,
374
+ inputs=[mode_selection, chatbot, question_input],
375
+ outputs=[chatbot, question_input, audio_output],
376
+ api_name="api_handle_response"
377
+ )
378
+
379
+ question_input.submit(
380
+ fn=handle_mode_selection,
381
+ inputs=[mode_selection, chatbot, question_input],
382
+ outputs=[chatbot, question_input, audio_output],
383
+ api_name="api_handle_response_on_enter"
384
+ )
385
+
386
+ submit_voice_btn.click(
387
+ fn=handle_mode_selection,
388
+ inputs=[mode_selection, chatbot, question_input],
389
+ outputs=[chatbot, question_input, audio_output],
390
+ api_name="api_handle_voice_mode"
391
+ )
392
 
393
  # Speech-to-Text functionality
394
  state = gr.State()