Shafagh99 commited on
Commit
8fe38ae
·
verified ·
1 Parent(s): e235df0

make the agent answers more readable

Browse files
Files changed (1) hide show
  1. app.py +35 -15
app.py CHANGED
@@ -3,15 +3,14 @@ import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
- from tools.final_answer import FinalAnswerTool
7
-
8
  import gradio as gr
 
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
  def my_custom_tool(arg1: str, arg2: int) -> str: # it's important to specify the return type
13
  # Keep this format for the description / args / args description but feel free to modify the tool
14
- """Fetch top headlines from popular news sources for today.
15
 
16
  Args:
17
  arg1: Comma-separated list of sources to include (options: "bbc", "nyt", "guardian", "hn", "all").
@@ -19,7 +18,8 @@ def my_custom_tool(arg1: str, arg2: int) -> str: # it's important to specify th
19
  arg2: Maximum number of headlines per source (must be > 0).
20
 
21
  This tool scrapes public RSS feeds (no API key needed) and returns a
22
- human-readable summary grouped by source, with each headline on its own line.
 
23
  """
24
  import xml.etree.ElementTree as ET
25
 
@@ -65,30 +65,38 @@ def my_custom_tool(arg1: str, arg2: int) -> str: # it's important to specify th
65
  url = meta["url"]
66
 
67
  try:
68
- resp = requests.get(url, timeout=5)
69
  resp.raise_for_status()
70
  root = ET.fromstring(resp.content)
71
 
72
- # RSS structure: channel/item/title
73
  items = []
74
  for item in root.findall(".//item"):
75
  title_el = item.find("title")
 
76
  if title_el is not None and title_el.text:
77
  title = title_el.text.strip()
 
78
  if title:
79
- items.append(title)
80
  if len(items) >= arg2:
81
  break
82
 
83
  if not items:
84
- output_sections.append(f"{name}:\n (no headlines found)")
85
  else:
86
- joined = "\n".join(f" - {t}" for t in items)
87
- output_sections.append(f"{name} (top {len(items)}):\n{joined}")
 
 
 
 
 
 
88
  except Exception as e:
89
- output_sections.append(f"{name}:\n Error fetching headlines: {e}")
90
 
91
- header = "Top headlines from popular news sources (via public RSS feeds):"
92
  return header + "\n\n" + "\n\n".join(output_sections)
93
 
94
  @tool
@@ -149,11 +157,23 @@ def chat_fn(message, history):
149
  "returned an error like 502). Please try again or with a simpler request."
150
  )
151
  result_str = str(result)
152
- history = history + [(message, result_str)]
153
- # ChatInterface expects (assistant_message)
154
  return result_str
155
 
156
 
157
- demo = gr.ChatInterface(fn=chat_fn, title="SmolAgents News & Tools Agent")
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
  demo.launch()
 
3
  import requests
4
  import pytz
5
  import yaml
 
 
6
  import gradio as gr
7
+ from tools.final_answer import FinalAnswerTool
8
 
9
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
10
  @tool
11
  def my_custom_tool(arg1: str, arg2: int) -> str: # it's important to specify the return type
12
  # Keep this format for the description / args / args description but feel free to modify the tool
13
+ """Fetch and nicely format top headlines from popular news sources for today.
14
 
15
  Args:
16
  arg1: Comma-separated list of sources to include (options: "bbc", "nyt", "guardian", "hn", "all").
 
18
  arg2: Maximum number of headlines per source (must be > 0).
19
 
20
  This tool scrapes public RSS feeds (no API key needed) and returns a
21
+ markdown-formatted string grouped by source, with each headline on its own line
22
+ and linked to the original article when available.
23
  """
24
  import xml.etree.ElementTree as ET
25
 
 
65
  url = meta["url"]
66
 
67
  try:
68
+ resp = requests.get(url, timeout=8)
69
  resp.raise_for_status()
70
  root = ET.fromstring(resp.content)
71
 
72
+ # RSS structure: channel/item/title/link
73
  items = []
74
  for item in root.findall(".//item"):
75
  title_el = item.find("title")
76
+ link_el = item.find("link")
77
  if title_el is not None and title_el.text:
78
  title = title_el.text.strip()
79
+ link = link_el.text.strip() if link_el is not None and link_el.text else ""
80
  if title:
81
+ items.append((title, link))
82
  if len(items) >= arg2:
83
  break
84
 
85
  if not items:
86
+ output_sections.append(f"### {name}\n\n_(no headlines found)_")
87
  else:
88
+ lines = []
89
+ for idx, (title, link) in enumerate(items, start=1):
90
+ if link:
91
+ lines.append(f"{idx}. [{title}]({link})")
92
+ else:
93
+ lines.append(f"{idx}. {title}")
94
+ joined = "\n".join(lines)
95
+ output_sections.append(f"### {name} (top {len(items)})\n\n{joined}")
96
  except Exception as e:
97
+ output_sections.append(f"### {name}\n\n_Error fetching headlines: {e}_")
98
 
99
+ header = "## Top headlines from popular news sources\n"
100
  return header + "\n\n" + "\n\n".join(output_sections)
101
 
102
  @tool
 
157
  "returned an error like 502). Please try again or with a simpler request."
158
  )
159
  result_str = str(result)
160
+ # For gr.ChatInterface, we only return the assistant message;
161
+ # ChatInterface manages the history itself.
162
  return result_str
163
 
164
 
165
+ demo = gr.ChatInterface(
166
+ fn=chat_fn,
167
+ title="Daily Headlines Assistant",
168
+ description=(
169
+ "Ask for today's top headlines from BBC, The New York Times, The Guardian, "
170
+ "and Hacker News. Results include clickable links and are grouped by source."
171
+ ),
172
+ examples=[
173
+ "Show me today’s top 3 headlines from BBC, NYT and Hacker News.",
174
+ "Give me the top 5 headlines from BBC only.",
175
+ "Fetch today’s main stories from The Guardian and Hacker News.",
176
+ ],
177
+ )
178
 
179
  demo.launch()