Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -129,6 +129,8 @@ def fetch_news_headlines() -> str:
|
|
129 |
|
130 |
# Format the news articles into a readable string with clickable links
|
131 |
formatted_news = []
|
|
|
|
|
132 |
for i, item in enumerate(root.findall('.//item')):
|
133 |
if i >= 5:
|
134 |
break
|
@@ -137,9 +139,9 @@ def fetch_news_headlines() -> str:
|
|
137 |
pub_date = item.find('pubDate').text if item.find('pubDate') is not None else 'N/A'
|
138 |
|
139 |
# Make the title a clickable link using Markdown syntax
|
140 |
-
formatted_news.append(f"**[{title}]({link})**")
|
141 |
-
formatted_news.append(f"Published: {pub_date}")
|
142 |
-
formatted_news.append("
|
143 |
|
144 |
return "\n".join(formatted_news) if formatted_news else "No news articles found."
|
145 |
|
@@ -177,6 +179,8 @@ def fetch_news_topics(query: str) -> str:
|
|
177 |
|
178 |
# Format the news articles with clickable links
|
179 |
formatted_news = []
|
|
|
|
|
180 |
for i, article in enumerate(news_data.get('news', [])):
|
181 |
if i >= 5:
|
182 |
break
|
@@ -186,10 +190,10 @@ def fetch_news_topics(query: str) -> str:
|
|
186 |
snippet = article.get('snippet', 'N/A')
|
187 |
|
188 |
# Make the title a clickable link using Markdown syntax
|
189 |
-
formatted_news.append(f"**[{title}]({link})**")
|
190 |
-
formatted_news.append(f"Source: {source}")
|
191 |
-
formatted_news.append(f"
|
192 |
-
formatted_news.append("
|
193 |
|
194 |
return "\n".join(formatted_news) if formatted_news else "No news articles found."
|
195 |
|
@@ -310,54 +314,37 @@ async def run_query(query: str):
|
|
310 |
# Start the handler
|
311 |
handler = web_agent.run(query, ctx=ctx)
|
312 |
|
|
|
|
|
|
|
313 |
# Stream content
|
314 |
async for event in handler.stream_events():
|
315 |
-
# Add some debugging info to understand event structure
|
316 |
-
# print(f"Event type: {type(event)}")
|
317 |
-
# print(f"Event attrs: {dir(event)}")
|
318 |
-
|
319 |
if isinstance(event, AgentStream):
|
320 |
-
#
|
321 |
if hasattr(event, 'delta') and event.delta:
|
322 |
-
|
|
|
|
|
|
|
|
|
|
|
323 |
|
324 |
elif isinstance(event, ToolCall):
|
325 |
-
|
326 |
-
tool_name = "unknown tool"
|
327 |
|
328 |
-
#
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
elif hasattr(event, 'tool_name'):
|
334 |
-
tool_name = event.tool_name
|
335 |
-
|
336 |
-
yield f"\n\n🔧 Using tool: {tool_name}...\n"
|
337 |
|
338 |
elif isinstance(event, ToolCallResult):
|
339 |
-
#
|
340 |
-
|
341 |
-
|
342 |
-
# Try different possible attribute locations
|
343 |
-
if hasattr(event, 'result'):
|
344 |
-
result = event.result
|
345 |
-
elif hasattr(event, 'output'):
|
346 |
-
result = event.output
|
347 |
-
elif hasattr(event, 'data') and hasattr(event.data, 'get'):
|
348 |
-
result = event.data.get("result", "")
|
349 |
-
|
350 |
-
# Truncate long results for display
|
351 |
-
if isinstance(result, str) and len(result) > 200:
|
352 |
-
result_preview = result[:200] + "... (truncated)"
|
353 |
-
else:
|
354 |
-
result_preview = str(result)
|
355 |
-
|
356 |
-
yield f"\n📊 Got result from tool\n"
|
357 |
|
358 |
except Exception as e:
|
359 |
yield f"\n\n❌ Error: {str(e)}\n"
|
360 |
-
# For debugging:
|
361 |
import traceback
|
362 |
yield f"Traceback: {traceback.format_exc()}"
|
363 |
finally:
|
@@ -392,7 +379,7 @@ with grb:
|
|
392 |
👉 Try asking 'What's the weather in Montreal?' or 'What's in the news today?'
|
393 |
"""
|
394 |
)
|
395 |
-
chatbot = gr.Chatbot(type="messages")
|
396 |
txt = gr.Textbox(placeholder="Ask me anything...", show_label=False)
|
397 |
|
398 |
# Set up event handlers for streaming
|
|
|
129 |
|
130 |
# Format the news articles into a readable string with clickable links
|
131 |
formatted_news = []
|
132 |
+
formatted_news.append("# Latest Headlines\n")
|
133 |
+
|
134 |
for i, item in enumerate(root.findall('.//item')):
|
135 |
if i >= 5:
|
136 |
break
|
|
|
139 |
pub_date = item.find('pubDate').text if item.find('pubDate') is not None else 'N/A'
|
140 |
|
141 |
# Make the title a clickable link using Markdown syntax
|
142 |
+
formatted_news.append(f"{i+1}. **[{title}]({link})**")
|
143 |
+
formatted_news.append(f" *Published: {pub_date}*")
|
144 |
+
formatted_news.append("")
|
145 |
|
146 |
return "\n".join(formatted_news) if formatted_news else "No news articles found."
|
147 |
|
|
|
179 |
|
180 |
# Format the news articles with clickable links
|
181 |
formatted_news = []
|
182 |
+
formatted_news.append(f"# News About '{query}'\n")
|
183 |
+
|
184 |
for i, article in enumerate(news_data.get('news', [])):
|
185 |
if i >= 5:
|
186 |
break
|
|
|
190 |
snippet = article.get('snippet', 'N/A')
|
191 |
|
192 |
# Make the title a clickable link using Markdown syntax
|
193 |
+
formatted_news.append(f"{i+1}. **[{title}]({link})**")
|
194 |
+
formatted_news.append(f" *Source: {source}*")
|
195 |
+
formatted_news.append(f" {snippet}")
|
196 |
+
formatted_news.append("")
|
197 |
|
198 |
return "\n".join(formatted_news) if formatted_news else "No news articles found."
|
199 |
|
|
|
314 |
# Start the handler
|
315 |
handler = web_agent.run(query, ctx=ctx)
|
316 |
|
317 |
+
# Keep track of what we're showing to avoid duplicates
|
318 |
+
tool_calls_shown = set()
|
319 |
+
|
320 |
# Stream content
|
321 |
async for event in handler.stream_events():
|
|
|
|
|
|
|
|
|
322 |
if isinstance(event, AgentStream):
|
323 |
+
# Filter out any lines starting with "Thought:" or "Action:"
|
324 |
if hasattr(event, 'delta') and event.delta:
|
325 |
+
delta = event.delta
|
326 |
+
# Filter out thought processes and internal reasoning
|
327 |
+
if not (delta.strip().startswith("Thought:") or
|
328 |
+
delta.strip().startswith("Action:") or
|
329 |
+
delta.strip().startswith("Answer:")):
|
330 |
+
yield delta
|
331 |
|
332 |
elif isinstance(event, ToolCall):
|
333 |
+
tool_name = getattr(event, 'name', getattr(event, 'function_name', getattr(event, 'tool_name', "unknown tool")))
|
|
|
334 |
|
335 |
+
# Only show tool call message once per tool+call combo
|
336 |
+
tool_call_id = f"{tool_name}_{hash(str(getattr(event, 'args', '')))}"
|
337 |
+
if tool_call_id not in tool_calls_shown:
|
338 |
+
tool_calls_shown.add(tool_call_id)
|
339 |
+
yield f"\n\n🔧 Using tool: {tool_name}...\n"
|
|
|
|
|
|
|
|
|
340 |
|
341 |
elif isinstance(event, ToolCallResult):
|
342 |
+
# We don't need to show the raw tool result to the user
|
343 |
+
# The agent will incorporate the results in its response
|
344 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
345 |
|
346 |
except Exception as e:
|
347 |
yield f"\n\n❌ Error: {str(e)}\n"
|
|
|
348 |
import traceback
|
349 |
yield f"Traceback: {traceback.format_exc()}"
|
350 |
finally:
|
|
|
379 |
👉 Try asking 'What's the weather in Montreal?' or 'What's in the news today?'
|
380 |
"""
|
381 |
)
|
382 |
+
chatbot = gr.Chatbot(type="messages", render_markdown=True)
|
383 |
txt = gr.Textbox(placeholder="Ask me anything...", show_label=False)
|
384 |
|
385 |
# Set up event handlers for streaming
|