add working weeather code and key is ok
Browse files
app.py
CHANGED
@@ -11,16 +11,13 @@ import os
|
|
11 |
from Gradio_UI import GradioUI
|
12 |
|
13 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
14 |
-
# ok my tool gets weather for
|
15 |
from dotenv import load_dotenv
|
16 |
import os
|
17 |
|
18 |
-
# Load environment variables from .env file
|
19 |
load_dotenv()
|
20 |
|
21 |
-
# Access environment variables
|
22 |
-
weather_key = os.getenv("WEATHER_APIKEY")
|
23 |
-
|
24 |
@tool
|
25 |
#Keep this format for the description / args / args description but feel free to modify
|
26 |
#it's import to specify the return type
|
@@ -32,40 +29,54 @@ weather_key = os.getenv("WEATHER_APIKEY")
|
|
32 |
# arg2: the second argument
|
33 |
# """
|
34 |
# return "What magic will you build ?"
|
35 |
-
def my_custom_get_weather_info(
|
36 |
-
"""A tool that fetches current weather information for a specified
|
37 |
Args:
|
38 |
-
location: City name or location (e.g., 'New York', 'London,UK')
|
39 |
-
units: Temperature unit system - 'metric' (Celsius)
|
40 |
"""
|
41 |
try:
|
42 |
-
#
|
43 |
-
api_key =
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
|
46 |
params = {
|
47 |
-
"
|
48 |
-
"
|
49 |
-
"units": units
|
50 |
}
|
51 |
|
52 |
response = requests.get(base_url, params=params)
|
53 |
data = response.json()
|
54 |
|
55 |
if response.status_code == 200:
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
60 |
|
61 |
-
|
|
|
|
|
62 |
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
else:
|
65 |
-
|
|
|
66 |
except Exception as e:
|
67 |
return f"Failed to retrieve weather information: {str(e)}"
|
68 |
-
|
69 |
@tool
|
70 |
def get_current_time_in_timezone(timezone: str) -> str:
|
71 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
11 |
from Gradio_UI import GradioUI
|
12 |
|
13 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
14 |
+
# ok my tool gets weather for <city>
|
15 |
from dotenv import load_dotenv
|
16 |
import os
|
17 |
|
18 |
+
# Load environment variables from .env file (used for def my_custom_get_weather_info)
|
19 |
load_dotenv()
|
20 |
|
|
|
|
|
|
|
21 |
@tool
|
22 |
#Keep this format for the description / args / args description but feel free to modify
|
23 |
#it's import to specify the return type
|
|
|
29 |
# arg2: the second argument
|
30 |
# """
|
31 |
# return "What magic will you build ?"
|
32 |
+
def my_custom_get_weather_info(city: str) -> str:
|
33 |
+
"""A tool that fetches current weather information for a specified city.
|
34 |
Args:
|
35 |
+
location: City name or location (e.g., 'New York', 'London,UK', 'London,ON', 'London,Canada')
|
36 |
+
units: Temperature unit system - 'metric' (Celsius) and 'imperial' (Fahrenheit)
|
37 |
"""
|
38 |
try:
|
39 |
+
# Get API key from environment variables
|
40 |
+
api_key = os.getenv("WEATHER_APIKEY")
|
41 |
+
if not api_key:
|
42 |
+
return "Error: WEATHER_APIKEY not found in environment variables"
|
43 |
+
|
44 |
+
# WeatherAPI.com endpoint
|
45 |
+
base_url = "http://api.weatherapi.com/v1/current.json"
|
46 |
|
47 |
params = {
|
48 |
+
"key": api_key,
|
49 |
+
"q": city,
|
|
|
50 |
}
|
51 |
|
52 |
response = requests.get(base_url, params=params)
|
53 |
data = response.json()
|
54 |
|
55 |
if response.status_code == 200:
|
56 |
+
# Extract data from the WeatherAPI.com response format
|
57 |
+
temp_c = data["current"]["temp_c"]
|
58 |
+
temp_f = data["current"]["temp_f"]
|
59 |
+
condition = data["current"]["condition"]["text"]
|
60 |
+
last_updated = data["current"]["last_updated"]
|
61 |
|
62 |
+
# Extract location information
|
63 |
+
location_name = data["location"]["name"]
|
64 |
+
country = data["location"]["country"]
|
65 |
|
66 |
+
# Build location string with all available geographic details
|
67 |
+
location_info = f"{location_name}, {country}"
|
68 |
+
|
69 |
+
# Add region/state/province if available
|
70 |
+
if "region" in data["location"] and data["location"]["region"]:
|
71 |
+
location_info = f"{location_name}, {data['location']['region']}, {country}"
|
72 |
+
|
73 |
+
# Format the output with both temperature units and location details
|
74 |
+
return f"Weather in {location_info}:\n{condition}\nTemperature: {temp_c}°C / {temp_f}°F\nLocal Time: {last_updated}"
|
75 |
else:
|
76 |
+
error_msg = data.get("error", {}).get("message", "Unknown error")
|
77 |
+
return f"Error getting weather data: {error_msg}"
|
78 |
except Exception as e:
|
79 |
return f"Failed to retrieve weather information: {str(e)}"
|
|
|
80 |
@tool
|
81 |
def get_current_time_in_timezone(timezone: str) -> str:
|
82 |
"""A tool that fetches the current local time in a specified timezone.
|