cjb97 commited on
Commit
64a5985
·
verified ·
1 Parent(s): ae7a494

Updated to get weather forecast for a zip code

Browse files
Files changed (1) hide show
  1. app.py +43 -1
app.py CHANGED
@@ -3,7 +3,11 @@ 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
9
 
@@ -33,6 +37,44 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  final_answer = FinalAnswerTool()
38
 
@@ -55,7 +97,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
3
  import requests
4
  import pytz
5
  import yaml
6
+ from smartystreets_python_sdk import StaticCredentials, exceptions, ClientBuilder
7
+ from smartystreets_python_sdk.us_street import Lookup as StreetLookup
8
+ from smartystreets_python_sdk.us_zipcode import Lookup as ZipcodeLookup
9
  from tools.final_answer import FinalAnswerTool
10
+ import os
11
 
12
  from Gradio_UI import GradioUI
13
 
 
37
  except Exception as e:
38
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
39
 
40
+ @tool
41
+ def get_weather_by_zipcode(zipcode: str) -> str:
42
+ """Get weather forecast for a given US zip code
43
+ Args:
44
+ zipcode: A 5-digit US zip code
45
+ """
46
+ try:
47
+ # Validate zipcode
48
+ if not (zipcode.isdigit() and len(zipcode) == 5):
49
+ return "Please provide a valid 5-digit US zip code"
50
+
51
+ # Get forecast data using WeatherAPI.com
52
+ weather_api_key = os.getenv('WEATHERAPI_KEY')
53
+ url = f"http://api.weatherapi.com/v1/forecast.json?key={weather_api_key}&q={zipcode}&days=3&aqi=no"
54
+
55
+ response = requests.get(url)
56
+ if response.status_code != 200:
57
+ return "Error fetching weather data"
58
+
59
+ weather_data = response.json()
60
+ location = weather_data['location']['name']
61
+ forecast = weather_data['forecast']['forecastday']
62
+
63
+ result = f"3-Day Forecast for {location} ({zipcode}):\n\n"
64
+
65
+ for day in forecast:
66
+ date = day['date']
67
+ max_temp = day['day']['maxtemp_f']
68
+ min_temp = day['day']['mintemp_f']
69
+ condition = day['day']['condition']['text']
70
+ result += f"{date}:\n"
71
+ result += f"High: {max_temp}°F, Low: {min_temp}°F\n"
72
+ result += f"Conditions: {condition}\n\n"
73
+
74
+ return result
75
+
76
+ except Exception as e:
77
+ return f"Error getting weather: {str(e)}"
78
 
79
  final_answer = FinalAnswerTool()
80
 
 
97
 
98
  agent = CodeAgent(
99
  model=model,
100
+ tools=[get_weather_by_zipcode, final_answer], # Added weather tool
101
  max_steps=6,
102
  verbosity_level=1,
103
  grammar=None,