Redmind commited on
Commit
c7f5f5d
·
verified ·
1 Parent(s): b0eaf5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -1
app.py CHANGED
@@ -596,7 +596,7 @@ def handle_query(user_question, chatbot, audio=None):
596
 
597
  """
598
  Function to handle the processing of user input with `AgentExecutor.invoke()`.
599
- """
600
  global current_event, stop_event
601
 
602
  # Clear previous stop event and current_event
@@ -639,7 +639,47 @@ def handle_query(user_question, chatbot, audio=None):
639
  print(f"Error occurred: {e}")
640
  chatbot.append((user_question, "Sorry, we encountered an error while processing your request. Please try after some time."))
641
  return gr.update(value=chatbot)
 
 
642
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
643
 
644
  def stop_processing(chatbot):
645
  """
 
596
 
597
  """
598
  Function to handle the processing of user input with `AgentExecutor.invoke()`.
599
+
600
  global current_event, stop_event
601
 
602
  # Clear previous stop event and current_event
 
639
  print(f"Error occurred: {e}")
640
  chatbot.append((user_question, "Sorry, we encountered an error while processing your request. Please try after some time."))
641
  return gr.update(value=chatbot)
642
+ """
643
+ global current_event, stop_event
644
 
645
+ # Clear the stop event to ensure we start fresh
646
+ stop_event.clear()
647
+
648
+ # Check if a task is already running
649
+ if current_event and not current_event.done():
650
+ chatbot.append(("", "A query is already being processed. Please stop it before starting a new one."))
651
+ return gr.update(value=chatbot)
652
+
653
+ # Start the processing in a new thread
654
+ current_event = executor.submit(answer_question_thread, user_question, chatbot)
655
+
656
+ # Periodically check if current_event is done
657
+ try:
658
+ while not current_event.done():
659
+ if stop_event.is_set():
660
+ # Attempt to cancel the current_event
661
+ if current_event.cancel():
662
+ chatbot.append((user_question, "Processing was stopped by the user."))
663
+ else:
664
+ chatbot.append((user_question, "Unable to stop processing. Please try again."))
665
+
666
+ # Return the updated chatbot and stop further processing
667
+ return gr.update(value=chatbot)
668
+
669
+ time.sleep(1) # Wait for 1 second before checking again
670
+
671
+ # Task completed successfully; fetch the result
672
+ try:
673
+ user_question1, response_text1 = current_event.result()
674
+ chatbot.append((user_question1, response_text1))
675
+ return gr.update(value=chatbot)
676
+ except Exception as e:
677
+ print(f"Error occurred: {e}")
678
+ chatbot.append((user_question, "An error occurred while processing your request. Please try again later."))
679
+ return gr.update(value=chatbot)
680
+ finally:
681
+ # Clean up after processing
682
+ current_event = None
683
 
684
  def stop_processing(chatbot):
685
  """