CultriX commited on
Commit
39c710d
·
verified ·
1 Parent(s): 7b71415

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -23
app.py CHANGED
@@ -13,7 +13,7 @@ from functools import lru_cache
13
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
14
 
15
  # -------------------- External Model Call (with Caching and Retry) --------------------
16
- @lru_cache(maxsize=128) # Cache up to 128 responses
17
  async def call_model(prompt: str, model: str = "gpt-4o", api_key: str = None, max_retries: int = 3) -> str:
18
  """Sends a prompt to the OpenAI API endpoint, with caching and retries."""
19
  if api_key is None:
@@ -344,20 +344,20 @@ def get_human_feedback(placeholder_text):
344
  """Gets human input using a Gradio Textbox."""
345
  with gr.Blocks() as human_feedback_interface:
346
  with gr.Row():
347
- human_input = gr.Textbox(lines=4, label="Human Feedback", placeholder=placeholder_text) #Removed placeholder
348
  with gr.Row():
349
  submit_button = gr.Button("Submit Feedback")
350
 
351
- feedback_queue = queue.Queue()
352
-
353
  def submit_feedback(input_text):
354
- feedback_queue.put(input_text)
355
- return ""
 
 
356
 
357
- submit_button.click(submit_feedback, inputs=human_input, outputs=human_input)
358
  human_feedback_interface.load(None, [], []) # Keep interface alive
359
 
360
- return human_feedback_interface, feedback_queue
361
 
362
  # -------------------- Chat Function for Gradio --------------------
363
 
@@ -368,28 +368,53 @@ def multi_agent_chat(message: str, history: List[Any], openai_api_key: str = Non
368
  if not openai_api_key:
369
  yield "Error: API key not provided."
370
  return
 
371
  human_in_the_loop_event = threading.Event()
372
- human_input_queue = queue.Queue() #For receiving the feedback request
 
 
 
 
 
 
 
 
 
373
 
374
- yield from process_conversation_generator(message, openai_api_key, human_in_the_loop_event, human_input_queue)
 
375
 
376
- while human_in_the_loop_event.is_set():
377
- yield "Waiting for human feedback..."
 
 
378
  try:
379
- feedback_request = human_input_queue.get(timeout=0.1) #Non-blocking, check for feedback request
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
380
 
381
- human_interface, feedback_queue = get_human_feedback(feedback_request)
 
 
382
 
383
- #This is a hacky but currently only working way to make this work with gradio
384
- yield gr.Textbox.update(visible=False), gr.update(visible=True)
385
- human_feedback = feedback_queue.get(timeout=300) # Wait for up to 5 minutes
386
- human_input_queue.put(human_feedback) #Put feedback where Orchestrator can find it.
387
- human_in_the_loop_event.clear()
388
- yield gr.Textbox.update(visible=True), human_interface.close() #Hide human input box
389
- yield from process_conversation_generator(message, openai_api_key, human_in_the_loop_event, human_input_queue)
390
 
391
- except queue.Empty: #If we get here, there was NO human feedback request, so skip.
392
- continue #Go back to the top of the while loop
393
 
394
  # -------------------- Launch the Chatbot --------------------
395
 
 
13
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
14
 
15
  # -------------------- External Model Call (with Caching and Retry) --------------------
16
+ # Removed @lru_cache here, as it caused issues with async and Gradio
17
  async def call_model(prompt: str, model: str = "gpt-4o", api_key: str = None, max_retries: int = 3) -> str:
18
  """Sends a prompt to the OpenAI API endpoint, with caching and retries."""
19
  if api_key is None:
 
344
  """Gets human input using a Gradio Textbox."""
345
  with gr.Blocks() as human_feedback_interface:
346
  with gr.Row():
347
+ human_input = gr.Textbox(lines=4, label="Human Feedback", placeholder=placeholder_text)
348
  with gr.Row():
349
  submit_button = gr.Button("Submit Feedback")
350
 
 
 
351
  def submit_feedback(input_text):
352
+ # Put the feedback into the shared queue
353
+ human_input_queue.put(input_text)
354
+ return "" # Clear the input box after submission
355
+
356
 
357
+ submit_button.click(fn=submit_feedback, inputs=human_input, outputs=human_input)
358
  human_feedback_interface.load(None, [], []) # Keep interface alive
359
 
360
+ return human_feedback_interface
361
 
362
  # -------------------- Chat Function for Gradio --------------------
363
 
 
368
  if not openai_api_key:
369
  yield "Error: API key not provided."
370
  return
371
+
372
  human_in_the_loop_event = threading.Event()
373
+ human_input_queue = queue.Queue() # Use a single queue for both requests and responses
374
+
375
+ # Start the conversation in a separate thread
376
+ conversation_thread = threading.Thread(
377
+ target=lambda: asyncio.run(
378
+ multi_agent_conversation(message, queue.Queue(), openai_api_key, human_in_the_loop_event, human_input_queue)
379
+ )
380
+ )
381
+ conversation_thread.start()
382
+
383
 
384
+ log_queue = queue.Queue() # Local log queue for this chat instance
385
+ asyncio.run(multi_agent_conversation(message, log_queue, openai_api_key, human_in_the_loop_event, human_input_queue))
386
 
387
+
388
+
389
+ while conversation_thread.is_alive() or not log_queue.empty() or human_in_the_loop_event.is_set():
390
+ # Yield log messages
391
  try:
392
+ log_message = log_queue.get_nowait() # Non-blocking get
393
+ if isinstance(log_message, tuple) and log_message[0] == "result":
394
+ final_result_text = "\n=== Conversation ===\n"
395
+ for entry in log_message[1]:
396
+ final_result_text+= f"[{entry['agent']}]: {entry['message']}\n\n"
397
+ yield final_result_text
398
+ else:
399
+ yield log_message
400
+ except queue.Empty:
401
+ pass
402
+
403
+ # Handle human feedback requests
404
+ if human_in_the_loop_event.is_set():
405
+ yield "Waiting for human feedback..."
406
+ try:
407
+ feedback_request = human_input_queue.get(timeout=0.1) # Get context for the request.
408
+ human_interface = get_human_feedback(feedback_request) #Show context in the input box
409
+ yield gr.Textbox.update(visible=False), gr.update(visible=True) # Show the human feedback interface.
410
+ human_in_the_loop_event.wait() # Wait until the event is cleared
411
 
412
+ except queue.Empty:
413
+ pass
414
+ await asyncio.sleep(0.1) # Prevent busy-waiting
415
 
416
+ conversation_thread.join()
 
 
 
 
 
 
417
 
 
 
418
 
419
  # -------------------- Launch the Chatbot --------------------
420