ddeng1990 commited on
Commit
2703cc3
·
verified ·
1 Parent(s): bfd1690

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -27
app.py CHANGED
@@ -16,12 +16,9 @@ def get_weather_api_key() -> str:
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.
@@ -72,37 +69,60 @@ def get_weather_forecast(city: str, date: str, api_key: str) -> str:
72
  return "Invalid date format. Please use YYYY-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 YYYY-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
 
16
  """
17
  api_key = '978b53bb6550f5b81b031a823a63f5dc'
18
  #api_key = os.getenv("OPENWEATHERMAP_API_KEY")
 
 
 
 
 
19
 
20
+ return api_key
21
+ '''
22
  @tool
23
  def get_weather_forecast(city: str, date: str, api_key: str) -> str:
24
  """Retrieves the weather forecast for a specified city and date using OpenWeatherMap API.
 
69
  return "Invalid date format. Please use YYYY-MM-DD."
70
  except KeyError:
71
  return "Weather data not available or API key is invalid"
72
+ '''
73
 
74
+ @tool
75
+ def get_weather_forecast(city: str, date: str, api_key: str) -> str:
76
+ """Retrieves the weather forecast for a specified city and date using OpenWeatherMap API.
77
 
78
  Args:
79
+ city: The name of the city.
80
+ date: The date in YYYY-MM-DD format.
81
+ api_key: Your OpenWeatherMap API key.
82
 
83
  Returns:
84
+ A string containing the weather forecast, or an error message.
85
  """
86
+ try:
87
+ date_obj = datetime.datetime.strptime(date, "%Y-%m-%d").date()
88
+ today = datetime.date.today()
89
+ if date_obj < today:
90
+ return "Cannot get weather from past dates."
91
 
92
+ # Convert date to Unix timestamp for OpenWeatherMap API
93
+ dt = int(datetime.datetime.combine(date_obj, datetime.datetime.min.time()).timestamp())
94
 
95
+ # Get latitude and longitude of the city using geocoding API
96
+ geocoding_url = f"http://api.openweathermap.org/geo/1.0/direct?q={city}&limit=1&appid={api_key}"
97
+ geocoding_response = requests.get(geocoding_url)
98
+ geocoding_data = geocoding_response.json()
99
 
100
+ if not geocoding_data:
101
+ return f"Could not find coordinates for {city}."
102
+
103
+ lat = geocoding_data[0]["lat"]
104
+ lon = geocoding_data[0]["lon"]
105
+
106
+ # Get weather data using One Call API day_summary endpoint
107
+ weather_url = f"https://api.openweathermap.org/data/3.0/onecall/day_summary?lat={lat}&lon={lon}&date={dt}&appid={api_key}"
108
+ weather_response = requests.get(weather_url)
109
+ weather_data = weather_response.json()
110
+
111
+ if "weather" in weather_data and weather_data["weather"]:
112
+ weather_description = weather_data["weather"][0]["description"]
113
+ temp_max = weather_data["temp"]["max"]
114
+ return f"{weather_description}, with a high of {temp_max} degrees Celsius."
115
+ elif "message" in weather_data:
116
+ return f"OpenWeatherMap API Error: {weather_data['message']}"
117
+ else:
118
+ return "Weather data not available for the specified date."
119
+
120
+ except requests.exceptions.RequestException as e:
121
+ return f"Error connecting to OpenWeatherMap API: {e}"
122
+ except ValueError:
123
+ return "Invalid date format. Please use YYYY-MM-DD."
124
+ except KeyError:
125
+ return "Weather data not available or API key is invalid"
126
 
127
 
128
  @tool