fdaudens HF staff commited on
Commit
0f51b42
·
verified ·
1 Parent(s): 8df7869

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -7
app.py CHANGED
@@ -1,5 +1,6 @@
1
  from smolagents import CodeAgent, HfApiModel, tool
2
  from tools.final_answer import FinalAnswerTool
 
3
  from Gradio_UI import GradioUI
4
  import requests
5
  import yaml
@@ -55,12 +56,33 @@ def fetch_news(topic: str, num_results: int = 5) -> List[Dict]:
55
  except Exception as e:
56
  return f"Error: {str(e)}"
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  @tool
59
  def summarize_news(articles: List[Dict]) -> str:
60
  """Creates a summary of the news articles followed by a list of sources.
61
 
62
  Args:
63
- articles: List of article dictionaries containing title, source, date, link, and snippet
64
 
65
  Returns:
66
  A string containing a summary followed by article references
@@ -68,13 +90,13 @@ def summarize_news(articles: List[Dict]) -> str:
68
  if not articles or not isinstance(articles, list):
69
  return "No articles to summarize"
70
 
71
- # Collect all snippets for the overall summary
72
- all_snippets = [article['snippet'] for article in articles if article.get('snippet')]
73
 
74
- # Create a high-level summary from snippets
75
  summary = "📰 Summary:\n"
76
  summary += "Latest news covers " + ", ".join(set(article['source'] for article in articles)) + ". "
77
- summary += "Key points: " + ". ".join(all_snippets[:2]) + "\n\n"
78
 
79
  # List individual articles
80
  summary += "🔍 Articles:\n"
@@ -82,7 +104,8 @@ def summarize_news(articles: List[Dict]) -> str:
82
  title = article['title']
83
  link = article['link']
84
  date = article['date']
85
- snippet = article['snippet'][:100] + "..." if len(article['snippet']) > 100 else article['snippet']
 
86
 
87
  summary += f"{idx}. **{title}**\n"
88
  summary += f" {snippet}\n"
@@ -107,7 +130,7 @@ final_answer = FinalAnswerTool()
107
  # Create the agent with all tools
108
  agent = CodeAgent(
109
  model=model,
110
- tools=[fetch_news, summarize_news, final_answer],
111
  max_steps=6,
112
  verbosity_level=1,
113
  grammar=None,
 
1
  from smolagents import CodeAgent, HfApiModel, tool
2
  from tools.final_answer import FinalAnswerTool
3
+ from tools.visit_webpage import VisitWebpageTool
4
  from Gradio_UI import GradioUI
5
  import requests
6
  import yaml
 
56
  except Exception as e:
57
  return f"Error: {str(e)}"
58
 
59
+ @tool
60
+ def scrape_articles(articles: List[Dict]) -> List[Dict]:
61
+ """Scrapes the full content of news articles from their URLs.
62
+
63
+ Args:
64
+ articles: List of article dictionaries containing article information
65
+
66
+ Returns:
67
+ List of articles with additional full_content field
68
+ """
69
+ webpage_tool = VisitWebpageTool()
70
+
71
+ for article in articles:
72
+ try:
73
+ full_content = webpage_tool.forward(article['link'])
74
+ article['full_content'] = full_content
75
+ except Exception as e:
76
+ article['full_content'] = f"Failed to scrape content: {str(e)}"
77
+
78
+ return articles
79
+
80
  @tool
81
  def summarize_news(articles: List[Dict]) -> str:
82
  """Creates a summary of the news articles followed by a list of sources.
83
 
84
  Args:
85
+ articles: List of article dictionaries containing title, source, date, link, snippet, and full_content
86
 
87
  Returns:
88
  A string containing a summary followed by article references
 
90
  if not articles or not isinstance(articles, list):
91
  return "No articles to summarize"
92
 
93
+ # Collect all content for the overall summary
94
+ all_content = [article.get('full_content', article['snippet']) for article in articles]
95
 
96
+ # Create a high-level summary from content
97
  summary = "📰 Summary:\n"
98
  summary += "Latest news covers " + ", ".join(set(article['source'] for article in articles)) + ". "
99
+ summary += "Key points: " + ". ".join(all_content[:2]) + "\n\n"
100
 
101
  # List individual articles
102
  summary += "🔍 Articles:\n"
 
104
  title = article['title']
105
  link = article['link']
106
  date = article['date']
107
+ content = article.get('full_content', article['snippet'])
108
+ snippet = content[:200] + "..." if len(content) > 200 else content
109
 
110
  summary += f"{idx}. **{title}**\n"
111
  summary += f" {snippet}\n"
 
130
  # Create the agent with all tools
131
  agent = CodeAgent(
132
  model=model,
133
+ tools=[fetch_news, scrape_articles, summarize_news, final_answer], # Added scrape_articles
134
  max_steps=6,
135
  verbosity_level=1,
136
  grammar=None,