Anish Tatke commited on
Commit
26c88bb
·
1 Parent(s): 9172dbf

Added Weather Tool

Browse files
Files changed (3) hide show
  1. agent.json +4 -2
  2. app.py +5 -64
  3. tools/get_weather.py +82 -0
agent.json CHANGED
@@ -2,7 +2,8 @@
2
  "tools": [
3
  "web_search",
4
  "visit_webpage",
5
- "final_answer"
 
6
  ],
7
  "model": {
8
  "class": "HfApiModel",
@@ -48,6 +49,7 @@
48
  "queue",
49
  "time",
50
  "collections",
51
- "re"
 
52
  ]
53
  }
 
2
  "tools": [
3
  "web_search",
4
  "visit_webpage",
5
+ "final_answer",
6
+ "get_weather"
7
  ],
8
  "model": {
9
  "class": "HfApiModel",
 
49
  "queue",
50
  "time",
51
  "collections",
52
+ "re",
53
+ "requests"
54
  ]
55
  }
app.py CHANGED
@@ -3,15 +3,15 @@ import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
- from geopy.geocoders import Nominatim
7
- from timezonefinder import TimezoneFinder
8
  from tools.final_answer import FinalAnswerTool
 
9
 
10
  from Gradio_UI import GradioUI
11
 
12
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
13
  @tool
14
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
15
  #Keep this format for the description / args / args description but feel free to modify the tool
16
  """A tool that does nothing yet
17
  Args:
@@ -34,69 +34,10 @@ def get_current_time_in_timezone(timezone: str) -> str:
34
  return f"The current local time in {timezone} is: {local_time}"
35
  except Exception as e:
36
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
37
-
38
- def get_lat_long(address: str) -> tuple | str:
39
- """
40
- Get the latitude and longitude of a given address
41
- Args:
42
- address: A string representing a valid address (e.g., '1600 Amphitheatre Parkway, Mountain View, CA').
43
- """
44
- if not address:
45
- return "No address provided", "No address provided"
46
-
47
- geolocator = Nominatim(user_agent="myGeocoder", timeout=10)
48
- try:
49
- location = geolocator.geocode(address)
50
- return location.latitude, location.longitude
51
- except:
52
- return "Location not found", "Location not found"
53
-
54
-
55
- def get_timezone(lat: float, long: float) -> str:
56
- """
57
- Get the timezone of a given latitude and longitude
58
- Args:
59
- lat: A float representing the latitude of a location.
60
- long: A float representing the longitude of a location.
61
- """
62
- if not lat or not long:
63
- return "No latitude or longitude provided"
64
-
65
- tf = TimezoneFinder()
66
- try:
67
- timezone_str = tf.timezone_at(lng=long, lat=lat)
68
- return timezone_str
69
- except:
70
- return "Timezone not found"
71
-
72
- @tool
73
- def get_weather(location: str) -> str:
74
- """
75
- Get the weather of a given latitude and longitude
76
- Args:
77
- location: A string representing a valid location (e.g., '1600 Amphitheatre Parkway, Mountain View, CA').
78
- """
79
- if not location:
80
- return "No location provided"
81
-
82
- lat, long = get_lat_long(location)
83
- if lat == "Location not found" or long == "Location not found":
84
- return "Location not found"
85
-
86
- timezone = get_timezone(lat, long)
87
- if timezone == "Timezone not found":
88
- return "Timezone not found"
89
-
90
- api_call = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={long}&current=temperature_2m,apparent_temperature,precipitation,wind_speed_10m&timezone={timezone}&past_days=1&forecast_days=1"
91
- try:
92
- response = requests.get(api_call)
93
- data = response.json()
94
- return f"Temperature: {data['current']['temperature_2m']}{data['current_units']['temperature_2m']}\n" + f"Feels like: {data['current']['apparent_temperature']}{data['current_units']['apparent_temperature']}\n" + f"Precipitation: {data['current']['precipitation']}{data['current_units']['precipitation']}\n" + f"Wind speed: {data['current']['wind_speed_10m']}{data['current_units']['wind_speed_10m']}"
95
- except Exception as e:
96
- return "Error fetching weather data"
97
 
98
 
99
  final_answer = FinalAnswerTool()
 
100
 
101
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
102
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
@@ -117,7 +58,7 @@ with open("prompts.yaml", 'r') as stream:
117
 
118
  agent = CodeAgent(
119
  model=model,
120
- tools=[final_answer], ## add your tools here (don't remove final answer)
121
  max_steps=6,
122
  verbosity_level=1,
123
  grammar=None,
 
3
  import requests
4
  import pytz
5
  import yaml
6
+
 
7
  from tools.final_answer import FinalAnswerTool
8
+ from tools.get_weather import GetWeather
9
 
10
  from Gradio_UI import GradioUI
11
 
12
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
13
  @tool
14
+ def my_custom_tool(arg1:str, arg2:int)-> str: #it's important to specify the return type
15
  #Keep this format for the description / args / args description but feel free to modify the tool
16
  """A tool that does nothing yet
17
  Args:
 
34
  return f"The current local time in {timezone} is: {local_time}"
35
  except Exception as e:
36
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
 
39
  final_answer = FinalAnswerTool()
40
+ get_weather = GetWeather()
41
 
42
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
43
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
 
58
 
59
  agent = CodeAgent(
60
  model=model,
61
+ tools=[get_weather, final_answer], ## add your tools here (don't remove final answer)
62
  max_steps=6,
63
  verbosity_level=1,
64
  grammar=None,
tools/get_weather.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import Tool
2
+
3
+ class GetWeather(Tool):
4
+ name: str = "get_weather"
5
+ description: str = "Get the weather of a given location."
6
+ inputs: dict = {'location': {'type': 'string', 'description': 'The location to get the weather of.'}}
7
+ output_type: str = "string"
8
+
9
+ def forward(self, location: str) -> str:
10
+ import requests
11
+ if not location:
12
+ return "No location provided"
13
+
14
+ lat, long = self.get_lat_long(location)
15
+ if lat == "Location not found" or long == "Location not found":
16
+ return "Location not found"
17
+
18
+ timezone = self.get_timezone(lat, long)
19
+ if timezone == "Timezone not found":
20
+ return "Timezone not found"
21
+
22
+ api_call = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={long}&current=temperature_2m,apparent_temperature,precipitation,wind_speed_10m&timezone={timezone}&past_days=1&forecast_days=1"
23
+ try:
24
+ response = requests.get(api_call)
25
+ data = response.json()
26
+ return f"Temperature: {data['current']['temperature_2m']}{data['current_units']['temperature_2m']}\n" + f"Feels like: {data['current']['apparent_temperature']}{data['current_units']['apparent_temperature']}\n" + f"Precipitation: {data['current']['precipitation']}{data['current_units']['precipitation']}\n" + f"Wind speed: {data['current']['wind_speed_10m']}{data['current_units']['wind_speed_10m']}"
27
+ except Exception as e:
28
+ return "Error fetching weather data"
29
+
30
+
31
+ def get_lat_long(self, address: str) -> tuple | str:
32
+ """
33
+ Get the latitude and longitude of a given address
34
+ Args:
35
+ address: A string representing a valid address (e.g., '1600 Amphitheatre Parkway, Mountain View, CA').
36
+ """
37
+ try:
38
+ from geopy.geocoders import Nominatim
39
+ except ImportError as e:
40
+ raise ImportError(
41
+ "You must install the `geopy` package to run this tool: for instance run `pip install geopy`."
42
+ )
43
+
44
+ if not address:
45
+ return "No address provided", "No address provided"
46
+
47
+ geolocator = Nominatim(user_agent="myGeocoder", timeout=10)
48
+ try:
49
+ location = geolocator.geocode(address)
50
+ return location.latitude, location.longitude
51
+ except:
52
+ return "Location not found", "Location not found"
53
+
54
+
55
+ def get_timezone(self, lat: float, long: float) -> str:
56
+ """
57
+ Get the timezone of a given latitude and longitude
58
+ Args:
59
+ lat: A float representing the latitude of a location.
60
+ long: A float representing the longitude of a location.
61
+ """
62
+ try:
63
+ from timezonefinder import TimezoneFinder
64
+ except ImportError as e:
65
+ raise ImportError(
66
+ "You must install the `timezonefinder` package to run this tool: for instance run `pip install timezonefinder`."
67
+ )
68
+
69
+
70
+ if not lat or not long:
71
+ return "No latitude or longitude provided"
72
+
73
+ tf = TimezoneFinder()
74
+ try:
75
+ timezone_str = tf.timezone_at(lng=long, lat=lat)
76
+ return timezone_str
77
+ except:
78
+ return "Timezone not found"
79
+
80
+ def __init__(self, *args, **kwargs):
81
+ self.is_initialized = False
82
+