Spaces:
Sleeping
Sleeping
Update app.py
Browse files12 unmodified lines1314151616171819324 unmodified lines3443453463473473483493503513523513543553523533543553563573573583593603603613623634 unmodified lines36836937037137237237337437537637737837938038138238337438438538637637738738838939039139239339439539639739839940040140240340440540637940740840941041138141241341441538338438538638738838941641739139241841942012 unmodified lineslogging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
# -------------------- External Model Call (with Caching and Retry) --------------------@lru_cache(maxsize=128) # Cache up to 128 responses# Removed @lru_cache here, as it caused issues with async and Gradioasync def call_model(prompt: str, model: str = "gpt-4o", api_key: str = None, max_retries: int = 3) -> str: """Sends a prompt to the OpenAI API endpoint, with caching and retries.""" if api_key is None:324 unmodified lines """Gets human input using a Gradio Textbox.""" with gr.Blocks() as human_feedback_interface: with gr.Row(): human_input = gr.Textbox(lines=4, label="Human Feedback", placeholder=placeholder_text) #Removed placeholder human_input = gr.Textbox(lines=4, label="Human Feedback", placeholder=placeholder_text) with gr.Row(): submit_button = gr.Button("Submit Feedback")
feedback_queue = queue.Queue()
def submit_feedback(input_text): feedback_queue.put(input_text) return "" # Put the feedback into the shared queue human_input_queue.put(input_text) return "" # Clear the input box after submission
submit_button.click(submit_feedback, inputs=human_input, outputs=human_input) submit_button.click(fn=submit_feedback, inputs=human_input, outputs=human_input) human_feedback_interface.load(None, [], []) # Keep interface alive
return human_feedback_interface, feedback_queue return human_feedback_interface
# -------------------- Chat Function for Gradio --------------------
4 unmodified lines if not openai_api_key: yield "Error: API key not provided." return
human_in_the_loop_event = threading.Event() human_input_queue = queue.Queue() #For receiving the feedback request 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) ) ) conversation_thread.start()
yield from process_conversation_generator(message, openai_api_key, human_in_the_loop_event, human_input_queue) 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 human_in_the_loop_event.is_set(): yield "Waiting for human feedback..."
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) #Non-blocking, check for feedback request 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
human_interface, feedback_queue = get_human_feedback(feedback_request) except queue.Empty: pass await asyncio.sleep(0.1) # Prevent busy-waiting
#This is a hacky but currently only working way to make this work with gradio yield gr.Textbox.update(visible=False), gr.update(visible=True) human_feedback = feedback_queue.get(timeout=300) # Wait for up to 5 minutes human_input_queue.put(human_feedback) #Put feedback where Orchestrator can find it. human_in_the_loop_event.clear() yield gr.Textbox.update(visible=True), human_interface.close() #Hide human input box yield from process_conversation_generator(message, openai_api_key, human_in_the_loop_event, human_input_queue) conversation_thread.join()
except queue.Empty: #If we get here, there was NO human feedback request, so skip. continue #Go back to the top of the while loop
# -------------------- Launch the Chatbot --------------------
app.py+48-23