fdaudens HF Staff commited on
Commit
2dc923b
·
verified ·
1 Parent(s): 7154062

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -23
app.py CHANGED
@@ -117,7 +117,11 @@ forecast_tool = FunctionTool.from_defaults(
117
  # -----------------------------
118
 
119
  def fetch_news_headlines() -> str:
120
- """Fetches the latest news from Google News RSS feed."""
 
 
 
 
121
  url = "https://news.google.com/rss"
122
 
123
  try:
@@ -127,21 +131,21 @@ def fetch_news_headlines() -> str:
127
  # Parse the XML content
128
  root = ET.fromstring(response.content)
129
 
130
- # Format the news articles into a readable string with clickable links
131
  formatted_news = []
132
- formatted_news.append("# Latest Headlines\n")
133
-
134
  for i, item in enumerate(root.findall('.//item')):
135
  if i >= 5:
136
  break
137
  title = item.find('title').text if item.find('title') is not None else 'N/A'
138
  link = item.find('link').text if item.find('link') is not None else 'N/A'
139
  pub_date = item.find('pubDate').text if item.find('pubDate') is not None else 'N/A'
 
140
 
141
- # Make the title a clickable link using Markdown syntax
142
- formatted_news.append(f"{i+1}. **[{title}]({link})**")
143
- formatted_news.append(f" *Published: {pub_date}*")
144
- formatted_news.append("")
 
145
 
146
  return "\n".join(formatted_news) if formatted_news else "No news articles found."
147
 
@@ -159,7 +163,14 @@ google_rss_tool = FunctionTool.from_defaults(
159
  # SERPER API
160
  # -----------------------------
161
  def fetch_news_topics(query: str) -> str:
162
- """Fetches news articles about a specific topic using the Serper API."""
 
 
 
 
 
 
 
163
  url = "https://google.serper.dev/news"
164
 
165
  payload = json.dumps({
@@ -177,23 +188,16 @@ def fetch_news_topics(query: str) -> str:
177
 
178
  news_data = response.json()
179
 
180
- # Format the news articles with clickable links
181
  formatted_news = []
182
- formatted_news.append(f"# News About '{query}'\n")
183
-
184
  for i, article in enumerate(news_data.get('news', [])):
185
  if i >= 5:
186
  break
187
- title = article.get('title', 'N/A')
188
- link = article.get('link', 'N/A')
189
- source = article.get('source', 'N/A')
190
- snippet = article.get('snippet', 'N/A')
191
-
192
- # Make the title a clickable link using Markdown syntax
193
- formatted_news.append(f"{i+1}. **[{title}]({link})**")
194
- formatted_news.append(f" *Source: {source}*")
195
- formatted_news.append(f" {snippet}")
196
- formatted_news.append("")
197
 
198
  return "\n".join(formatted_news) if formatted_news else "No news articles found."
199
 
@@ -289,6 +293,7 @@ web_agent = AgentWorkflow.from_tools_or_functions(
289
  - For locations, use the format "City, Country Code" (e.g., "Montreal, CA")
290
  - For URLs, include the full address with http:// or https://
291
  - When multiple tools are needed to answer a complex question, use them in sequence
 
292
 
293
  When you use a tool, explain to the user that you're retrieving information. After receiving the tool's output, provide a helpful summary of the information.
294
  """
@@ -379,7 +384,7 @@ with grb:
379
  👉 Try asking 'What's the weather in Montreal?' or 'What's in the news today?'
380
  """
381
  )
382
- chatbot = gr.Chatbot(type="messages", render_markdown=True)
383
  txt = gr.Textbox(placeholder="Ask me anything...", show_label=False)
384
 
385
  # Set up event handlers for streaming
 
117
  # -----------------------------
118
 
119
  def fetch_news_headlines() -> str:
120
+ """Fetches the latest news from Google News RSS feed.
121
+
122
+ Returns:
123
+ A string containing the latest news articles from Google News, or an error message if the request fails.
124
+ """
125
  url = "https://news.google.com/rss"
126
 
127
  try:
 
131
  # Parse the XML content
132
  root = ET.fromstring(response.content)
133
 
134
+ # Format the news articles into a readable string
135
  formatted_news = []
 
 
136
  for i, item in enumerate(root.findall('.//item')):
137
  if i >= 5:
138
  break
139
  title = item.find('title').text if item.find('title') is not None else 'N/A'
140
  link = item.find('link').text if item.find('link') is not None else 'N/A'
141
  pub_date = item.find('pubDate').text if item.find('pubDate') is not None else 'N/A'
142
+ description = item.find('description').text if item.find('description') is not None else 'N/A'
143
 
144
+ formatted_news.append(f"Title: {title}")
145
+ formatted_news.append(f"Published: {pub_date}")
146
+ formatted_news.append(f"Link: {link}")
147
+ formatted_news.append(f"Description: {description}")
148
+ formatted_news.append("---")
149
 
150
  return "\n".join(formatted_news) if formatted_news else "No news articles found."
151
 
 
163
  # SERPER API
164
  # -----------------------------
165
  def fetch_news_topics(query: str) -> str:
166
+ """Fetches news articles about a specific topic using the Serper API.
167
+
168
+ Args:
169
+ query: The topic to search for news about.
170
+
171
+ Returns:
172
+ A string containing the news articles found, or an error message if the request fails.
173
+ """
174
  url = "https://google.serper.dev/news"
175
 
176
  payload = json.dumps({
 
188
 
189
  news_data = response.json()
190
 
191
+ # Format the news articles into a readable string
192
  formatted_news = []
 
 
193
  for i, article in enumerate(news_data.get('news', [])):
194
  if i >= 5:
195
  break
196
+ formatted_news.append(f"Title: {article.get('title', 'N/A')}")
197
+ formatted_news.append(f"Source: {article.get('source', 'N/A')}")
198
+ formatted_news.append(f"Link: {article.get('link', 'N/A')}")
199
+ formatted_news.append(f"Snippet: {article.get('snippet', 'N/A')}")
200
+ formatted_news.append("---")
 
 
 
 
 
201
 
202
  return "\n".join(formatted_news) if formatted_news else "No news articles found."
203
 
 
293
  - For locations, use the format "City, Country Code" (e.g., "Montreal, CA")
294
  - For URLs, include the full address with http:// or https://
295
  - When multiple tools are needed to answer a complex question, use them in sequence
296
+ - If possible, provide your sources with clickable links
297
 
298
  When you use a tool, explain to the user that you're retrieving information. After receiving the tool's output, provide a helpful summary of the information.
299
  """
 
384
  👉 Try asking 'What's the weather in Montreal?' or 'What's in the news today?'
385
  """
386
  )
387
+ chatbot = gr.Chatbot(type="messages")
388
  txt = gr.Textbox(placeholder="Ask me anything...", show_label=False)
389
 
390
  # Set up event handlers for streaming