Spaces:
Sleeping
Sleeping
Update app.py
Browse files310 unmodified lines31131231331431531631731831932032132232332432532632732832933033133233333433533633733833932 unmodified lines37237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841917 unmodified lines437438439440310 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."""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()final_result = Nonewhile thread.is_alive() or not log_q.empty():try:msg = log_q.get(timeout=0.1)if isinstance(msg, tuple) and msg[0] == "result":final_result = msg[1]yield "Conversation complete."else:yield msgexcept queue.Empty:continuethread.join()if final_result:conv_text = "\n=== Conversation ===\n"for entry in final_result:32 unmodified lineshuman_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 threadconversation_thread = threading.Thread(target=lambda: asyncio.run(multi_agent_conversation(message, queue.Queue(), openai_api_key, human_in_the_loop_event, human_input_queue)))conversation_thread.start()log_queue = queue.Queue() # Local log queue for this chat instanceasyncio.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 messagestry:log_message = log_queue.get_nowait() # Non-blocking getif 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_textelse:yield log_messageexcept queue.Empty:pass# Handle human feedback requestsif 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 boxyield 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 clearedexcept queue.Empty:passawait asyncio.sleep(0.1) # Prevent busy-waitingconversation_thread.join()# -------------------- Launch the Chatbot --------------------17 unmodified linesif __name__ == "__main__":demo = gr.TabbedInterface([iface, dummy_iface], ["Chatbot", "Dummy"])demo.launch(share=True)310 unmodified lines31131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635732 unmodified lines39039139239339439539617 unmodified lines414415416417418419310 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, yields log messages, and handles human input within a single thread."""log_q: queue.Queue = queue.Queue()# 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-loopfinal_result = Nonewhile True: # Loop indefinitely to handle multiple potential human feedback requests.try: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." # Indicate completion.break # Exit the loop after processing the final result.else:yield msg # Yield the log message.except queue.Empty: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)if final_result:conv_text = "\n=== Conversation ===\n"for entry in final_result:32 unmodified lineshuman_in_the_loop_event = threading.Event()human_input_queue = queue.Queue() # Use a single queue for both requests and responsesyield from process_conversation_generator(message, openai_api_key, human_in_the_loop_event, human_input_queue)# -------------------- Launch the Chatbot --------------------17 unmodified linesif __name__ == "__main__":demo = gr.TabbedInterface([iface, dummy_iface], ["Chatbot", "Dummy"])demo.launch(share=True)import time #Import the time module
app.py+34-55