Sebastian Wefers commited on
Commit
14cab04
·
1 Parent(s): c473949

altered WorldTides fucntion to accept location name

Browse files
Files changed (1) hide show
  1. app.py +27 -15
app.py CHANGED
@@ -24,36 +24,48 @@ except ModuleNotFoundError:
24
 
25
 
26
  @tool
27
- def get_tides_for_date(lat: float, lon: float, date: str = "today") -> str:
28
  """Fetches the high and low tides for a specific date at a given location.
29
 
30
  Args:
31
- lat: The latitude of the location.
32
- lon: The longitude of the location.
33
  date: The date for which to fetch tide data (format: 'YYYY-MM-DD' or 'today').
34
 
35
  Returns:
36
- A string containing high and low tide times for the specified date.
37
-
38
- Example:
39
- get_tides_for_date(36.97, -122.02, "2025-02-20") # Santa Cruz, CA on Feb 20
40
  """
41
  try:
42
- API_KEY = os.environ.get("WORLD_TIDES_KEY")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  if date == "today":
44
  date = datetime.datetime.utcnow().strftime("%Y-%m-%d") # Get today's date in UTC
45
 
46
- url = f"https://www.worldtides.info/api/v3?extremes&lat={lat}&lon={lon}&date={date}&key={API_KEY}"
47
- response = requests.get(url).json()
48
 
49
- if "extremes" not in response:
50
- return f"Could not fetch tide data for {date}. Ensure the location and date are valid."
51
 
52
- tides = response["extremes"]
53
  if not tides:
54
- return f"No tide data available for {date} at this location."
55
 
56
- result = f"Tide data for {date}:\n"
57
  for tide in tides:
58
  time_utc = tide["date"]
59
  tide_type = "High Tide" if tide["type"] == "High" else "Low Tide"
 
24
 
25
 
26
  @tool
27
+ def get_tides_for_location(location: str, date: str = "today") -> str:
28
  """Fetches the high and low tides for a specific date at a given location.
29
 
30
  Args:
31
+ location: The name of the city or place (e.g., 'San Francisco').
 
32
  date: The date for which to fetch tide data (format: 'YYYY-MM-DD' or 'today').
33
 
34
  Returns:
35
+ A string containing high and low tide times for the specified date and location.
 
 
 
36
  """
37
  try:
38
+ # Load API key from environment variable
39
+ API_KEY = os.getenv("WORLD_TIDES_KEY")
40
+
41
+ if not API_KEY:
42
+ return "API key is missing. Please set WORLDTIDES_API_KEY in Hugging Face Secrets."
43
+
44
+ # Step 1: Convert location name to latitude & longitude
45
+ geo_url = f"https://nominatim.openstreetmap.org/search?format=json&q={location}"
46
+ geo_response = requests.get(geo_url).json()
47
+
48
+ if not geo_response:
49
+ return f"Could not find coordinates for {location}."
50
+
51
+ lat = geo_response[0]["lat"]
52
+ lon = geo_response[0]["lon"]
53
+
54
+ # Step 2: Fetch tide data
55
  if date == "today":
56
  date = datetime.datetime.utcnow().strftime("%Y-%m-%d") # Get today's date in UTC
57
 
58
+ tide_url = f"https://www.worldtides.info/api/v3?extremes&lat={lat}&lon={lon}&date={date}&key={API_KEY}"
59
+ tide_response = requests.get(tide_url).json()
60
 
61
+ if "extremes" not in tide_response:
62
+ return f"Could not fetch tide data for {location} on {date}."
63
 
64
+ tides = tide_response["extremes"]
65
  if not tides:
66
+ return f"No tide data available for {location} on {date}."
67
 
68
+ result = f"Tide data for {location} on {date}:\n"
69
  for tide in tides:
70
  time_utc = tide["date"]
71
  tide_type = "High Tide" if tide["type"] == "High" else "Low Tide"