kolkhi commited on
Commit
31247db
·
verified ·
1 Parent(s): b148632

Add fetching of coordinates by location name

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -3,21 +3,27 @@ import datetime
3
  import requests
4
  import pytz
5
  import yaml
 
6
  from tools.final_answer import FinalAnswerTool
7
  from tools.visit_webpage import VisitWebpageTool
8
 
9
  from Gradio_UI import GradioUI
10
 
 
 
 
 
 
11
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
12
  @tool
13
- def sunrise_sunset_tool(lat:str, lng:str)-> str: #it's import to specify the return type
14
  #Keep this format for the description / args / args description but feel free to modify the tool
15
- """A tool that returns sunrise and sunset times for location specified by latitude and longitude
16
  Args:
17
- lat: A string representing location latitude
18
- lng: A string representing location longitude
19
  """
20
  try:
 
21
  api_url = "https://api.sunrise-sunset.org/json"
22
  payload = {'lat': lat, 'lng': lng}
23
  ret = requests.get(api_url, params=payload)
@@ -29,7 +35,7 @@ def sunrise_sunset_tool(lat:str, lng:str)-> str: #it's import to specify the ret
29
  solar_noon = js["results"]["solar_noon"]
30
  time_zone = js["tzid"]
31
  elif ret.status_code == 200:
32
- return f"Error fetching sunrise/sunset data for location lat={lat} long={lng}. Error code = {js["status"]}"
33
  else:
34
  return f"Error fetching sunrise/sunset data for location lat={lat} long={lng}. Error code = {ret.status_code}, response={js}"
35
 
 
3
  import requests
4
  import pytz
5
  import yaml
6
+ from geopy.geocoders import Nominatim
7
  from tools.final_answer import FinalAnswerTool
8
  from tools.visit_webpage import VisitWebpageTool
9
 
10
  from Gradio_UI import GradioUI
11
 
12
+ def get_city_coordinates(city: str) ->tuple[str, str]:
13
+ geolocator = Nominatim(user_agent="test_agent")
14
+ location = geolocator.geocode(city)
15
+ return (str(location.latitude), str(location.longitude))
16
+
17
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
18
  @tool
19
+ def sunrise_sunset_tool(city:str)-> str: #it's import to specify the return type
20
  #Keep this format for the description / args / args description but feel free to modify the tool
21
+ """A tool that returns sunrise and sunset times for location specified by name
22
  Args:
23
+ city: A string representing location name
 
24
  """
25
  try:
26
+ lat, lng = get_city_coordinates()
27
  api_url = "https://api.sunrise-sunset.org/json"
28
  payload = {'lat': lat, 'lng': lng}
29
  ret = requests.get(api_url, params=payload)
 
35
  solar_noon = js["results"]["solar_noon"]
36
  time_zone = js["tzid"]
37
  elif ret.status_code == 200:
38
+ return f"Error fetching sunrise/sunset data for location lat={lat} long={lng}. Error code = {js['status']}"
39
  else:
40
  return f"Error fetching sunrise/sunset data for location lat={lat} long={lng}. Error code = {ret.status_code}, response={js}"
41