Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,23 +8,102 @@ from tools.final_answer import FinalAnswerTool
|
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
@tool
|
11 |
-
def
|
12 |
-
"""Retrieves the
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
Args:
|
15 |
city: The name of the city.
|
16 |
-
date: The date in
|
|
|
17 |
|
18 |
Returns:
|
19 |
-
A string containing the weather forecast.
|
20 |
"""
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
else:
|
27 |
-
return "
|
|
|
28 |
|
29 |
@tool
|
30 |
def calculate_days_until_date(future_date: str) -> int:
|
@@ -97,7 +176,8 @@ with open("prompts.yaml", 'r') as stream:
|
|
97 |
agent = CodeAgent(
|
98 |
model=model,
|
99 |
tools=[final_answer,
|
100 |
-
image_generation_tool,
|
|
|
101 |
get_weather_forecast,
|
102 |
calculate_days_until_date,
|
103 |
generate_travel_plan,
|
|
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
@tool
|
11 |
+
def get_weather_api_key() -> str:
|
12 |
+
"""Retrieves the OpenWeatherMap API key from an environment variable.
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
The OpenWeatherMap API key, or an error message if not found.
|
16 |
+
"""
|
17 |
+
api_key = '978b53bb6550f5b81b031a823a63f5dc'
|
18 |
+
#api_key = os.getenv("OPENWEATHERMAP_API_KEY")
|
19 |
+
if api_key:
|
20 |
+
return api_key
|
21 |
+
else:
|
22 |
+
return "Error: OpenWeatherMap API key not found in environment variables."
|
23 |
+
|
24 |
+
|
25 |
+
@tool
|
26 |
+
def get_weather_forecast(city: str, date: str, api_key: str) -> str:
|
27 |
+
"""Retrieves the weather forecast for a specified city and date using OpenWeatherMap API.
|
28 |
|
29 |
Args:
|
30 |
city: The name of the city.
|
31 |
+
date: The date inാരോഗ്യകേന്ദ്രം-MM-DD format.
|
32 |
+
api_key: Your OpenWeatherMap API key.
|
33 |
|
34 |
Returns:
|
35 |
+
A string containing the weather forecast, or an error message.
|
36 |
"""
|
37 |
+
try:
|
38 |
+
date_obj = datetime.datetime.strptime(date, "%Y-%m-%d").date()
|
39 |
+
today = datetime.date.today()
|
40 |
+
if date_obj < today:
|
41 |
+
return "Cannot get weather from past dates."
|
42 |
+
|
43 |
+
# Convert date to Unix timestamp for OpenWeatherMap API
|
44 |
+
dt = int(datetime.datetime.combine(date_obj, datetime.datetime.min.time()).timestamp())
|
45 |
+
|
46 |
+
# Get latitude and longitude of the city using geocoding API
|
47 |
+
geocoding_url = f"http://api.openweathermap.org/geo/1.0/direct?q={city}&limit=1&appid={api_key}"
|
48 |
+
geocoding_response = requests.get(geocoding_url)
|
49 |
+
geocoding_data = geocoding_response.json()
|
50 |
+
|
51 |
+
if not geocoding_data:
|
52 |
+
return f"Could not find coordinates for {city}."
|
53 |
+
|
54 |
+
lat = geocoding_data[0]["lat"]
|
55 |
+
lon = geocoding_data[0]["lon"]
|
56 |
+
|
57 |
+
# Get historical weather data using One Call API
|
58 |
+
weather_url = f"https://api.openweathermap.org/data/3.0/onecall/day_summary?lat={lat}&lon={lon}&date={dt}&appid={api_key}"
|
59 |
+
weather_response = requests.get(weather_url)
|
60 |
+
weather_data = weather_response.json()
|
61 |
+
|
62 |
+
if "weather" in weather_data:
|
63 |
+
weather_description = weather_data["weather"][0]["description"]
|
64 |
+
temp_max = weather_data["temp"]["max"]
|
65 |
+
return f"{weather_description}, with a high of {temp_max} degrees Celsius."
|
66 |
+
else:
|
67 |
+
return "Weather data not available for the specified date."
|
68 |
+
|
69 |
+
except requests.exceptions.RequestException as e:
|
70 |
+
return f"Error connecting to OpenWeatherMap API: {e}"
|
71 |
+
except ValueError:
|
72 |
+
return "Invalid date format. Please useാരോഗ്യകേന്ദ്രം-MM-DD."
|
73 |
+
except KeyError:
|
74 |
+
return "Weather data not available or API key is invalid"
|
75 |
+
|
76 |
+
def calculate_days_until_date(future_date: str) -> int:
|
77 |
+
"""Calculates the number of days until a specified future date.
|
78 |
+
|
79 |
+
Args:
|
80 |
+
future_date: The future date inാരോഗ്യകേന്ദ്രം-MM-DD format.
|
81 |
+
|
82 |
+
Returns:
|
83 |
+
The number of days until the future date.
|
84 |
+
"""
|
85 |
+
future_date_obj = datetime.datetime.strptime(future_date, "%Y-%m-%d").date()
|
86 |
+
today = datetime.date.today()
|
87 |
+
return (future_date_obj - today).days
|
88 |
+
|
89 |
+
def generate_travel_plan(weather_forecast: str, days_until_date: int, city: str) -> str:
|
90 |
+
"""Generates a travel plan based on weather, days until travel, and city.
|
91 |
+
|
92 |
+
Args:
|
93 |
+
weather_forecast: The weather forecast string.
|
94 |
+
days_until_date: The number of days until the travel date.
|
95 |
+
city: The name of the city.
|
96 |
+
|
97 |
+
Returns:
|
98 |
+
A string containing the travel plan.
|
99 |
+
"""
|
100 |
+
if "rain" in weather_forecast.lower():
|
101 |
+
return f"Since it will be rainy in {city}, pack an umbrella. You are travelling in {days_until_date} days."
|
102 |
+
elif "sunny" in weather_forecast.lower():
|
103 |
+
return f"Enjoy the sunshine in {city}! You are travelling in {days_until_date} days."
|
104 |
else:
|
105 |
+
return f"Travel plan for {city} in {days_until_date} days. Weather is {weather_forecast}."
|
106 |
+
|
107 |
|
108 |
@tool
|
109 |
def calculate_days_until_date(future_date: str) -> int:
|
|
|
176 |
agent = CodeAgent(
|
177 |
model=model,
|
178 |
tools=[final_answer,
|
179 |
+
image_generation_tool,
|
180 |
+
get_weather_api_key,
|
181 |
get_weather_forecast,
|
182 |
calculate_days_until_date,
|
183 |
generate_travel_plan,
|