Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- utils/__pycache__/geo.cpython-313.pyc +0 -0
- utils/geo.py +29 -21
utils/__pycache__/geo.cpython-313.pyc
CHANGED
|
Binary files a/utils/__pycache__/geo.cpython-313.pyc and b/utils/__pycache__/geo.cpython-313.pyc differ
|
|
|
utils/geo.py
CHANGED
|
@@ -1,21 +1,29 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
"
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
|
| 3 |
+
_geo_cache = {}
|
| 4 |
+
|
| 5 |
+
def city_to_coordinates(city):
|
| 6 |
+
"""Convert a city name to (lat, lon) using Open-Meteo's geocoding API."""
|
| 7 |
+
if city in _geo_cache:
|
| 8 |
+
return _geo_cache[city]
|
| 9 |
+
|
| 10 |
+
url = "https://geocoding-api.open-meteo.com/v1/search"
|
| 11 |
+
params = {
|
| 12 |
+
"name": city.strip(),
|
| 13 |
+
"count": 1,
|
| 14 |
+
"format": "json"
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
response = requests.get(url, params=params, timeout=10)
|
| 19 |
+
response.raise_for_status()
|
| 20 |
+
data = response.json()
|
| 21 |
+
results = data.get("results")
|
| 22 |
+
if not results:
|
| 23 |
+
return None
|
| 24 |
+
coords = (results[0]["latitude"], results[0]["longitude"])
|
| 25 |
+
_geo_cache[city] = coords
|
| 26 |
+
return coords
|
| 27 |
+
except (requests.RequestException, ValueError) as e:
|
| 28 |
+
print(f"city_to_coordinates failed for '{city}': {e}")
|
| 29 |
+
return None
|