File size: 2,650 Bytes
2d8dd2a
9b5b26a
2d8dd2a
9b5b26a
c19d193
8aab7e0
 
8fe992b
8aab7e0
9b5b26a
 
 
8aab7e0
 
 
9b5b26a
8aab7e0
e8713c9
2d8dd2a
e8713c9
 
2d8dd2a
e8713c9
 
2d8dd2a
e8713c9
9b5b26a
 
8aab7e0
 
 
 
 
 
 
 
 
 
2d8dd2a
 
 
 
 
 
 
 
 
8aab7e0
 
 
 
 
 
 
2d8dd2a
8aab7e0
 
 
2d8dd2a
 
 
 
 
 
 
 
 
8aab7e0
 
 
9b5b26a
8aab7e0
8c01ffb
 
8aab7e0
2d8dd2a
8aab7e0
 
 
2d8dd2a
 
 
 
 
 
 
 
 
8aab7e0
 
 
 
 
 
 
6aae614
ae7a494
 
2d8dd2a
 
 
e121372
2d8dd2a
8aab7e0
2d8dd2a
 
13d500a
8c01ffb
 
2d8dd2a
 
 
 
 
8aab7e0
 
2d8dd2a
 
8aab7e0
8c01ffb
 
8fe992b
8aab7e0
 
 
 
 
 
 
 
2d8dd2a
8c01ffb
2d8dd2a
 
 
 
861422e
8fe992b
 
9b5b26a
8c01ffb
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
import pandas as pd
import json

from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI


# -----------------------------
# TIME TOOL
# -----------------------------
@tool
def get_time(timezone: str) -> str:
    """
    Get current time in a timezone.

    Args:
        timezone: Timezone name like Asia/Kathmandu

    Returns:
        Current time string
    """
    try:
        tz = pytz.timezone(timezone)
        return datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
    except Exception as e:
        return str(e)


# -----------------------------
# CALCULATOR TOOL
# -----------------------------
@tool
def calc(expression: str) -> str:
    """
    Evaluate math expression.

    Args:
        expression: Math expression like 2+2*5

    Returns:
        Result of calculation
    """
    try:
        return str(eval(expression))
    except:
        return "error"


# -----------------------------
# CSV TOOL
# -----------------------------
@tool
def read_csv(url: str) -> str:
    """
    Read CSV file from URL.

    Args:
        url: CSV file URL

    Returns:
        CSV content
    """
    try:
        df = pd.read_csv(url)
        return df.to_string()
    except Exception as e:
        return str(e)


# -----------------------------
# JSON TOOL
# -----------------------------
@tool
def read_json(url: str) -> str:
    """
    Read JSON file from URL.

    Args:
        url: JSON file URL

    Returns:
        JSON content
    """
    try:
        r = requests.get(url)
        return json.dumps(r.json(), indent=2)[:3000]
    except Exception as e:
        return str(e)


final_answer = FinalAnswerTool()


# If model overloads use endpoint:
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'

model = HfApiModel(
    max_tokens=2096,
    temperature=0.1,
    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
    custom_role_conversions=None,
)


# optional image tool
image_generation_tool = load_tool(
    "agents-course/text-to-image",
    trust_remote_code=True
)


with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)


agent = CodeAgent(
    model=model,
    tools=[
        final_answer,
        DuckDuckGoSearchTool(),
        get_time,
        calc,
        read_csv,
        read_json
    ],
    max_steps=12,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)


GradioUI(agent).launch()