Spaces:
Sleeping
Sleeping
Update app.py
Browse files5 unmodified lines67891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848514 unmodified lines1001011021031041051065 unmodified linesfrom tools.final_answer import FinalAnswerToolfrom Gradio_UI import GradioUIfrom transformers import pipelinesummarizer = pipeline("summarization")@tooldef summarize_article(article_text: str) -> str:"""A tool that summarizes the content of a news article.Args:article_text: The text content of the article to summarize.Returns:A summarized version of the article."""try:# Summarize the article textsummary = summarizer(article_text, max_length=50, min_length=25, do_sample=False)return summary[0]['summary_text']except Exception as e:return f"Error summarizing article: {str(e)}"@tooldef news_aggregator(keyword: str, max_results: int = 5) -> str:"""A tool that fetches the latest news articles and summarizes them.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, summaries, and URLs of the latest news articles."""try:api_key = "d43565ac0e674145a22c5f0de4dcd69c" # Replace with your actual NewsAPI key# 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}'."# Process each articleresult = 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", "#")content = article.get("content", "") # Get the article content# Summarize the articlesummary = summarize_article(content)# Format the resultsresult += f"{i}. {title}\n"result += f" Summary: {summary}\n"result += f" URL: {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:14 unmodified lines# Agent and toolsagent = CodeAgent(model=model,tools=[final_answer, news_aggregator, summarize_article], ## add your tools here (don't remove final answer)max_steps=6,verbosity_level=1,grammar=None,5 unmodified lines6789101112131415161718192021222324252627282930313233343514 unmodified lines505152535455565 unmodified linesfrom tools.final_answer import FinalAnswerToolfrom Gradio_UI import GradioUIimport os@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:14 unmodified lines# Agent and toolsagent = CodeAgent(model=model,tools=[final_answer, news_aggregator], ## add your tools here (don't remove final answer)max_steps=6,verbosity_level=1,grammar=None,
app.py+9-59