dygoo's picture
Update app.py
a3cdf6b verified
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
import datetime
import requests
import xml.etree.ElementTree as ET
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from bs4 import BeautifulSoup
from typing import List, Dict
import urllib.request
from Gradio_UI import GradioUI
# Below is an example of a tool that does nothing. Amaze us with your creativity !
search_tool = DuckDuckGoSearchTool()
system_message="""You have access to a variety of tools that you can use to fetch news sources. Your goal is to provide a summary of all news in a maximum of five sentences."""
@tool
def get_latest_news() -> Dict[str, List[Dict]]:
"""
Tool returns latest news from major news outlets using reliable RSS feeds.
Returns:
A summary of all titles compiled in a single concise text of maximum five sentences.Please do not provide the titles, just a five sentence summary of your making encompassing all titles.
# Dict[str, List[Dict]]: A dictionary where keys are news sources and values are lists of news items.
"""
rss_feeds = {
"NPR": {
"News": "https://feeds.npr.org/1001/rss.xml",
"World": "https://feeds.npr.org/1004/rss.xml",
"Politics": "https://feeds.npr.org/1014/rss.xml",
"Business": "https://feeds.npr.org/1006/rss.xml"
},
"BBC": {
"Top Stories": "http://feeds.bbci.co.uk/news/rss.xml",
"World": "http://feeds.bbci.co.uk/news/world/rss.xml",
"Politics": "http://feeds.bbci.co.uk/news/politics/rss.xml",
"Business": "http://feeds.bbci.co.uk/news/business/rss.xml"
}
}
news_items = {}
for source, feeds in rss_feeds.items():
news_items[source] = []
print(f"\nTrying source: {source}")
for feed_name, feed_url in feeds.items():
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept': 'application/rss+xml,application/xml;q=0.9,*/*;q=0.8'
}
print(f" Fetching {feed_name}...", end=" ")
req = urllib.request.Request(feed_url, headers=headers)
response = urllib.request.urlopen(req, timeout=10)
xml_data = response.read().decode('utf-8')
root = ET.fromstring(xml_data)
# Try different possible XML paths for items
items = root.findall('.//item')
if not items:
items = root.findall('./channel/item')
successful_items = 0
for item in items[:5]:
title = item.find('title')
link = item.find('link')
pub_date = item.find('pubDate')
description = item.find('description')
# Only add items that have at least a title
if title is not None and title.text:
news_item = {
'category': feed_name,
'title': title.text.strip(),
'link': link.text.strip() if link is not None and link.text else '',
'published': pub_date.text if pub_date is not None else '',
'summary': description.text[:200] + '...' if description is not None and description.text else ''
}
news_items[source].append(news_item)
successful_items += 1
print(f"Success! Found {successful_items} articles")
except Exception as e:
print(f"Failed: {str(e)}")
news_items[source].append({
'category': feed_name,
'title': f"Error fetching {feed_name} feed: {str(e)}",
'link': '',
'published': '',
'summary': ''
})
return news_items
final_answer = FinalAnswerTool()
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
#model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
custom_role_conversions=None,
)
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[final_answer, search_tool, get_latest_news], ## add your tools here (don't remove final answer)
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
GradioUI(agent).launch()