Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import datetime
|
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
|
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
@@ -18,12 +19,34 @@ def get_weather(city: str) -> str:
|
|
18 |
Returns:
|
19 |
str: A string containing the weather information or an error message.
|
20 |
"""
|
21 |
-
|
|
|
|
|
|
|
|
|
22 |
response = requests.get(api_url)
|
23 |
|
24 |
if response.status_code == 200:
|
25 |
data = response.json()
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
else:
|
28 |
return "Error: Unable to fetch weather data."
|
29 |
|
|
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
6 |
+
import os
|
7 |
from tools.final_answer import FinalAnswerTool
|
8 |
|
9 |
from Gradio_UI import GradioUI
|
|
|
19 |
Returns:
|
20 |
str: A string containing the weather information or an error message.
|
21 |
"""
|
22 |
+
api_key = os.getenv('API_KEY')
|
23 |
+
if not api_key:
|
24 |
+
return "Error: API Key not found in environment variables."
|
25 |
+
|
26 |
+
api_url = f"http://api.weatherapi.com/v1/current.json?key={api_key}&q={city}&aqi=no"
|
27 |
response = requests.get(api_url)
|
28 |
|
29 |
if response.status_code == 200:
|
30 |
data = response.json()
|
31 |
+
|
32 |
+
location = data.get("location", {})
|
33 |
+
current_weather = data.get("current", {})
|
34 |
+
|
35 |
+
city_name = location.get("name", "Unknown")
|
36 |
+
country = location.get("country", "Unknown")
|
37 |
+
temperature_c = current_weather.get("temp_c", "N/A")
|
38 |
+
condition = current_weather.get("condition", {}).get("text", "N/A")
|
39 |
+
wind_speed_kph = current_weather.get("wind_kph", "N/A")
|
40 |
+
humidity = current_weather.get("humidity", "N/A")
|
41 |
+
|
42 |
+
weather_info = (
|
43 |
+
f"Weather in {city_name}, {country}:\n"
|
44 |
+
f"Temperature: {temperature_c}°C\n"
|
45 |
+
f"Condition: {condition}\n"
|
46 |
+
f"Wind Speed: {wind_speed_kph} kph\n"
|
47 |
+
f"Humidity: {humidity}%"
|
48 |
+
)
|
49 |
+
return weather_info
|
50 |
else:
|
51 |
return "Error: Unable to fetch weather data."
|
52 |
|