Spaces:
Sleeping
Sleeping
Update app.py
Browse files10 unmodified lines1112131415161718192021222324252627282930313233343510 unmodified lines@tooldef news_aggregator(keyword: str, max_results: int = 5) -> str:try:api_key = os.getenv("NEWSAPI_KEY") # Fetch the API key from environment variablesif not api_key:return "NewsAPI key is missing. Please set the NEWSAPI_KEY environment variable."url = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}"response = requests.get(url)response.raise_for_status()data = response.json()articles = data.get("articles", [])if not articles:return "No news articles found for the given keyword."result = "Here are the latest news articles:\n"for i, article in enumerate(articles, 1):title = article.get("title", "No title")url = article.get("url", "#")result += f"{i}. {title}\n {url}\n"return resultexcept requests.exceptions.RequestException as e:return f"Error fetching news articles: {str(e)}"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:10 unmodified lines111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596010 unmodified lines@tooldef news_aggregator(keyword: str, max_results: int = 5) -> str:"""A tool that fetches the latest news articles related to a specific keyword.Args:keyword: The topic or keyword to search for (e.g., "AI", "technology").max_results: The maximum number of articles to return (default is 5).Returns:A string containing the titles and URLs of the latest news articles."""try:# Fetch the API key from environment variablesapi_key = os.getenv("NEWSAPI_KEY")if not api_key:return "Error: NewsAPI key is missing. Please set the NEWSAPI_KEY environment variable."# Construct the API URLurl = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}"# Make the API requestresponse = requests.get(url)response.raise_for_status() # Raise an error for bad status codes# Parse the JSON responsedata = response.json()# Check if the response contains articlesarticles = data.get("articles", [])if not articles:return f"No news articles found for the keyword '{keyword}'."# Format the resultsresult = f"Here are the latest news articles about '{keyword}':\n"for i, article in enumerate(articles, 1):title = article.get("title", "No title")url = article.get("url", "#")result += f"{i}. {title}\n {url}\n"return resultexcept requests.exceptions.RequestException as e:return f"Error fetching news articles: {str(e)}"except Exception as e:return f"An unexpected error occurred: {str(e)}"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:
app.py+30-5