Gaiaa commited on
Commit
66a297d
·
verified ·
1 Parent(s): ae7a494

Add tool get_weather

Browse files
Files changed (1) hide show
  1. app.py +22 -7
app.py CHANGED
@@ -9,15 +9,30 @@ 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:
23
  """A tool that fetches the current local time in a specified timezone.
 
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
+ def get_weather(city: str, api_key: str) -> str:
13
+ """Fetches the current weather for a given city using the OpenWeatherMap API.
14
+
15
  Args:
16
+ city: The name of the city (e.g., 'New York').
17
+ api_key: Your OpenWeatherMap API key.
18
+
19
+ Returns:
20
+ A string containing the weather description and temperature.
21
  """
22
+ try:
23
+ url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
24
+ response = requests.get(url)
25
+ data = response.json()
26
+
27
+ if response.status_code != 200:
28
+ return f"Error fetching weather: {data.get('message', 'Unknown error')}"
29
+
30
+ weather_desc = data["weather"][0]["description"]
31
+ temp = data["main"]["temp"]
32
+ return f"The current weather in {city} is {weather_desc} with a temperature of {temp}°C."
33
+
34
+ except Exception as e:
35
+ return f"Error fetching weather data: {str(e)}"
36
  @tool
37
  def get_current_time_in_timezone(timezone: str) -> str:
38
  """A tool that fetches the current local time in a specified timezone.