ApeMaster commited on
Commit
46c0612
·
verified ·
1 Parent(s): f808fd5

Update app.py

Browse files

Version-safe model import (handles smolagents < 1.9 and >= 1.14)

Files changed (1) hide show
  1. app.py +18 -25
app.py CHANGED
@@ -1,6 +1,14 @@
 
 
 
 
 
 
 
 
1
  from smolagents import (
2
  CodeAgent,
3
- HfApiModel,
4
  load_tool,
5
  tool,
6
  )
@@ -13,19 +21,6 @@ from tools.final_answer import FinalAnswerTool
13
 
14
  from Gradio_UI import GradioUI
15
 
16
- # ---------------------------------------------------------------------------
17
- # Search tool: try Bing first (via WebSearchTool), fallback to DuckDuckGo
18
- # ---------------------------------------------------------------------------
19
- try:
20
- from smolagents import WebSearchTool
21
- search_tool = WebSearchTool(engine="bing")
22
- print("Using Bing search engine via WebSearchTool")
23
- except ImportError:
24
- # smolagents < 1.16.0 does not have WebSearchTool
25
- from smolagents import DuckDuckGoSearchTool
26
- search_tool = DuckDuckGoSearchTool()
27
- print("Using DuckDuckGo search engine (upgrade smolagents for Bing)")
28
-
29
 
30
  # ---------------------------------------------------------------------------
31
  # Custom tool 1: Real-time weather (Open-Meteo — free, no API key required)
@@ -133,7 +128,7 @@ def get_current_time_in_timezone(timezone: str) -> str:
133
 
134
 
135
  # ---------------------------------------------------------------------------
136
- # Custom tool 4: Browse a webpage (Chrome‑mimicking headers)
137
  # ---------------------------------------------------------------------------
138
  @tool
139
  def browse_webpage(url: str) -> str:
@@ -148,7 +143,6 @@ def browse_webpage(url: str) -> str:
148
  """
149
  MAX_CHARS = 5000
150
 
151
- # Full Chrome‑like header set — this is what actually gets past many bot checks
152
  headers = {
153
  "User-Agent": (
154
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
@@ -173,7 +167,6 @@ def browse_webpage(url: str) -> str:
173
  if not url.startswith(("http://", "https://")):
174
  url = "https://" + url
175
 
176
- # --- Network request ---------------------------------------------------
177
  try:
178
  resp = requests.get(url, headers=headers, timeout=20, allow_redirects=True)
179
  except requests.exceptions.Timeout:
@@ -184,8 +177,7 @@ def browse_webpage(url: str) -> str:
184
  if resp.status_code != 200:
185
  return (
186
  f"Error: '{url}' returned HTTP {resp.status_code}. "
187
- f"The site may be blocking automated access — try a different URL "
188
- f"or use a search result that does not require a login."
189
  )
190
 
191
  content_type = resp.headers.get("Content-Type", "").lower()
@@ -196,7 +188,6 @@ def browse_webpage(url: str) -> str:
196
  text = text[:MAX_CHARS] + "\n\n[...truncated...]"
197
  return f"Source: {url}\n\n{text}"
198
 
199
- # --- Parse HTML --------------------------------------------------------
200
  try:
201
  soup = BeautifulSoup(resp.text, "html.parser")
202
  except Exception as e:
@@ -240,11 +231,13 @@ def browse_webpage(url: str) -> str:
240
 
241
  final_answer = FinalAnswerTool()
242
 
243
- model = HfApiModel(
 
 
 
 
244
  max_tokens=2096,
245
  temperature=0.5,
246
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
247
- custom_role_conversions=None,
248
  )
249
 
250
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
@@ -256,8 +249,8 @@ agent = CodeAgent(
256
  model=model,
257
  tools=[
258
  final_answer,
259
- search_tool, # 🔍 Bing (or DuckDuckGo fallback)
260
- browse_webpage, # 🌐 read a result URL
261
  image_generation_tool,
262
  get_current_time_in_timezone,
263
  get_weather,
 
1
+ # ---------------------------------------------------------------------------
2
+ # Version-safe model import (handles smolagents < 1.9 and >= 1.14)
3
+ # ---------------------------------------------------------------------------
4
+ try:
5
+ from smolagents import InferenceClientModel as HFModel
6
+ except ImportError:
7
+ from smolagents import HfApiModel as HFModel
8
+
9
  from smolagents import (
10
  CodeAgent,
11
+ DuckDuckGoSearchTool,
12
  load_tool,
13
  tool,
14
  )
 
21
 
22
  from Gradio_UI import GradioUI
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  # ---------------------------------------------------------------------------
26
  # Custom tool 1: Real-time weather (Open-Meteo — free, no API key required)
 
128
 
129
 
130
  # ---------------------------------------------------------------------------
131
+ # Custom tool 4: Browse a webpage (Chrome-mimicking headers)
132
  # ---------------------------------------------------------------------------
133
  @tool
134
  def browse_webpage(url: str) -> str:
 
143
  """
144
  MAX_CHARS = 5000
145
 
 
146
  headers = {
147
  "User-Agent": (
148
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
 
167
  if not url.startswith(("http://", "https://")):
168
  url = "https://" + url
169
 
 
170
  try:
171
  resp = requests.get(url, headers=headers, timeout=20, allow_redirects=True)
172
  except requests.exceptions.Timeout:
 
177
  if resp.status_code != 200:
178
  return (
179
  f"Error: '{url}' returned HTTP {resp.status_code}. "
180
+ f"The site may be blocking automated access — try a different URL."
 
181
  )
182
 
183
  content_type = resp.headers.get("Content-Type", "").lower()
 
188
  text = text[:MAX_CHARS] + "\n\n[...truncated...]"
189
  return f"Source: {url}\n\n{text}"
190
 
 
191
  try:
192
  soup = BeautifulSoup(resp.text, "html.parser")
193
  except Exception as e:
 
231
 
232
  final_answer = FinalAnswerTool()
233
 
234
+ # ---------------------------------------------------------------------------
235
+ # Model — uses InferenceClientModel on new smolagents, HfApiModel on old
236
+ # ---------------------------------------------------------------------------
237
+ model = HFModel(
238
+ model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
239
  max_tokens=2096,
240
  temperature=0.5,
 
 
241
  )
242
 
243
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
249
  model=model,
250
  tools=[
251
  final_answer,
252
+ DuckDuckGoSearchTool(),
253
+ browse_webpage,
254
  image_generation_tool,
255
  get_current_time_in_timezone,
256
  get_weather,