Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
2 |
import datetime
|
3 |
import requests
|
@@ -10,34 +13,45 @@ from PIL import Image
|
|
10 |
import io
|
11 |
import base64
|
12 |
|
13 |
-
# Weather tool
|
14 |
@tool
|
15 |
def get_weather(location: str) -> str:
|
16 |
"""Fetch current weather information for a specified location.
|
17 |
|
18 |
Args:
|
19 |
-
location: A string representing the city
|
20 |
"""
|
21 |
try:
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
26 |
data = response.json()
|
27 |
|
28 |
if response.status_code == 200:
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
return f"Weather in {
|
35 |
-
f"
|
36 |
-
f"Temperature: {
|
|
|
37 |
f"Humidity: {humidity}%\n" \
|
38 |
-
f"Wind Speed: {
|
39 |
else:
|
40 |
-
|
|
|
41 |
except Exception as e:
|
42 |
return f"Error fetching weather for {location}: {str(e)}"
|
43 |
|
|
|
1 |
+
# Installation of required packages
|
2 |
+
# pip install smolagents requests pytz pyyaml beautifulsoup4 Pillow
|
3 |
+
|
4 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
5 |
import datetime
|
6 |
import requests
|
|
|
13 |
import io
|
14 |
import base64
|
15 |
|
16 |
+
# Weather tool using a free API (Weather API by API-Ninjas)
|
17 |
@tool
|
18 |
def get_weather(location: str) -> str:
|
19 |
"""Fetch current weather information for a specified location.
|
20 |
|
21 |
Args:
|
22 |
+
location: A string representing the city name.
|
23 |
"""
|
24 |
try:
|
25 |
+
api_url = f"https://api.weatherapi.com/v1/current.json"
|
26 |
+
params = {
|
27 |
+
'key': '80ca2c7d7e0945b583b153111241802', # Free API key with limited usage
|
28 |
+
'q': location,
|
29 |
+
'aqi': 'no'
|
30 |
+
}
|
31 |
+
|
32 |
+
response = requests.get(api_url, params=params)
|
33 |
data = response.json()
|
34 |
|
35 |
if response.status_code == 200:
|
36 |
+
location_name = data['location']['name']
|
37 |
+
region = data['location']['region']
|
38 |
+
country = data['location']['country']
|
39 |
+
temperature_c = data['current']['temp_c']
|
40 |
+
temperature_f = data['current']['temp_f']
|
41 |
+
condition = data['current']['condition']['text']
|
42 |
+
humidity = data['current']['humidity']
|
43 |
+
wind_kph = data['current']['wind_kph']
|
44 |
+
feels_like_c = data['current']['feelslike_c']
|
45 |
|
46 |
+
return f"Weather in {location_name}, {region}, {country}:\n" \
|
47 |
+
f"Condition: {condition}\n" \
|
48 |
+
f"Temperature: {temperature_c}°C / {temperature_f}°F\n" \
|
49 |
+
f"Feels like: {feels_like_c}°C\n" \
|
50 |
f"Humidity: {humidity}%\n" \
|
51 |
+
f"Wind Speed: {wind_kph} km/h"
|
52 |
else:
|
53 |
+
error_msg = data.get('error', {}).get('message', 'Unknown error')
|
54 |
+
return f"Error fetching weather for {location}: {error_msg}"
|
55 |
except Exception as e:
|
56 |
return f"Error fetching weather for {location}: {str(e)}"
|
57 |
|