Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,25 +11,50 @@ import os
|
|
| 11 |
|
| 12 |
@tool
|
| 13 |
def news_aggregator(keyword: str, max_results: int = 5) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
-
|
|
|
|
| 16 |
if not api_key:
|
| 17 |
-
return "NewsAPI key is missing. Please set the NEWSAPI_KEY environment variable."
|
|
|
|
|
|
|
| 18 |
url = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}"
|
|
|
|
|
|
|
| 19 |
response = requests.get(url)
|
| 20 |
-
response.raise_for_status()
|
|
|
|
|
|
|
| 21 |
data = response.json()
|
|
|
|
|
|
|
| 22 |
articles = data.get("articles", [])
|
| 23 |
if not articles:
|
| 24 |
-
return "No news articles found for the
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
for i, article in enumerate(articles, 1):
|
| 27 |
title = article.get("title", "No title")
|
| 28 |
url = article.get("url", "#")
|
| 29 |
result += f"{i}. {title}\n {url}\n"
|
|
|
|
| 30 |
return result
|
|
|
|
| 31 |
except requests.exceptions.RequestException as e:
|
| 32 |
return f"Error fetching news articles: {str(e)}"
|
|
|
|
|
|
|
| 33 |
final_answer = FinalAnswerTool()
|
| 34 |
|
| 35 |
# 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:
|
|
|
|
| 11 |
|
| 12 |
@tool
|
| 13 |
def news_aggregator(keyword: str, max_results: int = 5) -> str:
|
| 14 |
+
"""
|
| 15 |
+
A tool that fetches the latest news articles related to a specific keyword.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
keyword: The topic or keyword to search for (e.g., "AI", "technology").
|
| 19 |
+
max_results: The maximum number of articles to return (default is 5).
|
| 20 |
+
|
| 21 |
+
Returns:
|
| 22 |
+
A string containing the titles and URLs of the latest news articles.
|
| 23 |
+
"""
|
| 24 |
try:
|
| 25 |
+
# Fetch the API key from environment variables
|
| 26 |
+
api_key = os.getenv("NEWSAPI_KEY")
|
| 27 |
if not api_key:
|
| 28 |
+
return "Error: NewsAPI key is missing. Please set the NEWSAPI_KEY environment variable."
|
| 29 |
+
|
| 30 |
+
# Construct the API URL
|
| 31 |
url = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}"
|
| 32 |
+
|
| 33 |
+
# Make the API request
|
| 34 |
response = requests.get(url)
|
| 35 |
+
response.raise_for_status() # Raise an error for bad status codes
|
| 36 |
+
|
| 37 |
+
# Parse the JSON response
|
| 38 |
data = response.json()
|
| 39 |
+
|
| 40 |
+
# Check if the response contains articles
|
| 41 |
articles = data.get("articles", [])
|
| 42 |
if not articles:
|
| 43 |
+
return f"No news articles found for the keyword '{keyword}'."
|
| 44 |
+
|
| 45 |
+
# Format the results
|
| 46 |
+
result = f"Here are the latest news articles about '{keyword}':\n"
|
| 47 |
for i, article in enumerate(articles, 1):
|
| 48 |
title = article.get("title", "No title")
|
| 49 |
url = article.get("url", "#")
|
| 50 |
result += f"{i}. {title}\n {url}\n"
|
| 51 |
+
|
| 52 |
return result
|
| 53 |
+
|
| 54 |
except requests.exceptions.RequestException as e:
|
| 55 |
return f"Error fetching news articles: {str(e)}"
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"An unexpected error occurred: {str(e)}"
|
| 58 |
final_answer = FinalAnswerTool()
|
| 59 |
|
| 60 |
# 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:
|