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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -21
app.py CHANGED
@@ -247,7 +247,6 @@ tools = [
247
  web_agent = AgentWorkflow.from_tools_or_functions(
248
  tools,
249
  llm=llm,
250
- streaming=True,
251
  system_prompt="""You are a helpful assistant with access to specialized tools for retrieving information about weather, and news.
252
  AVAILABLE TOOLS:
253
  1. current_weather - Get current weather conditions for a location
@@ -323,30 +322,34 @@ async def gradio_query(user_input, chat_history=None):
323
  history.append({"role": "user", "content": user_input})
324
 
325
  # Add initial assistant message
326
- history.append({"role": "assistant", "content": ""})
327
  yield history, history
328
 
329
- # Get streaming response generator
330
- response_gen = await run_query(user_input)
 
331
 
332
- # Accumulated response
333
- full_response = ""
 
 
334
 
335
- # Process the streaming response
336
- async for delta in response_gen:
337
- if hasattr(delta, 'delta'):
338
- # For streaming text chunks
339
- full_response += delta.delta
340
- elif hasattr(delta, 'response') and delta.response:
341
- # For final response or tool outputs
342
- if isinstance(delta.response, str):
343
- full_response = delta.response
344
- else:
345
- full_response = str(delta.response)
346
 
347
- # Update the last message with accumulated text
348
- history[-1]["content"] = full_response
 
 
349
  yield history, history
 
 
 
 
 
 
 
350
 
351
  # Build and launch Gradio app
352
  grb = gr.Blocks()
@@ -363,7 +366,7 @@ with grb:
363
  chatbot = gr.Chatbot(type="messages")
364
  txt = gr.Textbox(placeholder="Ask me anything...", show_label=False)
365
 
366
- # Modify the submit handler to work with streaming
367
  txt.submit(
368
  gradio_query,
369
  inputs=[txt, chatbot],
@@ -375,7 +378,8 @@ with grb:
375
  )
376
 
377
  # Also update the button click handler
378
- gr.Button("Send").click(
 
379
  gradio_query,
380
  [txt, chatbot],
381
  [chatbot, chatbot]
 
247
  web_agent = AgentWorkflow.from_tools_or_functions(
248
  tools,
249
  llm=llm,
 
250
  system_prompt="""You are a helpful assistant with access to specialized tools for retrieving information about weather, and news.
251
  AVAILABLE TOOLS:
252
  1. current_weather - Get current weather conditions for a location
 
322
  history.append({"role": "user", "content": user_input})
323
 
324
  # Add initial assistant message
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()
 
366
  chatbot = gr.Chatbot(type="messages")
367
  txt = gr.Textbox(placeholder="Ask me anything...", show_label=False)
368
 
369
+ # Set up event handlers for streaming
370
  txt.submit(
371
  gradio_query,
372
  inputs=[txt, chatbot],
 
378
  )
379
 
380
  # Also update the button click handler
381
+ send_btn = gr.Button("Send")
382
+ send_btn.click(
383
  gradio_query,
384
  [txt, chatbot],
385
  [chatbot, chatbot]