AC-Angelo93 commited on
Commit
7cb41fc
·
verified ·
1 Parent(s): 3584698

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -59
app.py CHANGED
@@ -6,80 +6,30 @@ import yaml
6
  from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
- from transformers import pipeline
10
 
11
- summarizer = pipeline("summarization")
12
-
13
- @tool
14
- def summarize_article(article_text: str) -> str:
15
- """
16
- A tool that summarizes the content of a news article.
17
-
18
- Args:
19
- article_text: The text content of the article to summarize.
20
-
21
- Returns:
22
- A summarized version of the article.
23
- """
24
- try:
25
- # Summarize the article text
26
- summary = summarizer(article_text, max_length=50, min_length=25, do_sample=False)
27
- return summary[0]['summary_text']
28
- except Exception as e:
29
- return f"Error summarizing article: {str(e)}"
30
 
31
  @tool
32
  def news_aggregator(keyword: str, max_results: int = 5) -> str:
33
- """
34
- A tool that fetches the latest news articles and summarizes them.
35
-
36
- Args:
37
- keyword: The topic or keyword to search for (e.g., "AI", "technology").
38
- max_results: The maximum number of articles to return (default is 5).
39
-
40
- Returns:
41
- A string containing the titles, summaries, and URLs of the latest news articles.
42
- """
43
  try:
44
-
45
- api_key = "d43565ac0e674145a22c5f0de4dcd69c" # Replace with your actual NewsAPI key
46
-
47
- # Construct the API URL
48
  url = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}"
49
-
50
- # Make the API request
51
  response = requests.get(url)
52
- response.raise_for_status() # Raise an error for bad status codes
53
-
54
- # Parse the JSON response
55
  data = response.json()
56
-
57
- # Check if the response contains articles
58
  articles = data.get("articles", [])
59
  if not articles:
60
- return f"No news articles found for the keyword '{keyword}'."
61
-
62
- # Process each article
63
- result = f"Here are the latest news articles about '{keyword}':\n"
64
  for i, article in enumerate(articles, 1):
65
  title = article.get("title", "No title")
66
  url = article.get("url", "#")
67
- content = article.get("content", "") # Get the article content
68
-
69
- # Summarize the article
70
- summary = summarize_article(content)
71
-
72
- # Format the results
73
- result += f"{i}. {title}\n"
74
- result += f" Summary: {summary}\n"
75
- result += f" URL: {url}\n"
76
-
77
  return result
78
-
79
  except requests.exceptions.RequestException as e:
80
  return f"Error fetching news articles: {str(e)}"
81
- except Exception as e:
82
- return f"An unexpected error occurred: {str(e)}"
83
  final_answer = FinalAnswerTool()
84
 
85
  # 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:
@@ -100,7 +50,7 @@ with open("prompts.yaml", 'r') as stream:
100
  # Agent and tools
101
  agent = CodeAgent(
102
  model=model,
103
- tools=[final_answer, news_aggregator, summarize_article], ## add your tools here (don't remove final answer)
104
  max_steps=6,
105
  verbosity_level=1,
106
  grammar=None,
 
6
  from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
 
9
 
10
+ import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  @tool
13
  def news_aggregator(keyword: str, max_results: int = 5) -> str:
 
 
 
 
 
 
 
 
 
 
14
  try:
15
+ api_key = os.getenv("NEWSAPI_KEY") # Fetch the API key from environment variables
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 given keyword."
25
+ result = "Here are the latest news articles:\n"
 
 
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:
 
50
  # Agent and tools
51
  agent = CodeAgent(
52
  model=model,
53
+ tools=[final_answer, news_aggregator], ## add your tools here (don't remove final answer)
54
  max_steps=6,
55
  verbosity_level=1,
56
  grammar=None,