Redmind commited on
Commit
a399790
·
verified ·
1 Parent(s): 47c982e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -17
app.py CHANGED
@@ -51,11 +51,13 @@ load_dotenv()
51
  # langfuse analytics
52
  from langfuse.callback import CallbackHandler
53
 
54
- import multiprocessing
55
 
56
- # Define global variables to manage the process and queue
57
- process = None
58
- queue = None
 
 
59
 
60
  # LangFuse API keys and host settings
61
  os.environ["LANGFUSE_PUBLIC_KEY"] = os.getenv("LANGFUSE_PUBLIC_KEY")
@@ -554,20 +556,49 @@ def clean_gradio_tmp_dir():
554
  max_iterations = 5
555
  iterations = 0
556
 
 
 
 
 
 
557
 
558
- def answer_question(user_question, chatbot, audio=None):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
559
 
560
- #initialize the process
 
561
  """
562
- Starts the agent in a separate process.
563
  """
564
- global process
 
 
 
 
 
565
 
566
- # Create a multiprocessing queue to get the result back from the process
567
- queue = multiprocessing.Queue()
568
-
569
- process.start()
570
-
571
  global iterations
572
  iterations = 0
573
  # Ensure the temporary chart directory exists
@@ -777,9 +808,9 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
777
  with gr.Column(scale=1):
778
  with gr.Row():
779
  button = gr.Button("Submit", elem_id="submit",elem_classes="gr-button")
780
- # Button to stop the agent process
781
- stop_button = gr.Button("Stop Agent")
782
- stop_button.click(stop_agent, [])
783
 
784
  button.click(answer_question, [message, chatbot], [chatbot])
785
  message.submit(answer_question, [message, chatbot], [chatbot])
@@ -790,7 +821,8 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
790
  submit_feedback_button.click(submit_feedback, [feedback_textbox, chatbot], [chatbot, feedback_textbox, submit_feedback_button])
791
  submit_feedback_button.click(lambda x: gr.update(value=''), [], [feedback_textbox])
792
 
793
- sample_button.click(answer_question, [sample_button, chatbot], [chatbot])
 
794
  sample_button1.click(answer_question, [sample_button1, chatbot], [chatbot])
795
  sample_button2.click(answer_question, [sample_button2, chatbot], [chatbot])
796
  sample_button3.click(answer_question, [sample_button3, chatbot], [chatbot])
 
51
  # langfuse analytics
52
  from langfuse.callback import CallbackHandler
53
 
54
+ import concurrent.futures
55
 
56
+
57
+ # Define global variables for managing the thread and future
58
+ executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
59
+ future = None
60
+ stop_event = concurrent.futures.thread.Event()
61
 
62
  # LangFuse API keys and host settings
63
  os.environ["LANGFUSE_PUBLIC_KEY"] = os.getenv("LANGFUSE_PUBLIC_KEY")
 
556
  max_iterations = 5
557
  iterations = 0
558
 
559
+ def handle_query(user_question, chatbot, audio=None):
560
+ """
561
+ Function to handle the processing of user input with `AgentExecutor.invoke()`.
562
+ """
563
+ global future, stop_event
564
 
565
+ # Clear previous stop event and future
566
+ stop_event.clear()
567
+
568
+ if future and not future.done():
569
+ return "A query is already being processed. Please stop it before starting a new one."
570
+
571
+ # Start the processing in a new thread
572
+ future = executor.submit(answer_question, user_question,chatbot)
573
+
574
+ # Check if the process is done or cancelled
575
+ if future.done():
576
+ if future.cancelled():
577
+ return "Processing was cancelled."
578
+ try:
579
+ result = future.result() # Get the result of the completed future
580
+ return result
581
+ except Exception as e:
582
+ return f"Error occurred: {e}"
583
+ else:
584
+ return "Processing your request. You can stop it if needed."
585
 
586
+
587
+ def stop_processing():
588
  """
589
+ Stops the current processing if it's running.
590
  """
591
+ global future, stop_event
592
+ if future and not future.done():
593
+ stop_event.set() # Signal the process to stop
594
+ future.cancel() # Attempt to cancel the future
595
+ return "Sorry, there is some issue with the query. Please try after some time."
596
+ return "No ongoing processing to stop."
597
 
598
+
599
+ def answer_question(user_question, chatbot, audio=None):
600
+
601
+
 
602
  global iterations
603
  iterations = 0
604
  # Ensure the temporary chart directory exists
 
808
  with gr.Column(scale=1):
809
  with gr.Row():
810
  button = gr.Button("Submit", elem_id="submit",elem_classes="gr-button")
811
+ # Button to stop the current processing
812
+ stop_button = gr.Button("Stop Processing")
813
+ stop_button.click(stop_processing, [],[chatbot])
814
 
815
  button.click(answer_question, [message, chatbot], [chatbot])
816
  message.submit(answer_question, [message, chatbot], [chatbot])
 
821
  submit_feedback_button.click(submit_feedback, [feedback_textbox, chatbot], [chatbot, feedback_textbox, submit_feedback_button])
822
  submit_feedback_button.click(lambda x: gr.update(value=''), [], [feedback_textbox])
823
 
824
+ #sample_button.click(answer_question, [sample_button, chatbot], [chatbot])
825
+ sample_button.click(handle_query, [sample_button, chatbot], [chatbot])
826
  sample_button1.click(answer_question, [sample_button1, chatbot], [chatbot])
827
  sample_button2.click(answer_question, [sample_button2, chatbot], [chatbot])
828
  sample_button3.click(answer_question, [sample_button3, chatbot], [chatbot])