File size: 9,651 Bytes
560d019
 
 
e0aafee
9b5b26a
 
 
c19d193
6aae614
9b5b26a
e0aafee
 
 
 
9b5b26a
e795a77
f78d551
e0aafee
 
 
f78d551
560d019
f78d551
 
e795a77
 
560d019
 
e795a77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560d019
 
e795a77
 
e0aafee
e795a77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f78d551
e0aafee
f78d551
e0aafee
9b5b26a
e0aafee
 
 
9b5b26a
e0aafee
 
9b5b26a
f78d551
e0aafee
 
 
f78d551
 
e0aafee
 
f78d551
e0aafee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f78d551
e0aafee
9b5b26a
e0aafee
f78d551
e0aafee
 
 
f78d551
e0aafee
 
f78d551
32548b2
e0aafee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32548b2
e0aafee
f78d551
e0aafee
 
 
 
 
 
 
 
 
 
1e747f7
9b5b26a
 
e0aafee
 
9b5b26a
e0aafee
9b5b26a
 
e0aafee
9b5b26a
e0aafee
9b5b26a
 
 
e0aafee
8c01ffb
6aae614
ae7a494
e0aafee
e121372
f78d551
 
 
 
13d500a
8c01ffb
e0aafee
 
 
861422e
 
f78d551
8c01ffb
8fe992b
e0aafee
 
 
 
 
 
 
 
 
8c01ffb
 
 
 
 
 
861422e
8fe992b
 
e0aafee
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Installation of required packages
# pip install smolagents requests pytz pyyaml beautifulsoup4 Pillow

from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
import bs4
from PIL import Image
import io
import base64

# Weather tool using Open Meteo (completely free, no API key required)
@tool
def get_weather(location: str) -> str:
    """Fetch current weather information for a specified location.
    
    Args:
        location: A string representing the city name.
    """
    try:
        # First, geocode the location to get coordinates
        geocoding_url = f"https://nominatim.openstreetmap.org/search"
        params = {
            'q': location,
            'format': 'json',
            'limit': 1
        }
        headers = {
            'User-Agent': 'WeatherToolAgent/1.0'  # OpenStreetMap requires a user agent
        }
        
        geo_response = requests.get(geocoding_url, params=params, headers=headers)
        geo_data = geo_response.json()
        
        if not geo_data:
            return f"Could not find coordinates for location: {location}"
        
        # Extract latitude and longitude
        lat = float(geo_data[0]['lat'])
        lon = float(geo_data[0]['lon'])
        display_name = geo_data[0]['display_name']
        
        # Now get weather data from Open-Meteo
        weather_url = "https://api.open-meteo.com/v1/forecast"
        weather_params = {
            'latitude': lat,
            'longitude': lon,
            'current': 'temperature_2m,relative_humidity_2m,apparent_temperature,precipitation,weather_code,wind_speed_10m',
            'timezone': 'auto'
        }
        
        weather_response = requests.get(weather_url, params=weather_params)
        weather_data = weather_response.json()
        
        if 'current' not in weather_data:
            return f"Error fetching weather data for {location}"
        
        # WMO Weather interpretation codes (https://open-meteo.com/en/docs)
        weather_codes = {
            0: "Clear sky",
            1: "Mainly clear", 2: "Partly cloudy", 3: "Overcast",
            45: "Fog", 48: "Depositing rime fog",
            51: "Light drizzle", 53: "Moderate drizzle", 55: "Dense drizzle",
            56: "Light freezing drizzle", 57: "Dense freezing drizzle",
            61: "Slight rain", 63: "Moderate rain", 65: "Heavy rain",
            66: "Light freezing rain", 67: "Heavy freezing rain",
            71: "Slight snow fall", 73: "Moderate snow fall", 75: "Heavy snow fall",
            77: "Snow grains",
            80: "Slight rain showers", 81: "Moderate rain showers", 82: "Violent rain showers",
            85: "Slight snow showers", 86: "Heavy snow showers",
            95: "Thunderstorm", 96: "Thunderstorm with slight hail", 99: "Thunderstorm with heavy hail"
        }
        
        current = weather_data['current']
        weather_description = weather_codes.get(current['weather_code'], "Unknown")
        
        weather_info = (
            f"Weather in {display_name}:\n"
            f"Condition: {weather_description}\n"
            f"Temperature: {current['temperature_2m']}°C\n"
            f"Feels like: {current['apparent_temperature']}°C\n"
            f"Humidity: {current['relative_humidity_2m']}%\n"
            f"Wind Speed: {current['wind_speed_10m']} km/h\n"
            f"Precipitation: {current['precipitation']} mm"
        )
        
        return weather_info
    
    except Exception as e:
        return f"Error fetching weather for {location}: {str(e)}"

# Web scraping tool
@tool
def web_scrape(url: str, selector: str = None) -> str:
    """Scrape content from a webpage.
    
    Args:
        url: The URL of the webpage to scrape.
        selector: Optional CSS selector to extract specific elements (default: None, returns full page text).
    """
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        response = requests.get(url, headers=headers)
        
        if response.status_code != 200:
            return f"Failed to access the URL. Status code: {response.status_code}"
        
        soup = bs4.BeautifulSoup(response.text, 'html.parser')
        
        # Remove script and style elements
        for script in soup(["script", "style"]):
            script.extract()
        
        if selector:
            elements = soup.select(selector)
            if not elements:
                return f"No elements found matching selector: {selector}"
            content = "\n".join([elem.get_text(strip=True) for elem in elements])
        else:
            # Get all text
            content = soup.get_text(separator='\n', strip=True)
        
        # Truncate if too long
        if len(content) > 5000:
            content = content[:5000] + "... (content truncated)"
        
        return content
    except Exception as e:
        return f"Error scraping {url}: {str(e)}"

# Image processing tool
@tool
def process_image(image_url: str, operation: str = "info") -> str:
    """Process an image with various operations.
    
    Args:
        image_url: URL of the image to process.
        operation: The operation to perform (options: "info", "resize", "grayscale", "blur").
    """
    try:
        response = requests.get(image_url)
        if response.status_code != 200:
            return f"Failed to download image. Status code: {response.status_code}"
        
        image = Image.open(io.BytesIO(response.content))
        
        if operation == "info":
            info = {
                "format": image.format,
                "mode": image.mode,
                "width": image.width,
                "height": image.height,
                "size_kb": len(response.content) / 1024
            }
            return f"Image information:\n" + "\n".join([f"{k}: {v}" for k, v in info.items()])
        
        elif operation == "resize":
            # Resize to 50% of original size
            new_size = (image.width // 2, image.height // 2)
            resized = image.resize(new_size)
            
            # Convert to base64 for return
            buffered = io.BytesIO()
            resized.save(buffered, format=image.format if image.format else "JPEG")
            img_str = base64.b64encode(buffered.getvalue()).decode()
            
            return f"Resized image (now {new_size[0]}x{new_size[1]}):\ndata:image/{image.format.lower() if image.format else 'jpeg'};base64,{img_str}"
        
        elif operation == "grayscale":
            grayscale = image.convert('L')
            
            # Convert to base64 for return
            buffered = io.BytesIO()
            grayscale.save(buffered, format=image.format if image.format else "JPEG")
            img_str = base64.b64encode(buffered.getvalue()).decode()
            
            return f"Grayscale image:\ndata:image/{image.format.lower() if image.format else 'jpeg'};base64,{img_str}"
        
        elif operation == "blur":
            from PIL import ImageFilter
            blurred = image.filter(ImageFilter.GaussianBlur(radius=5))
            
            # Convert to base64 for return
            buffered = io.BytesIO()
            blurred.save(buffered, format=image.format if image.format else "JPEG")
            img_str = base64.b64encode(buffered.getvalue()).decode()
            
            return f"Blurred image:\ndata:image/{image.format.lower() if image.format else 'jpeg'};base64,{img_str}"
        
        else:
            return f"Unknown operation: {operation}. Available operations: info, resize, grayscale, blur"
    
    except Exception as e:
        return f"Error processing image: {str(e)}"

# Keeping your existing custom tools
@tool
def my_custom_tool(arg1:str, arg2:int)-> str:
    """A tool that does nothing yet
    
    Args:
        arg1: the first argument
        arg2: the second argument
    """
    return "What magic will you build ?"

@tool
def get_current_time_in_timezone(timezone: str) -> str:
    """A tool that fetches the current local time in a specified timezone.
    
    Args:
        timezone: A string representing a valid timezone (e.g., 'America/New_York').
    """
    try:
        # Create timezone object
        tz = pytz.timezone(timezone)
        # Get current time in that 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)}"

final_answer = FinalAnswerTool()

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

# Import tool from Hub
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,
        get_weather,
        web_scrape,
        process_image,
        get_current_time_in_timezone,
        my_custom_tool,
        image_generation_tool
    ],  # Added the new tools here
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)

GradioUI(agent).launch()