fdaudens HF Staff commited on
Commit
38e33fe
Β·
verified Β·
1 Parent(s): 5db64ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -315,13 +315,20 @@ async def run_query(query: str):
315
  instrumentor.flush()
316
 
317
  # Gradio interface function
318
- async def gradio_query(user_input, chat_history=None):
319
  history = chat_history or []
320
  history.append({"role": "user", "content": user_input})
321
- result = await run_query(user_input)
322
- text = result.response if isinstance(result.response, str) else str(result.response)
323
- history.append({"role": "assistant", "content": text})
324
- return history, history
 
 
 
 
 
 
 
325
 
326
  # Build and launch Gradio app
327
  grb = gr.Blocks()
@@ -332,17 +339,18 @@ with grb:
332
  This bot can check the news, tell you the weather, and even browse websites to answer follow-up questions β€” all powered by a team of tiny AI tools working behind the scenes.\n\n
333
  πŸ§ͺ Built for fun during the [AI Agents course](https://huggingface.co/learn/agents-course/unit0/introduction) β€” it's just a demo to show what agents can do.\n
334
  πŸ™Œ Got ideas or improvements? PRs welcome!\n\n
335
- πŸ‘‰ _Try asking 'What's the weather in Montreal?' or 'What's in the news today?'
336
  """
337
  )
338
  chatbot = gr.Chatbot(type="messages")
339
  txt = gr.Textbox(placeholder="Ask me anything...", show_label=False)
340
  txt.submit(
341
- gradio_query,
342
  inputs=[txt, chatbot],
343
- outputs=[chatbot, chatbot] # first for display, second for state
 
344
  )
345
- gr.Button("Send").click(gradio_query, [txt, chatbot], [chatbot, chatbot])
346
 
347
  if __name__ == "__main__":
348
  grb.launch()
 
315
  instrumentor.flush()
316
 
317
  # Gradio interface function
318
+ async def gradio_query_stream(user_input, chat_history=None):
319
  history = chat_history or []
320
  history.append({"role": "user", "content": user_input})
321
+ # Start streaming response from the agent
322
+ result = await web_agent.run(user_input, ctx=ctx, streaming=True)
323
+ partial = ""
324
+ async for chunk in result.response_gen:
325
+ # If chunk is an object, use chunk.delta or chunk.text
326
+ partial += chunk
327
+ if len(history) > 1 and history[-1]["role"] == "assistant":
328
+ history[-1]["content"] = partial
329
+ else:
330
+ history.append({"role": "assistant", "content": partial})
331
+ yield history, history
332
 
333
  # Build and launch Gradio app
334
  grb = gr.Blocks()
 
339
  This bot can check the news, tell you the weather, and even browse websites to answer follow-up questions β€” all powered by a team of tiny AI tools working behind the scenes.\n\n
340
  πŸ§ͺ Built for fun during the [AI Agents course](https://huggingface.co/learn/agents-course/unit0/introduction) β€” it's just a demo to show what agents can do.\n
341
  πŸ™Œ Got ideas or improvements? PRs welcome!\n\n
342
+ πŸ‘‰ Try asking 'What's the weather in Montreal?' or 'What's in the news today?'
343
  """
344
  )
345
  chatbot = gr.Chatbot(type="messages")
346
  txt = gr.Textbox(placeholder="Ask me anything...", show_label=False)
347
  txt.submit(
348
+ gradio_query_stream,
349
  inputs=[txt, chatbot],
350
+ outputs=[chatbot, chatbot],
351
+ stream=True
352
  )
353
+ gr.Button("Send").click(gradio_query_stream, [txt, chatbot], [chatbot, chatbot])
354
 
355
  if __name__ == "__main__":
356
  grb.launch()