alamanna commited on
Commit
4ecc9ce
·
verified ·
1 Parent(s): 2b7e86c

Create functionalities.py

Browse files

Create the functionalities.py file

Files changed (1) hide show
  1. functionalities.py +61 -0
functionalities.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import tool
2
+ import requests
3
+
4
+ @tool
5
+ def suggest_outfit(occasion: str) -> str:
6
+ if occasion == "casual":
7
+ return "T‑shirt, Jeans and sneakers."
8
+ elif occasion == "formal":
9
+ return "White shirt, tie, blue suit and Oxford shoes."
10
+ elif occasion == "active":
11
+ return "Jogging trousers, T‑shirt and trainers."
12
+ else:
13
+ return "Custom outfit for the fashion advisor."
14
+
15
+ @tool
16
+ def define_coordinates(city: str) -> dict:
17
+ # calls Open‑Meteo geocoding API
18
+ city = city.strip()
19
+ if not city:
20
+ return {"error": "City name cannot be empty."}
21
+ url = f"https://geocoding‑api.open‑meteo.com/v1/search?name={city}&count=1"
22
+ resp = requests.get(url); data = resp.json()
23
+ if not data.get("results"):
24
+ return {"error": f"No location found for city: {city}"}
25
+ r = data["results"][0]
26
+ return {"latitude": r["latitude"], "longitude": r["longitude"], "timezone": r["timezone"]}
27
+
28
+ @tool
29
+ def get_weather(city: str) -> dict:
30
+ coords = define_coordinates(city)
31
+ if "error" in coords:
32
+ return {"error": coords["error"]}
33
+ url = (
34
+ f"https://api.open‑meteo.com/v1/forecast?"
35
+ f"latitude={coords['latitude']}&longitude={coords['longitude']}"
36
+ f"&current_weather=true&timezone={coords['timezone']}"
37
+ )
38
+ resp = requests.get(url); data = resp.json()
39
+ cw = data["current_weather"]
40
+ code = cw["weathercode"]
41
+ # classify code
42
+ (desc := (
43
+ "clear sky" if code==0 else
44
+ "mainly clear to partly cloudy" if 1<=code<=3 else
45
+ "fog" if code in (45,48) else
46
+ "drizzle" if 51<=code<=57 else
47
+ "rain" if 61<=code<=67 else
48
+ "snow" if 71<=code<=77 else
49
+ "rain showers" if 80<=code<=82 else
50
+ "snow showers" if 85<=code<=86 else
51
+ "thunderstorm" if code==95 else
52
+ "thunderstorm with hail" if 96<=code<=99 else
53
+ "unknown"
54
+ ))
55
+ return {
56
+ "temperature": cw["temperature"],
57
+ "wind_speed": cw["windspeed"],
58
+ "weather_code": code,
59
+ "description": desc,
60
+ "summary": f"Temperature in {city}: {cw['temperature']}°C, wind {cw['windspeed']} km/h, {desc}."
61
+ }