File size: 2,179 Bytes
24343a2
fd0881c
9b5b26a
 
 
c19d193
6aae614
9b5b26a
 
24343a2
9b5b26a
 
4a77741
9b5b26a
4a77741
9b5b26a
 
 
 
 
 
 
8c01ffb
24343a2
4a77741
 
 
 
 
 
 
 
24343a2
4a77741
 
 
 
 
 
8c01ffb
ae7a494
24343a2
4a77741
 
ae7a494
24343a2
4a77741
 
 
fd0881c
4a77741
13d500a
8c01ffb
 
8fe992b
24343a2
 
 
 
 
 
 
8c01ffb
 
 
 
 
8fe992b
 
24343a2
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
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, load_tool, tool  # FIX 1: HfApiModel → InferenceClientModel
import os 
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI


@tool
def get_current_time_in_timezone(timezone: str) -> str:
    """Fetches the current local time in a specified timezone.
    Args:
        timezone: A valid timezone string, e.g. 'Asia/Kolkata' or 'America/New_York'.
    """
    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 get_fun_fact(topic: str) -> str:
    """Returns a fun fact about a given topic using a public API.
    Args:
        topic: The subject to get a fun fact about, e.g. 'math', 'science'.
    """
    try:
        response = requests.get(
            "https://uselessfacts.jsph.pl/api/v2/facts/random?language=en",
            timeout=5
        )
        data = response.json()
        return f"Fun fact about {topic}: {data.get('text', 'No fact found.')}"
    except Exception as e:
        return f"Error fetching fun fact: {str(e)}"


final_answer = FinalAnswerTool()  # FIX 2: was "ffinal_answer" (typo)
search_tool = DuckDuckGoSearchTool()
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)

model = InferenceClientModel(      # FIX 3: works now because FIX 1 imported it
    max_tokens=2096,
    temperature=0.5,
    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
    token=os.environ.get("HF_TOKEN"),
    custom_role_conversions=None,
)

agent = CodeAgent(
    model=model,
    tools=[
        final_answer,               # FIX 2 cascades here — name now matches
        search_tool,
        image_generation_tool,
        get_current_time_in_timezone,
        get_fun_fact,
    ],
    max_steps=6,
    verbosity_level=1,
    planning_interval=None,
    name=None,
    description=None,
)

GradioUI(agent).launch()