Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
315 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
#
|
329 |
-
|
330 |
-
text = result.response if isinstance(result.response, str) else str(result.response)
|
331 |
|
332 |
-
#
|
333 |
-
|
334 |
-
|
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()
|