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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -25
app.py CHANGED
@@ -311,8 +311,29 @@ async def run_query(query: str):
311
  session_id="web-agent-session",
312
  user_id=ANON_USER_ID,
313
  ):
314
- response_gen = await web_agent.astream(query, ctx=ctx)
315
- return response_gen
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
  finally:
317
  instrumentor.flush()
318
 
@@ -325,31 +346,13 @@ async def gradio_query(user_input, chat_history=None):
325
  history.append({"role": "assistant", "content": "Thinking..."})
326
  yield history, history
327
 
328
- # Run the query (non-streaming at the agent level)
329
- result = await run_query(user_input)
330
- text = result.response if isinstance(result.response, str) else str(result.response)
331
 
332
- # Simulate streaming by yielding partial responses
333
- # This is a simple approach - we'll show the response word by word
334
- words = text.split()
335
- partial_response = ""
336
-
337
- for i, word in enumerate(words):
338
- # Add the word to the partial response
339
- partial_response += word + " "
340
-
341
- # Update the history with the partial response
342
- history[-1]["content"] = partial_response
343
-
344
- # Yield the updated history
345
  yield history, history
346
-
347
- # Add a small delay to simulate typing (can be adjusted)
348
- await asyncio.sleep(0.05)
349
-
350
- # Final update with complete response
351
- history[-1]["content"] = text
352
- yield history, history
353
 
354
  # Build and launch Gradio app
355
  grb = gr.Blocks()
 
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
  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()