Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,18 +56,22 @@ def get_weather(location: str) -> str | None:
|
|
| 56 |
pass
|
| 57 |
return None
|
| 58 |
|
|
|
|
|
|
|
| 59 |
def extract_location(text: str) -> str | None:
|
| 60 |
"""
|
| 61 |
-
|
| 62 |
-
Works for
|
| 63 |
"""
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
return max(matches, key=len) if matches else None
|
| 71 |
|
| 72 |
|
| 73 |
def crop_recommendation(query: str, weather: str | None = None) -> str:
|
|
|
|
| 56 |
pass
|
| 57 |
return None
|
| 58 |
|
| 59 |
+
import requests
|
| 60 |
+
|
| 61 |
def extract_location(text: str) -> str | None:
|
| 62 |
"""
|
| 63 |
+
Use Open-Meteo geocoding to detect if a location exists in the query.
|
| 64 |
+
Works for lowercase, uppercase, and misspellings (fuzzy matching).
|
| 65 |
"""
|
| 66 |
+
try:
|
| 67 |
+
url = f"https://geocoding-api.open-meteo.com/v1/search?name={text}&count=1&language=en&format=json"
|
| 68 |
+
r = requests.get(url, timeout=5).json()
|
| 69 |
+
if r.get("results"):
|
| 70 |
+
return r["results"][0]["name"]
|
| 71 |
+
except:
|
| 72 |
+
pass
|
| 73 |
+
return None
|
| 74 |
|
|
|
|
| 75 |
|
| 76 |
|
| 77 |
def crop_recommendation(query: str, weather: str | None = None) -> str:
|