fdaudens HF Staff commited on
Commit
3da38b0
·
verified ·
1 Parent(s): a73408f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -19
app.py CHANGED
@@ -47,7 +47,8 @@ SERPER_API_KEY = os.getenv("SERPER_API_KEY")
47
  llm = HuggingFaceInferenceAPI(
48
  model_name="Qwen/Qwen2.5-Coder-32B-Instruct",
49
  token=HF_TOKEN,
50
- task="conversational"
 
51
  )
52
 
53
  memory = ChatMemoryBuffer.from_defaults(token_limit=8192)
@@ -246,6 +247,7 @@ tools = [
246
  web_agent = AgentWorkflow.from_tools_or_functions(
247
  tools,
248
  llm=llm,
 
249
  system_prompt="""You are a helpful assistant with access to specialized tools for retrieving information about weather, and news.
250
  AVAILABLE TOOLS:
251
  1. current_weather - Get current weather conditions for a location
@@ -310,24 +312,40 @@ async def run_query(query: str):
310
  session_id="web-agent-session",
311
  user_id=ANON_USER_ID,
312
  ):
313
- return await web_agent.run(query, ctx=ctx)
 
314
  finally:
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
@@ -344,13 +362,28 @@ with grb:
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()
 
47
  llm = HuggingFaceInferenceAPI(
48
  model_name="Qwen/Qwen2.5-Coder-32B-Instruct",
49
  token=HF_TOKEN,
50
+ task="conversational",
51
+ streaming=True
52
  )
53
 
54
  memory = ChatMemoryBuffer.from_defaults(token_limit=8192)
 
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
 
312
  session_id="web-agent-session",
313
  user_id=ANON_USER_ID,
314
  ):
315
+ response_gen = await web_agent.astream(query, ctx=ctx)
316
+ return response_gen
317
  finally:
318
  instrumentor.flush()
319
 
320
  # Gradio interface function
321
+ async def gradio_query(user_input, chat_history=None):
322
  history = chat_history or []
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
 
362
  )
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],
370
+ outputs=[chatbot, chatbot]
371
+ ).then(
372
+ lambda: gr.update(value=""), # Clear the textbox after submission
373
+ None,
374
+ [txt]
375
+ )
376
+
377
+ # Also update the button click handler
378
+ gr.Button("Send").click(
379
+ gradio_query,
380
+ [txt, chatbot],
381
+ [chatbot, chatbot]
382
+ ).then(
383
+ lambda: gr.update(value=""), # Clear the textbox after submission
384
+ None,
385
+ [txt]
386
+ )
387
 
388
  if __name__ == "__main__":
389
  grb.launch()