Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool,
|
| 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
|
| 25 |
-
"""
|
| 26 |
|
| 27 |
Args:
|
| 28 |
-
source:
|
| 29 |
-
Fox News, CBS News, Wall Street Journal, Daily Mail)
|
| 30 |
|
| 31 |
Returns:
|
| 32 |
-
|
| 33 |
"""
|
| 34 |
-
|
| 35 |
-
"BBC News": "
|
| 36 |
-
"CNN": "
|
| 37 |
-
"Reuters": "
|
| 38 |
-
"AP": "
|
| 39 |
-
"Fox News": "
|
| 40 |
-
"CBS News": "
|
| 41 |
-
"Wall Street Journal": "
|
| 42 |
-
"Daily Mail": "
|
| 43 |
}
|
| 44 |
-
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
if not url:
|
| 49 |
return f"Error: {source} is not a supported news source"
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 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 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 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"
|
| 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,
|
| 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,
|