Spaces:
Sleeping
Sleeping
Update app.py
Browse files310 unmodified lines31131231331431431531631731831932032132231831932032132232532332432732532632733032832933033233133233433333433533633733833934034134234334434534634734834935035135235335433635535635732 unmodified lines39039139237537637737839337938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541639439539617 unmodified lines414415416440417418419310 unmodified lines# -------------------- Process Generator and Human Input --------------------def process_conversation_generator(task_message: str, api_key: str, human_in_the_loop_event: threading.Event, human_input_queue: queue.Queue) -> Generator[str, None, None]: """ Wraps the conversation and yields log messages. Handles human input. Wraps the conversation, yields log messages, and handles human input within a single thread. """ log_q: queue.Queue = queue.Queue()
def run_conversation() -> None: asyncio.run(multi_agent_conversation(task_message, log_q, api_key, human_in_the_loop_event, human_input_queue))
thread = threading.Thread(target=run_conversation) thread.start() # Run the multi-agent conversation *synchronously* within this function. asyncio.run(multi_agent_conversation(task_message, log_q, api_key, human_in_the_loop_event, human_input_queue))
# Process the log queue and handle human-in-the-loop final_result = None while thread.is_alive() or not log_q.empty(): while True: # Loop indefinitely to handle multiple potential human feedback requests. try: msg = log_q.get(timeout=0.1) msg = log_q.get_nowait() # Non-blocking get from the log queue. if isinstance(msg, tuple) and msg[0] == "result": final_result = msg[1] yield "Conversation complete." yield "Conversation complete." # Indicate completion. break # Exit the loop after processing the final result. else: yield msg yield msg # Yield the log message. except queue.Empty: continue pass # No log message available, continue checking for human input.
if human_in_the_loop_event.is_set(): yield "Waiting for human feedback..." # Indicate waiting state. try: feedback_request = human_input_queue.get( timeout=0.1) # Get the context/question for feedback. human_interface = get_human_feedback(feedback_request) yield gr.Textbox.update(visible=False), gr.update(visible=True) human_feedback = human_input_queue.get( timeout=300) # Wait (block) for human feedback, with a timeout. human_in_the_loop_event.clear() # Reset the event after getting feedback. yield gr.Textbox.update(visible=True), human_interface.close() # Hide feedback UI.
except queue.Empty: pass # Add a small sleep to avoid busy-waiting and reduce CPU usage. time.sleep(0.1)
thread.join() if final_result: conv_text = "\n=== Conversation ===\n" for entry in final_result:32 unmodified lines human_in_the_loop_event = threading.Event() human_input_queue = queue.Queue() # Use a single queue for both requests and responses
# Start the conversation in a separate thread conversation_thread = threading.Thread( target=lambda: asyncio.run( multi_agent_conversation(message, queue.Queue(), openai_api_key, human_in_the_loop_event, human_input_queue) yield from process_conversation_generator(message, openai_api_key, human_in_the_loop_event, human_input_queue) ) ) conversation_thread.start()
log_queue = queue.Queue() # Local log queue for this chat instance asyncio.run(multi_agent_conversation(message, log_queue, openai_api_key, human_in_the_loop_event, human_input_queue))
while conversation_thread.is_alive() or not log_queue.empty() or human_in_the_loop_event.is_set(): # Yield log messages try: log_message = log_queue.get_nowait() # Non-blocking get if isinstance(log_message, tuple) and log_message[0] == "result": final_result_text = "\n=== Conversation ===\n" for entry in log_message[1]: final_result_text+= f"[{entry['agent']}]: {entry['message']}\n\n" yield final_result_text else: yield log_message except queue.Empty: pass
# Handle human feedback requests if human_in_the_loop_event.is_set(): yield "Waiting for human feedback..." try: feedback_request = human_input_queue.get(timeout=0.1) # Get context for the request. human_interface = get_human_feedback(feedback_request) #Show context in the input box yield gr.Textbox.update(visible=False), gr.update(visible=True) # Show the human feedback interface. human_in_the_loop_event.wait() # Wait until the event is cleared
except queue.Empty: pass await asyncio.sleep(0.1) # Prevent busy-waiting
conversation_thread.join()
# -------------------- Launch the Chatbot --------------------17 unmodified lines
if __name__ == "__main__": demo = gr.TabbedInterface([iface, dummy_iface], ["Chatbot", "Dummy"]) demo.launch(share=True) demo.launch(share=True)
import time #Import the time moduleapp.py+34-55