Spaces:
Sleeping
Sleeping
tool changed to get weather
Browse files
app.py
CHANGED
|
@@ -9,14 +9,44 @@ 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
|
| 13 |
-
"""A tool that
|
| 14 |
Args:
|
| 15 |
-
|
| 16 |
"""
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 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)-> str:
|
| 13 |
+
"""A tool that gets the weather in a city
|
| 14 |
Args:
|
| 15 |
+
city: A string representing a valid city
|
| 16 |
"""
|
| 17 |
+
if not city:
|
| 18 |
+
return "enter a city"
|
| 19 |
+
|
| 20 |
+
url = "https://api.openweathermap.org/data/2.5/weather"
|
| 21 |
+
|
| 22 |
+
params = {
|
| 23 |
+
"q": city,
|
| 24 |
+
"appid": API_KEY,
|
| 25 |
+
"units": "imperial"
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
response = requests.get(url, params=params)
|
| 29 |
+
|
| 30 |
+
if response.status_code != 200:
|
| 31 |
+
return "City not found or API error."
|
| 32 |
+
|
| 33 |
+
data = response.json()
|
| 34 |
+
|
| 35 |
+
temp = data["main"]["temp"]
|
| 36 |
+
feels_like = data["main"]["feels_like"]
|
| 37 |
+
humidity = data["main"]["humidity"]
|
| 38 |
+
description = data["weather"][0]["description"]
|
| 39 |
+
|
| 40 |
+
result = f"""
|
| 41 |
+
Weather in {city.title()}:
|
| 42 |
+
|
| 43 |
+
- Temperature: {temp} °F
|
| 44 |
+
- Feels Like: {feels_like} °F
|
| 45 |
+
- Humidity: {humidity}%
|
| 46 |
+
- Condition: {description.title()}
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
return result
|
| 50 |
|
| 51 |
@tool
|
| 52 |
def get_current_time_in_timezone(timezone: str) -> str:
|