fdaudens HF Staff commited on
Commit
f487d08
·
verified ·
1 Parent(s): fd36d8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -28
app.py CHANGED
@@ -303,6 +303,8 @@ def run_query_sync(query: str):
303
  web_agent.run(query, ctx=ctx)
304
  )
305
 
 
 
306
  async def run_query(query: str):
307
  trace_id = f"agent-run-{uuid.uuid4().hex}"
308
  try:
@@ -311,29 +313,50 @@ async def run_query(query: str):
311
  session_id="web-agent-session",
312
  user_id=ANON_USER_ID,
313
  ):
314
- # We'll use this to accumulate the response for the final trace
315
- full_response = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
 
317
- # Return a generator that we can iterate through in gradio_query
318
- async def response_generator():
319
- nonlocal full_response
320
- # Use the agent normally, but we'll handle streaming separately
321
- result = await web_agent.run(query, ctx=ctx)
322
- # Get the final response
323
- response_text = result.response if isinstance(result.response, str) else str(result.response)
324
-
325
- # Simulate streaming by yielding words
326
- words = response_text.split()
327
- partial = ""
328
- for word in words:
329
- partial += word + " "
330
- yield partial
331
- await asyncio.sleep(0.05) # Simulate typing speed
332
-
333
- # Store the full response for tracing
334
- full_response = response_text
335
 
336
- return response_generator()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337
  finally:
338
  instrumentor.flush()
339
 
@@ -346,13 +369,13 @@ async def gradio_query(user_input, chat_history=None):
346
  history.append({"role": "assistant", "content": "Thinking..."})
347
  yield history, history
348
 
349
- # Get response generator
350
- response_gen = await run_query(user_input)
351
-
352
- # Update the response as chunks come in
353
- async for chunk in response_gen:
354
- history[-1]["content"] = chunk
355
- yield history, history
356
 
357
  # Build and launch Gradio app
358
  grb = gr.Blocks()
 
303
  web_agent.run(query, ctx=ctx)
304
  )
305
 
306
+ stream_queue = asyncio.Queue()
307
+
308
  async def run_query(query: str):
309
  trace_id = f"agent-run-{uuid.uuid4().hex}"
310
  try:
 
313
  session_id="web-agent-session",
314
  user_id=ANON_USER_ID,
315
  ):
316
+ # Clear the queue before starting
317
+ while not stream_queue.empty():
318
+ try:
319
+ stream_queue.get_nowait()
320
+ except:
321
+ pass
322
+
323
+ # Add thinking message to the queue
324
+ await stream_queue.put("🤔 Thinking about your question...\n\n")
325
+
326
+ # Patch the agent's methods to capture tool usage
327
+ original_call_function = web_agent._call_function
328
+
329
+ async def patched_call_function(function_call):
330
+ tool_name = function_call.get("name", "unknown tool")
331
+ await stream_queue.put(f"🔧 Using tool: {tool_name}...\n")
332
+ result = await original_call_function(function_call)
333
+ await stream_queue.put(f"📊 Got result from {tool_name}\n")
334
+ return result
335
+
336
+ # Apply the patch
337
+ web_agent._call_function = patched_call_function
338
 
339
+ # Start the agent run in a separate task
340
+ run_task = asyncio.create_task(web_agent.run(query, ctx=ctx))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
341
 
342
+ # Stream from the queue while the agent is running
343
+ while not run_task.done():
344
+ try:
345
+ chunk = await asyncio.wait_for(stream_queue.get(), timeout=0.5)
346
+ yield chunk
347
+ except asyncio.TimeoutError:
348
+ # No new items, just wait
349
+ await asyncio.sleep(0.1)
350
+
351
+ # Get the final result
352
+ result = await run_task
353
+ final_response = result.response if isinstance(result.response, str) else str(result.response)
354
+
355
+ # Restore the original method
356
+ web_agent._call_function = original_call_function
357
+
358
+ # Yield the final answer
359
+ yield f"\n\n✅ Final answer: {final_response}"
360
  finally:
361
  instrumentor.flush()
362
 
 
369
  history.append({"role": "assistant", "content": "Thinking..."})
370
  yield history, history
371
 
372
+ # Get streaming response
373
+ full_response = ""
374
+ async for chunk in run_query(user_input):
375
+ if chunk:
376
+ full_response += chunk
377
+ history[-1]["content"] = full_response
378
+ yield history, history
379
 
380
  # Build and launch Gradio app
381
  grb = gr.Blocks()