Spaces:
Runtime error
Runtime error
Commit
Β·
b2326e1
1
Parent(s):
b365ccb
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -5,7 +5,7 @@ Nothing in this file is specific to Raven, code/information related to Raven can
|
|
| 5 |
|
| 6 |
For more information about the Google Maps Places API Python client, see https://github.com/googlemaps/google-maps-services-python
|
| 7 |
"""
|
| 8 |
-
from typing import Dict, List
|
| 9 |
|
| 10 |
from math import radians, cos, sin, asin, sqrt
|
| 11 |
|
|
@@ -44,19 +44,38 @@ class Tools:
|
|
| 44 |
"""
|
| 45 |
Returns the current location. ONLY use this if the user has not provided an explicit location in the query.
|
| 46 |
"""
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
country = location_data["countryCode"]
|
| 53 |
-
location = f"{city}, {region}, {country}"
|
| 54 |
-
print(f"User successfully located in {location}")
|
| 55 |
-
except:
|
| 56 |
-
location = "San Francisco, California, US"
|
| 57 |
-
print(f"Not able to find user. Defaulting to {location}")
|
| 58 |
return location
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
def sort_results(
|
| 61 |
self, places: list, sort: str, descending: bool = True, first_n: int = None
|
| 62 |
) -> List:
|
|
@@ -102,13 +121,20 @@ class Tools:
|
|
| 102 |
):
|
| 103 |
return location
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
# For response content, see https://developers.google.com/maps/documentation/places/web-service/search-find-place#find-place-responses
|
| 106 |
results = self.gmaps.find_place(
|
| 107 |
-
location,
|
|
|
|
|
|
|
| 108 |
)
|
| 109 |
if results["status"] != "OK":
|
| 110 |
return []
|
| 111 |
-
print(results)
|
| 112 |
|
| 113 |
# We always use the first candidate
|
| 114 |
place_id = results["candidates"][0]["place_id"]
|
|
|
|
| 5 |
|
| 6 |
For more information about the Google Maps Places API Python client, see https://github.com/googlemaps/google-maps-services-python
|
| 7 |
"""
|
| 8 |
+
from typing import Any, Dict, List
|
| 9 |
|
| 10 |
from math import radians, cos, sin, asin, sqrt
|
| 11 |
|
|
|
|
| 44 |
"""
|
| 45 |
Returns the current location. ONLY use this if the user has not provided an explicit location in the query.
|
| 46 |
"""
|
| 47 |
+
location_data = self._get_current_location_information()
|
| 48 |
+
city = location_data["city"]
|
| 49 |
+
region = location_data["regionName"]
|
| 50 |
+
country = location_data["countryCode"]
|
| 51 |
+
location = f"{city}, {region}, {country}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
return location
|
| 53 |
|
| 54 |
+
def _get_current_location_information(self) -> Dict[str, Any] | None:
|
| 55 |
+
default_response = {
|
| 56 |
+
"lat": "37.7577607",
|
| 57 |
+
"lon": "-122.4788854",
|
| 58 |
+
"city": "San Francisco",
|
| 59 |
+
"regionName": "California",
|
| 60 |
+
"countryCode": "US",
|
| 61 |
+
"country": "United States",
|
| 62 |
+
"region": "CA",
|
| 63 |
+
}
|
| 64 |
+
response = requests.get(
|
| 65 |
+
f"https://pro.ip-api.com/json/{self.client_ip}?key={self.config.ip_api_key}"
|
| 66 |
+
)
|
| 67 |
+
if not response.ok:
|
| 68 |
+
print(f"Not able to find user. Defaulting to {default_response}")
|
| 69 |
+
return default_response
|
| 70 |
+
|
| 71 |
+
response = response.json()
|
| 72 |
+
if response["status"] != "success":
|
| 73 |
+
print(f"Not able to find user. Defaulting to {default_response}")
|
| 74 |
+
return default_response
|
| 75 |
+
|
| 76 |
+
print(f"User successfully located in {response}")
|
| 77 |
+
return response
|
| 78 |
+
|
| 79 |
def sort_results(
|
| 80 |
self, places: list, sort: str, descending: bool = True, first_n: int = None
|
| 81 |
) -> List:
|
|
|
|
| 121 |
):
|
| 122 |
return location
|
| 123 |
|
| 124 |
+
current_loc_info = self._get_current_location_information()
|
| 125 |
+
lat = current_loc_info["lat"]
|
| 126 |
+
lng = current_loc_info["lon"]
|
| 127 |
+
|
| 128 |
+
radius_miles = 100 # Not a hyperparameter
|
| 129 |
+
radius_meters = radius_miles * 1609.34
|
| 130 |
# For response content, see https://developers.google.com/maps/documentation/places/web-service/search-find-place#find-place-responses
|
| 131 |
results = self.gmaps.find_place(
|
| 132 |
+
location,
|
| 133 |
+
input_type="textquery",
|
| 134 |
+
location_bias=f"circle:{radius_meters}@{lat},{lng}",
|
| 135 |
)
|
| 136 |
if results["status"] != "OK":
|
| 137 |
return []
|
|
|
|
| 138 |
|
| 139 |
# We always use the first candidate
|
| 140 |
place_id = results["candidates"][0]["place_id"]
|