agercas commited on
Commit
1fd30b9
·
1 Parent(s): ae7a494

add a weather tool

Browse files
Files changed (1) hide show
  1. app.py +70 -8
app.py CHANGED
@@ -7,16 +7,78 @@ from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
 
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -55,7 +117,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
7
 
8
  from Gradio_UI import GradioUI
9
 
 
10
  @tool
11
+ def get_real_weather(latitude: float, longitude: float) -> str:
12
+ """
13
+ Fetches real-time weather data for a given location using the Open-Meteo API.
14
+
15
  Args:
16
+ latitude: The latitude of the location
17
+ longitude: The longitude of the location
18
  """
19
+ import requests
20
+ import json
21
+ from datetime import datetime
22
+
23
+ base_url = "https://api.open-meteo.com/v1/forecast"
24
+ params = {
25
+ "latitude": latitude,
26
+ "longitude": longitude,
27
+ "current": "temperature_2m,relative_humidity_2m,wind_speed_10m,weather_code",
28
+ "timezone": "auto"
29
+ }
30
+
31
+ try:
32
+ response = requests.get(base_url, params=params)
33
+ response.raise_for_status()
34
+ data = response.json()
35
+
36
+ current = data["current"]
37
+ time = datetime.fromisoformat(current["time"])
38
+ temperature = current["temperature_2m"]
39
+ humidity = current["relative_humidity_2m"]
40
+ wind_speed = current["wind_speed_10m"]
41
+ weather_code = current["weather_code"]
42
+
43
+ weather_descriptions = {
44
+ 0: "Clear sky",
45
+ 1: "Mainly clear",
46
+ 2: "Partly cloudy",
47
+ 3: "Overcast",
48
+ 45: "Fog",
49
+ 48: "Depositing rime fog",
50
+ 51: "Light drizzle",
51
+ 53: "Moderate drizzle",
52
+ 55: "Dense drizzle",
53
+ 61: "Slight rain",
54
+ 63: "Moderate rain",
55
+ 65: "Heavy rain",
56
+ 71: "Slight snow fall",
57
+ 73: "Moderate snow fall",
58
+ 75: "Heavy snow fall",
59
+ 77: "Snow grains",
60
+ 80: "Slight rain showers",
61
+ 81: "Moderate rain showers",
62
+ 82: "Violent rain showers",
63
+ 85: "Slight snow showers",
64
+ 86: "Heavy snow showers",
65
+ 95: "Thunderstorm",
66
+ 96: "Thunderstorm with slight hail",
67
+ 99: "Thunderstorm with heavy hail"
68
+ }
69
+
70
+ weather_description = weather_descriptions.get(weather_code, "Unknown")
71
+
72
+ return f"""Weather report for {latitude}, {longitude}:
73
+ Time: {time.strftime('%Y-%m-%d %H:%M:%S')}
74
+ Temperature: {temperature}°C
75
+ Humidity: {humidity}%
76
+ Wind Speed: {wind_speed} m/s
77
+ Conditions: {weather_description}"""
78
+
79
+ except requests.exceptions.RequestException as e:
80
+ return f"Error fetching weather data: {str(e)}"
81
+
82
 
83
  @tool
84
  def get_current_time_in_timezone(timezone: str) -> str:
 
117
 
118
  agent = CodeAgent(
119
  model=model,
120
+ tools=[final_answer, get_current_time_in_timezone, get_real_weather],
121
  max_steps=6,
122
  verbosity_level=1,
123
  grammar=None,