Spaces:
Sleeping
Sleeping
File size: 2,934 Bytes
4ecc9ce 0659dec c792f80 0659dec c792f80 0659dec 4ecc9ce 0659dec c792f80 0659dec c792f80 0659dec 4ecc9ce 0659dec c792f80 0659dec c792f80 0659dec 4ecc9ce |
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 |
from smolagents import tool
import requests
@tool
def suggest_outfit(occasion: str) -> str:
"""
Suggests an outfit based on the occasion type.
Args:
occasion (str): One of "casual", "formal", "active", or "custom".
Returns:
str: A recommended outfit description suitable for the occasion.
"""
if occasion == "casual":
return "T‑shirt, Jeans and sneakers."
elif occasion == "formal":
return "White shirt, tie, blue suit and Oxford shoes."
elif occasion == "active":
return "Jogging trousers, T‑shirt and trainers."
else:
return "Custom outfit for the fashion advisor."
@tool
def define_coordinates(city: str) -> dict:
"""
Looks up geographic coordinates and timezone for a city.
Args:
city (str): Name of the city to geocode.
Returns:
dict: Contains "latitude", "longitude", and "timezone",
or {"error": "..."} if lookup fails.
"""
city = city.strip()
if not city:
return {"error": "City name cannot be empty."}
url = f"https://geocoding‑api.open‑meteo.com/v1/search?name={city}&count=1"
resp = requests.get(url); data = resp.json()
if not data.get("results"):
return {"error": f"No location found for city: {city}"}
r = data["results"][0]
return {"latitude": r["latitude"], "longitude": r["longitude"], "timezone": r["timezone"]}
@tool
def get_weather(city: str) -> dict:
"""
Retrieves current weather (temp, wind, code, description) for a city.
Args:
city (str): Name of the city to fetch weather for.
Returns:
dict: Keys include "temperature", "wind_speed", "weather_code",
"description", and "summary", or {"error": "..."} if failure.
"""
coords = define_coordinates(city)
if "error" in coords:
return {"error": coords["error"]}
url = (
f"https://api.open‑meteo.com/v1/forecast?"
f"latitude={coords['latitude']}&longitude={coords['longitude']}"
f"¤t_weather=true&timezone={coords['timezone']}"
)
resp = requests.get(url); data = resp.json()
cw = data["current_weather"]
code = cw["weathercode"]
# classify code
(desc := (
"clear sky" if code==0 else
"mainly clear to partly cloudy" if 1<=code<=3 else
"fog" if code in (45,48) else
"drizzle" if 51<=code<=57 else
"rain" if 61<=code<=67 else
"snow" if 71<=code<=77 else
"rain showers" if 80<=code<=82 else
"snow showers" if 85<=code<=86 else
"thunderstorm" if code==95 else
"thunderstorm with hail" if 96<=code<=99 else
"unknown"
))
return {
"temperature": cw["temperature"],
"wind_speed": cw["windspeed"],
"weather_code": code,
"description": desc,
"summary": f"Temperature in {city}: {cw['temperature']}°C, wind {cw['windspeed']} km/h, {desc}."
}
|