samwill89 commited on
Commit
92aaa26
·
verified ·
1 Parent(s): 48ccd1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -28
app.py CHANGED
@@ -5,7 +5,7 @@ from smolagents import (
5
  DuckDuckGoSearchTool,
6
  load_tool,
7
  tool,
8
- HfApiModel,
9
  )
10
  import datetime
11
  import re
@@ -13,10 +13,7 @@ import requests
13
  import pytz
14
  import yaml
15
 
16
- # In the template, FinalAnswerTool lives in a local module
17
  from tools.final_answer import FinalAnswerTool
18
-
19
- # Simple Gradio wrapper provided by the template Space
20
  from Gradio_UI import GradioUI
21
 
22
 
@@ -24,10 +21,7 @@ from Gradio_UI import GradioUI
24
 
25
  @tool
26
  def get_current_time_in_timezone(timezone: str) -> str:
27
- """Fetch the current local time in a specified timezone.
28
- Args:
29
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
30
- """
31
  try:
32
  tz = pytz.timezone(timezone)
33
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
@@ -38,14 +32,10 @@ def get_current_time_in_timezone(timezone: str) -> str:
38
 
39
  @tool
40
  def fetch_page_title(url: str) -> str:
41
- """Fetch the HTML page at a URL and return its <title>.
42
- Args:
43
- url: The web page to fetch (http/https).
44
- """
45
  try:
46
  resp = requests.get(url, timeout=10)
47
  resp.raise_for_status()
48
- # crude but robust title extraction
49
  m = re.search(r"<title[^>]*>(.*?)</title>", resp.text, flags=re.IGNORECASE | re.DOTALL)
50
  title = re.sub(r"\s+", " ", m.group(1)).strip() if m else "(no <title> found)"
51
  return f"Title: {title}"
@@ -55,10 +45,7 @@ def fetch_page_title(url: str) -> str:
55
 
56
  @tool
57
  def echo_and_length(text: str) -> str:
58
- """Return the text back (trimmed) and its character length.
59
- Args:
60
- text: Any text.
61
- """
62
  t = text.strip()
63
  return f"Echo: {t}\nLength: {len(t)}"
64
 
@@ -73,23 +60,18 @@ model = HfApiModel(
73
  max_tokens=2096,
74
  )
75
 
76
- # Import a community tool from the Hub (text-to-image)
77
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
78
-
79
- # Built-in search tool
80
  web_search = DuckDuckGoSearchTool()
81
 
82
- # Load system prompt templates from YAML
83
  with open("prompts.yaml", "r", encoding="utf-8") as stream:
84
  prompt_templates = yaml.safe_load(stream)
85
 
86
- # Create the Agent with ALL the tools wired in
87
  agent = CodeAgent(
88
  model=model,
89
  tools=[
90
- final_answer, # required: allows the agent to end with a final answer
91
- web_search, # web search capability
92
- image_generation_tool, # text-to-image generation
93
  get_current_time_in_timezone,
94
  fetch_page_title,
95
  echo_and_length,
@@ -98,10 +80,9 @@ agent = CodeAgent(
98
  verbosity_level=1,
99
  grammar=None,
100
  planning_interval=None,
101
- name="My First smolagents Agent",
102
  description="A tiny agent that can search, fetch titles, tell time, and generate images.",
103
- prompt_templates=prompt_templates, # use the system prompts provided in the template
104
  )
105
 
106
- # Launch the Gradio UI
107
  GradioUI(agent).launch()
 
5
  DuckDuckGoSearchTool,
6
  load_tool,
7
  tool,
8
+ HfApiModel, # <- replacement for the old InferenceClientModel
9
  )
10
  import datetime
11
  import re
 
13
  import pytz
14
  import yaml
15
 
 
16
  from tools.final_answer import FinalAnswerTool
 
 
17
  from Gradio_UI import GradioUI
18
 
19
 
 
21
 
22
  @tool
23
  def get_current_time_in_timezone(timezone: str) -> str:
24
+ """Fetch the current local time in a specified timezone, e.g. 'America/Los_Angeles'."""
 
 
 
25
  try:
26
  tz = pytz.timezone(timezone)
27
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
 
32
 
33
  @tool
34
  def fetch_page_title(url: str) -> str:
35
+ """Fetch the HTML page at a URL and return its <title>."""
 
 
 
36
  try:
37
  resp = requests.get(url, timeout=10)
38
  resp.raise_for_status()
 
39
  m = re.search(r"<title[^>]*>(.*?)</title>", resp.text, flags=re.IGNORECASE | re.DOTALL)
40
  title = re.sub(r"\s+", " ", m.group(1)).strip() if m else "(no <title> found)"
41
  return f"Title: {title}"
 
45
 
46
  @tool
47
  def echo_and_length(text: str) -> str:
48
+ """Return the text back (trimmed) and its character length."""
 
 
 
49
  t = text.strip()
50
  return f"Echo: {t}\nLength: {len(t)}"
51
 
 
60
  max_tokens=2096,
61
  )
62
 
 
63
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
 
64
  web_search = DuckDuckGoSearchTool()
65
 
 
66
  with open("prompts.yaml", "r", encoding="utf-8") as stream:
67
  prompt_templates = yaml.safe_load(stream)
68
 
 
69
  agent = CodeAgent(
70
  model=model,
71
  tools=[
72
+ final_answer,
73
+ web_search,
74
+ image_generation_tool,
75
  get_current_time_in_timezone,
76
  fetch_page_title,
77
  echo_and_length,
 
80
  verbosity_level=1,
81
  grammar=None,
82
  planning_interval=None,
83
+ name="my_first_smolagents_agent", # <- valid Python identifier (no spaces)
84
  description="A tiny agent that can search, fetch titles, tell time, and generate images.",
85
+ prompt_templates=prompt_templates,
86
  )
87
 
 
88
  GradioUI(agent).launch()