fdaudens HF staff commited on
Commit
2bd42a9
·
verified ·
1 Parent(s): 71e0a9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -27
app.py CHANGED
@@ -87,28 +87,65 @@ def scrape_articles(articles: List[Dict]) -> List[Dict]:
87
 
88
  return articles
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  @tool
91
  def summarize_news(articles: List[Dict]) -> str:
92
- """Creates a summary of the news articles followed by a list of sources.
93
-
94
- Args:
95
- articles: List of article dictionaries containing title, source, date, link, snippet, and full_content
96
-
97
- Returns:
98
- A string containing a summary followed by article references
99
- """
100
  if not articles or not isinstance(articles, list):
101
  return "No articles to summarize"
102
 
103
- # Collect all content for the overall summary
104
- all_content = [article.get('full_content', article['snippet']) for article in articles]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
- # Create a high-level summary from content
 
 
 
 
 
 
 
 
107
  summary = "📰 Summary:\n"
108
  summary += "Latest news covers " + ", ".join(set(article['source'] for article in articles)) + ". "
109
- summary += "Key points: " + ". ".join(all_content[:2]) + "\n\n"
 
110
 
111
- # List individual articles
112
  summary += "🔍 Articles:\n"
113
  for idx, article in enumerate(articles, 1):
114
  title = article['title']
@@ -123,24 +160,12 @@ def summarize_news(articles: List[Dict]) -> str:
123
 
124
  return summary
125
 
126
- # Load prompt templates
127
- with open("prompts.yaml", 'r') as stream:
128
- prompt_templates = yaml.safe_load(stream)
129
-
130
- # Initialize the model
131
- model = HfApiModel(
132
- max_tokens=2096,
133
- temperature=0.5,
134
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
135
- custom_role_conversions=None,
136
- )
137
-
138
  final_answer = FinalAnswerTool()
139
 
140
  # Create the agent with all tools
141
  agent = CodeAgent(
142
- model=model,
143
- tools=[fetch_news, scrape_articles, summarize_news, final_answer], # Added scrape_articles
144
  max_steps=6,
145
  verbosity_level=1,
146
  grammar=None,
 
87
 
88
  return articles
89
 
90
+ # Load prompt templates
91
+ with open("prompts.yaml", 'r') as stream:
92
+ prompt_templates = yaml.safe_load(stream)
93
+
94
+ # Initialize the models
95
+ qwen_model = HfApiModel(
96
+ max_tokens=2096,
97
+ temperature=0.5,
98
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
99
+ custom_role_conversions=None,
100
+ )
101
+
102
+ deepseek_model = HfApiModel(
103
+ max_tokens=2096,
104
+ temperature=0.3, # Lower temperature for more focused summaries
105
+ model_id='deepseek-ai/DeepSeek-R1-Distill-Qwen-32B',
106
+ custom_role_conversions=None,
107
+ )
108
+
109
  @tool
110
  def summarize_news(articles: List[Dict]) -> str:
111
+ """Creates a summary of the news articles followed by a list of sources."""
 
 
 
 
 
 
 
112
  if not articles or not isinstance(articles, list):
113
  return "No articles to summarize"
114
 
115
+ # Prepare content for summarization
116
+ content_to_summarize = ""
117
+ for article in articles:
118
+ content = article.get('full_content', article['snippet'])
119
+ content_to_summarize += f"Title: {article['title']}\nSource: {article['source']}\nContent: {content}\n\n"
120
+
121
+ # Use DeepSeek model to generate a concise summary
122
+ summary_prompt = f"""Please provide a concise summary of these news articles, focusing on the key points and main developments. Then list the individual articles with brief descriptions.
123
+
124
+ Articles to summarize:
125
+ {content_to_summarize}
126
+
127
+ Format the output as:
128
+ 📰 Summary: [overall summary]
129
+
130
+ 🔍 Key Articles:
131
+ 1. [Title] - [brief description]
132
+ [Read more link + date]
133
+ """
134
 
135
+ try:
136
+ summary = deepseek_model.complete(summary_prompt).strip()
137
+ return summary
138
+ except Exception as e:
139
+ # Fallback to original summary format if DeepSeek fails
140
+ return original_summary_format(articles)
141
+
142
+ def original_summary_format(articles: List[Dict]) -> str:
143
+ # Original summary format as fallback
144
  summary = "📰 Summary:\n"
145
  summary += "Latest news covers " + ", ".join(set(article['source'] for article in articles)) + ". "
146
+ all_snippets = [article.get('full_content', article['snippet']) for article in articles]
147
+ summary += "Key points: " + ". ".join(all_snippets[:2]) + "\n\n"
148
 
 
149
  summary += "🔍 Articles:\n"
150
  for idx, article in enumerate(articles, 1):
151
  title = article['title']
 
160
 
161
  return summary
162
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  final_answer = FinalAnswerTool()
164
 
165
  # Create the agent with all tools
166
  agent = CodeAgent(
167
+ model=qwen_model, # Use Qwen model for main agent
168
+ tools=[fetch_news, scrape_articles, summarize_news, final_answer],
169
  max_steps=6,
170
  verbosity_level=1,
171
  grammar=None,