Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
@@ -18,8 +19,57 @@ def my_cutom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
|
|
|
| 21 |
@tool
|
| 22 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""Fetches the current local time in a specified timezone and provides a human-friendly description.
|
| 24 |
|
| 25 |
Args:
|
|
@@ -52,7 +102,6 @@ model_id='https://jc26mwg228mkj8dw.us-east-1.aws.endpoints.huggingface.cloud',#
|
|
| 52 |
custom_role_conversions=None,
|
| 53 |
)
|
| 54 |
|
| 55 |
-
|
| 56 |
# Import tool from Hub
|
| 57 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 58 |
|
|
@@ -61,9 +110,9 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 61 |
|
| 62 |
agent = CodeAgent(
|
| 63 |
model=model,
|
| 64 |
-
tools=[final_answer,
|
| 65 |
max_steps=6,
|
| 66 |
-
verbosity_level=
|
| 67 |
grammar=None,
|
| 68 |
planning_interval=None,
|
| 69 |
name=None,
|
|
|
|
| 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 |
|
|
|
|
| 19 |
"""
|
| 20 |
return "What magic will you build ?"
|
| 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 |
+
response = requests.get(url, timeout=10)
|
| 53 |
+
response.raise_for_status()
|
| 54 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 55 |
+
|
| 56 |
+
# Extract headlines based on source-specific patterns
|
| 57 |
+
headlines = []
|
| 58 |
+
if source == "BBC News":
|
| 59 |
+
headlines = [h3.text.strip() for h3 in soup.select('h3[class^="gs-c-promo-heading"]')][:5]
|
| 60 |
+
elif source == "CNN":
|
| 61 |
+
headlines = [div.text.strip() for div in soup.select('div[class^="container__headline"]')][:5]
|
| 62 |
+
elif source == "Reuters":
|
| 63 |
+
headlines = [a.text.strip() for a in soup.select('a[data-testid="Heading"]')][:5]
|
| 64 |
+
# Add other sources' parsing logic here...
|
| 65 |
+
|
| 66 |
+
return f"Latest headlines from {source}:\n- " + "\n- ".join(headlines[:5])
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return f"Error fetching news from {source}: {str(e)}"
|
| 70 |
+
|
| 71 |
+
@tool
|
| 72 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 73 |
"""Fetches the current local time in a specified timezone and provides a human-friendly description.
|
| 74 |
|
| 75 |
Args:
|
|
|
|
| 102 |
custom_role_conversions=None,
|
| 103 |
)
|
| 104 |
|
|
|
|
| 105 |
# Import tool from Hub
|
| 106 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 107 |
|
|
|
|
| 110 |
|
| 111 |
agent = CodeAgent(
|
| 112 |
model=model,
|
| 113 |
+
tools=[final_answer, get_latest_news], ## add your tools here (don't remove final answer)
|
| 114 |
max_steps=6,
|
| 115 |
+
verbosity_level=0,
|
| 116 |
grammar=None,
|
| 117 |
planning_interval=None,
|
| 118 |
name=None,
|