Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
@@ -21,18 +22,21 @@ search_tool = DuckDuckGoSearchTool()
|
|
| 21 |
def get_latest_news() -> Dict[str, List[Dict]]:
|
| 22 |
"""
|
| 23 |
Tool returns latest news from major news outlets using RSS feeds.
|
| 24 |
-
Uses
|
| 25 |
Returns:
|
| 26 |
Dict[str, List[Dict]]: A dictionary where keys are news sources and values are lists of news items.
|
| 27 |
"""
|
|
|
|
| 28 |
rss_feeds = {
|
| 29 |
"Reuters": {
|
| 30 |
-
"
|
| 31 |
-
"Business": "https://
|
|
|
|
| 32 |
},
|
| 33 |
-
"
|
| 34 |
-
"
|
| 35 |
-
"World": "
|
|
|
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
|
@@ -43,10 +47,14 @@ def get_latest_news() -> Dict[str, List[Dict]]:
|
|
| 43 |
|
| 44 |
for feed_name, feed_url in feeds.items():
|
| 45 |
try:
|
| 46 |
-
# Add
|
| 47 |
headers = {
|
| 48 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
|
|
|
| 49 |
}
|
|
|
|
|
|
|
|
|
|
| 50 |
req = urllib.request.Request(feed_url, headers=headers)
|
| 51 |
response = urllib.request.urlopen(req, timeout=10)
|
| 52 |
xml_data = response.read().decode('utf-8')
|
|
@@ -54,8 +62,12 @@ def get_latest_news() -> Dict[str, List[Dict]]:
|
|
| 54 |
# Parse XML
|
| 55 |
root = ET.fromstring(xml_data)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
title = item.find('title')
|
| 60 |
link = item.find('link')
|
| 61 |
pub_date = item.find('pubDate')
|
|
@@ -70,11 +82,15 @@ def get_latest_news() -> Dict[str, List[Dict]]:
|
|
| 70 |
}
|
| 71 |
|
| 72 |
news_items[source].append(news_item)
|
|
|
|
|
|
|
| 73 |
|
| 74 |
except Exception as e:
|
|
|
|
|
|
|
| 75 |
news_items[source].append({
|
| 76 |
'category': feed_name,
|
| 77 |
-
'title':
|
| 78 |
'link': '',
|
| 79 |
'published': '',
|
| 80 |
'summary': ''
|
|
@@ -82,6 +98,8 @@ def get_latest_news() -> Dict[str, List[Dict]]:
|
|
| 82 |
|
| 83 |
return news_items
|
| 84 |
|
|
|
|
|
|
|
| 85 |
final_answer = FinalAnswerTool()
|
| 86 |
|
| 87 |
# 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:
|
|
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
+
import xml.etree.ElementTree as ET
|
| 5 |
import pytz
|
| 6 |
import yaml
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 22 |
def get_latest_news() -> Dict[str, List[Dict]]:
|
| 23 |
"""
|
| 24 |
Tool returns latest news from major news outlets using RSS feeds.
|
| 25 |
+
Uses verified RSS feed URLs and detailed error reporting.
|
| 26 |
Returns:
|
| 27 |
Dict[str, List[Dict]]: A dictionary where keys are news sources and values are lists of news items.
|
| 28 |
"""
|
| 29 |
+
# Verified working RSS feeds
|
| 30 |
rss_feeds = {
|
| 31 |
"Reuters": {
|
| 32 |
+
"Top News": "https://feeds.reuters.com/reuters/topNews",
|
| 33 |
+
"Business": "https://feeds.reuters.com/reuters/businessNews",
|
| 34 |
+
"World": "https://feeds.reuters.com/reuters/worldNews"
|
| 35 |
},
|
| 36 |
+
"NPR": {
|
| 37 |
+
"News": "https://feeds.npr.org/1001/rss.xml",
|
| 38 |
+
"World": "https://feeds.npr.org/1004/rss.xml",
|
| 39 |
+
"Business": "https://feeds.npr.org/1006/rss.xml"
|
| 40 |
}
|
| 41 |
}
|
| 42 |
|
|
|
|
| 47 |
|
| 48 |
for feed_name, feed_url in feeds.items():
|
| 49 |
try:
|
| 50 |
+
# Add modern browser headers
|
| 51 |
headers = {
|
| 52 |
+
'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',
|
| 53 |
+
'Accept': 'application/rss+xml,application/xml;q=0.9,*/*;q=0.8'
|
| 54 |
}
|
| 55 |
+
|
| 56 |
+
print(f"Fetching {source} - {feed_name} from {feed_url}") # Debug info
|
| 57 |
+
|
| 58 |
req = urllib.request.Request(feed_url, headers=headers)
|
| 59 |
response = urllib.request.urlopen(req, timeout=10)
|
| 60 |
xml_data = response.read().decode('utf-8')
|
|
|
|
| 62 |
# Parse XML
|
| 63 |
root = ET.fromstring(xml_data)
|
| 64 |
|
| 65 |
+
# RSS feeds typically have channel/item structure
|
| 66 |
+
items = root.findall('.//item')
|
| 67 |
+
if not items:
|
| 68 |
+
items = root.findall('./channel/item') # Alternative path
|
| 69 |
+
|
| 70 |
+
for item in items[:5]: # Get top 5 stories
|
| 71 |
title = item.find('title')
|
| 72 |
link = item.find('link')
|
| 73 |
pub_date = item.find('pubDate')
|
|
|
|
| 82 |
}
|
| 83 |
|
| 84 |
news_items[source].append(news_item)
|
| 85 |
+
|
| 86 |
+
print(f"Successfully fetched {len(items)} items from {source} - {feed_name}") # Debug info
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
+
error_message = f"Error fetching {feed_name} feed from {source}: {str(e)}"
|
| 90 |
+
print(error_message) # Debug info
|
| 91 |
news_items[source].append({
|
| 92 |
'category': feed_name,
|
| 93 |
+
'title': error_message,
|
| 94 |
'link': '',
|
| 95 |
'published': '',
|
| 96 |
'summary': ''
|
|
|
|
| 98 |
|
| 99 |
return news_items
|
| 100 |
|
| 101 |
+
|
| 102 |
+
|
| 103 |
final_answer = FinalAnswerTool()
|
| 104 |
|
| 105 |
# 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:
|