Pijush2023 commited on
Commit
66da79e
·
verified ·
1 Parent(s): 28ed422

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -13
app.py CHANGED
@@ -40,10 +40,7 @@ gpt_vectorstore = PineconeVectorStore(index_name="radardata11122024", embedding=
40
  gpt_retriever = gpt_vectorstore.as_retriever(search_kwargs={'k': 2})
41
 
42
 
43
- async def async_get_retriever_result(query, retriever, k=2):
44
- search_kwargs = {'k': k}
45
- results = await retriever.as_retriever(search_kwargs=search_kwargs)(query)
46
- return results
47
 
48
  # Pinecone setup
49
  from pinecone import Pinecone
@@ -139,15 +136,19 @@ def generate_audio_elevenlabs(text):
139
  import time
140
 
141
  # Main function to handle mode selection with character-by-character streaming
142
- async def async_handle_mode_selection(mode, chat_history, question):
143
  if mode == "Normal Chatbot":
144
- chat_history.append((question, ""))
145
- response = await async_get_retriever_result(question, retriever)
 
 
146
  response_text = response['result']
 
 
147
  for i, char in enumerate(response_text):
148
- chat_history[-1] = (question, chat_history[-1][1] + char)
149
- yield chat_history, "", None
150
- await asyncio.sleep(0.05)
151
 
152
  elif mode == "Voice to Voice Conversation":
153
  response_text = qa_chain({"query": question, "context": ""})['result']
@@ -291,7 +292,7 @@ with gr.Blocks(theme="rawrsor1/Everforest") as demo:
291
 
292
  # Define interactions for the Get Response button
293
  get_response_btn.click(
294
- fn=async_handle_mode_selection,
295
  inputs=[mode_selection, chatbot, question_input],
296
  outputs=[chatbot, question_input, audio_output],
297
  api_name="api_add_message_on_button_click"
@@ -301,7 +302,7 @@ with gr.Blocks(theme="rawrsor1/Everforest") as demo:
301
 
302
 
303
  question_input.submit(
304
- fn=async_handle_mode_selection,
305
  inputs=[mode_selection, chatbot, question_input],
306
  outputs=[chatbot, question_input, audio_output],
307
  api_name="api_add_message_on_enter"
@@ -309,7 +310,7 @@ with gr.Blocks(theme="rawrsor1/Everforest") as demo:
309
 
310
 
311
  submit_voice_btn.click(
312
- fn=async_handle_mode_selection,
313
  inputs=[mode_selection, chatbot, question_input],
314
  outputs=[chatbot, question_input, audio_output],
315
  api_name="api_voice_to_voice_translation"
 
40
  gpt_retriever = gpt_vectorstore.as_retriever(search_kwargs={'k': 2})
41
 
42
 
43
+
 
 
 
44
 
45
  # Pinecone setup
46
  from pinecone import Pinecone
 
136
  import time
137
 
138
  # Main function to handle mode selection with character-by-character streaming
139
+ def handle_mode_selection(mode, chat_history, question):
140
  if mode == "Normal Chatbot":
141
+ chat_history.append((question, "")) # Append user question with an empty response initially
142
+
143
+ # Get response from Pinecone using the qa_chain
144
+ response = qa_chain({"query": question, "context": ""})
145
  response_text = response['result']
146
+
147
+ # Stream each character in the response text to the chat history
148
  for i, char in enumerate(response_text):
149
+ chat_history[-1] = (question, chat_history[-1][1] + char) # Update the last message
150
+ yield chat_history, "", None # Yield updated chat history
151
+ time.sleep(0.05) # Small delay to simulate streaming
152
 
153
  elif mode == "Voice to Voice Conversation":
154
  response_text = qa_chain({"query": question, "context": ""})['result']
 
292
 
293
  # Define interactions for the Get Response button
294
  get_response_btn.click(
295
+ fn=handle_mode_selection,
296
  inputs=[mode_selection, chatbot, question_input],
297
  outputs=[chatbot, question_input, audio_output],
298
  api_name="api_add_message_on_button_click"
 
302
 
303
 
304
  question_input.submit(
305
+ fn=handle_mode_selection,
306
  inputs=[mode_selection, chatbot, question_input],
307
  outputs=[chatbot, question_input, audio_output],
308
  api_name="api_add_message_on_enter"
 
310
 
311
 
312
  submit_voice_btn.click(
313
+ fn=handle_mode_selection,
314
  inputs=[mode_selection, chatbot, question_input],
315
  outputs=[chatbot, question_input, audio_output],
316
  api_name="api_voice_to_voice_translation"