Gabandino commited on
Commit
29e0a1a
·
verified ·
1 Parent(s): ce49f55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -50
app.py CHANGED
@@ -1,10 +1,9 @@
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
7
- from bs4 import BeautifulSoup
8
 
9
  from Gradio_UI import GradioUI
10
 
@@ -21,65 +20,42 @@ def my_cutom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
21
 
22
  # Tool to fetch latest news headlines from different news outlets
23
  @tool
24
- def get_latest_news(source: str) -> str:
25
- """Fetches latest headlines from specified news source.
26
 
27
  Args:
28
- source: Name of news source from supported list (BBC News, CNN, Reuters, AP,
29
- Fox News, CBS News, Wall Street Journal, Daily Mail)
30
 
31
  Returns:
32
- String containing latest headlines or error message
33
  """
34
- news_urls = {
35
- "BBC News": "https://www.bbc.com/news",
36
- "CNN": "https://www.cnn.com",
37
- "Reuters": "https://www.reuters.com",
38
- "AP": "https://apnews.com",
39
- "Fox News": "https://www.foxnews.com",
40
- "CBS News": "https://www.cbsnews.com",
41
- "Wall Street Journal": "https://www.wsj.com",
42
- "Daily Mail": "https://www.dailymail.co.uk/home/index.html"
43
  }
44
-
45
  try:
46
- # Get the URL for the requested source
47
- url = news_urls.get(source)
48
- if not url:
49
  return f"Error: {source} is not a supported news source"
50
 
51
- # Fetch and parse the page
52
- headers = {
53
- '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'
54
- }
55
- response = requests.get(url, headers=headers, timeout=10)
56
- response.raise_for_status()
57
- soup = BeautifulSoup(response.text, 'html.parser')
58
 
59
- # Extract headlines based on source-specific patterns
60
- headlines = []
61
- if source == "BBC News":
62
- headlines = [h3.text.strip() for h3 in soup.select('h3[class^="gs-c-promo-heading"]')][:5]
63
- elif source == "CNN":
64
- headlines = [div.text.strip() for div in soup.select('div[class^="container__headline"]')][:5]
65
- elif source == "Reuters":
66
- headlines = [a.text.strip() for a in soup.select('a[data-testid="Heading"]')][:5]
67
- elif source == "AP":
68
- headlines = [a.text.strip() for a in soup.select('a[class*="Component-headline"]')][:5]
69
- elif source == "Fox News":
70
- headlines = [h2.text.strip() for h2 in soup.select('h2[class*="title"]')][:5]
71
- elif source == "CBS News":
72
- headlines = [h3.text.strip() for h3 in soup.select('h3[class*="item__hed"]')][:5]
73
- elif source == "Wall Street Journal":
74
- headlines = [h3.text.strip() for h3 in soup.select('h3[class*="WSJTheme--headline"]')][:5]
75
- elif source == "Daily Mail":
76
- headlines = [h2.text.strip() for h2 in soup.select('h2[class*="linkro-darkred"]')][:5]
77
- # Add other sources' parsing logic here...
78
-
79
- return f"Latest headlines from {source}:\n- " + "\n- ".join(headlines[:5])
80
 
 
 
81
  except Exception as e:
82
- return f"Error fetching news from {source}: {str(e)}"
83
 
84
 
85
  @tool
@@ -124,7 +100,7 @@ with open("prompts.yaml", 'r') as stream:
124
 
125
  agent = CodeAgent(
126
  model=model,
127
- tools=[final_answer, get_latest_news], ## add your tools here (don't remove final answer)
128
  max_steps=6,
129
  verbosity_level=0,
130
  grammar=None,
 
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
 
7
 
8
  from Gradio_UI import GradioUI
9
 
 
20
 
21
  # Tool to fetch latest news headlines from different news outlets
22
  @tool
23
+ def search_news_headlines(source: str) -> str:
24
+ """Searches for recent news headlines from specified source using DuckDuckGo.
25
 
26
  Args:
27
+ source: News source from supported list (BBC, CNN, etc.)
 
28
 
29
  Returns:
30
+ Formatted news results or error message
31
  """
32
+ domain_map = {
33
+ "BBC News": "bbc.com",
34
+ "CNN": "cnn.com",
35
+ "Reuters": "reuters.com",
36
+ "AP": "apnews.com",
37
+ "Fox News": "foxnews.com",
38
+ "CBS News": "cbsnews.com",
39
+ "Wall Street Journal": "wsj.com",
40
+ "Daily Mail": "dailymail.co.uk"
41
  }
42
+
43
  try:
44
+ domain = domain_map.get(source)
45
+ if not domain:
 
46
  return f"Error: {source} is not a supported news source"
47
 
48
+ # Create search query with site filter and time filter
49
+ search_query = f"site:{domain} latest news after:2023-01-01"
 
 
 
 
 
50
 
51
+ # Use existing DuckDuckGoSearchTool implementation
52
+ ddg_tool = DuckDuckGoSearchTool(max_results=5)
53
+ results = ddg_tool.forward(search_query)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
+ return f"Recent news from {source}:\n{results}"
56
+
57
  except Exception as e:
58
+ return f"News search failed: {str(e)}"
59
 
60
 
61
  @tool
 
100
 
101
  agent = CodeAgent(
102
  model=model,
103
+ tools=[final_answer, search_news_headlines], ## add your tools here (don't remove final answer)
104
  max_steps=6,
105
  verbosity_level=0,
106
  grammar=None,