File size: 5,184 Bytes
9b5b26a
 
 
032830b
9b5b26a
c19d193
6aae614
ec6fcf7
 
2a54448
 
ec6fcf7
8fe992b
9b5b26a
 
5df72d6
e63ddd1
 
 
0bbb75c
9a83014
7c1efed
f2291d0
7c1efed
6fd9231
8dbfa50
6fd9231
a3cdf6b
0bbb75c
6fd9231
7c1efed
032830b
 
 
8dbfa50
032830b
8dbfa50
 
 
 
 
 
ae517af
f999f0f
ae517af
7c1efed
 
 
 
8dbfa50
7c1efed
 
 
2a54448
032830b
 
2a54448
032830b
8dbfa50
032830b
2a54448
 
 
0125826
2a54448
 
8dbfa50
032830b
 
8dbfa50
032830b
8dbfa50
 
2a54448
 
 
 
0125826
8dbfa50
 
 
 
 
 
 
 
 
 
 
032830b
8dbfa50
7c1efed
 
8dbfa50
7c1efed
 
8dbfa50
7c1efed
 
 
 
 
 
 
032830b
 
6aae614
ae7a494
 
91ec8dc
ae7a494
e121372
bf6d34c
 
91ec8dc
bd0e4ce
fe328e0
13d500a
8c01ffb
 
 
861422e
 
9b5b26a
8c01ffb
8fe992b
ae517af
8c01ffb
 
 
 
 
 
861422e
8fe992b
 
9b5b26a
8c01ffb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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()