File size: 2,998 Bytes
c7d4dcb
 
 
 
 
 
 
4441edb
c7d4dcb
9b5b26a
c7d4dcb
9b5b26a
 
c19d193
952bbb1
c7d4dcb
6aae614
9b5b26a
 
c7d4dcb
 
9b5b26a
 
952bbb1
 
 
4441edb
 
 
 
 
 
 
 
9b5b26a
 
 
 
 
 
8c01ffb
 
c7d4dcb
952bbb1
 
 
4441edb
 
 
 
 
 
 
 
c7d4dcb
 
 
 
 
 
 
 
ae7a494
 
c7d4dcb
952bbb1
 
 
4441edb
 
 
 
 
 
 
 
c7d4dcb
 
 
 
 
8c01ffb
c7d4dcb
 
48ccd1e
c7d4dcb
48ccd1e
 
c7d4dcb
8c01ffb
9b5b26a
c7d4dcb
 
 
861422e
c7d4dcb
8c01ffb
8fe992b
c7d4dcb
92aaa26
 
 
c7d4dcb
 
 
 
 
8c01ffb
 
 
4441edb
c7d4dcb
92aaa26
8fe992b
 
c7d4dcb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# app.py

from smolagents import (
    CodeAgent,
    DuckDuckGoSearchTool,
    load_tool,
    tool,
    HfApiModel,   # replacement for InferenceClientModel
)
import datetime
import re
import requests
import pytz
import yaml
from typing import Annotated

from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI


# ---- Custom Tools ----

@tool
def get_current_time_in_timezone(
    timezone: Annotated[str, "IANA timezone like 'America/Los_Angeles'"]
) -> str:
    """Return the current local time for the given timezone.

    Args:
        timezone (str): IANA timezone name, e.g., 'America/Los_Angeles' or 'Asia/Tokyo'.

    Returns:
        str: A human-readable string with the local time in the requested timezone.
    """
    try:
        tz = pytz.timezone(timezone)
        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
        return f"The current local time in {timezone} is: {local_time}"
    except Exception as e:
        return f"Error fetching time for timezone '{timezone}': {str(e)}"


@tool
def fetch_page_title(
    url: Annotated[str, "An http/https URL to fetch"]
) -> str:
    """Fetch the page and return its <title> text.

    Args:
        url (str): A valid HTTP/HTTPS URL to retrieve.

    Returns:
        str: The page title, or an error message if fetching/parsing fails.
    """
    try:
        resp = requests.get(url, timeout=10)
        resp.raise_for_status()
        m = re.search(r"<title[^>]*>(.*?)</title>", resp.text, flags=re.IGNORECASE | re.DOTALL)
        title = re.sub(r"\s+", " ", m.group(1)).strip() if m else "(no <title> found)"
        return f"Title: {title}"
    except Exception as e:
        return f"Error fetching title from '{url}': {e}"


@tool
def echo_and_length(
    text: Annotated[str, "Any text to echo back"]
) -> str:
    """Echo the text and report its character length.

    Args:
        text (str): The text to echo back.

    Returns:
        str: The echoed text and its character count.
    """
    t = text.strip()
    return f"Echo: {t}\nLength: {len(t)}"


# ---- Model & Tools wiring ----

final_answer = FinalAnswerTool()

model = HfApiModel(
    model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
    temperature=0.5,
    max_tokens=2096,
)

image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
web_search = DuckDuckGoSearchTool()

with open("prompts.yaml", "r", encoding="utf-8") as stream:
    prompt_templates = yaml.safe_load(stream)

agent = CodeAgent(
    model=model,
    tools=[
        final_answer,
        web_search,
        image_generation_tool,
        get_current_time_in_timezone,
        fetch_page_title,
        echo_and_length,
    ],
    max_steps=8,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name="my_first_smolagents_agent",
    description="A tiny agent that can search, fetch titles, tell time, and generate images.",
    prompt_templates=prompt_templates,
)

GradioUI(agent).launch()