Aaayushiii commited on
Commit
cd9d141
·
verified ·
1 Parent(s): 6a50653

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -8
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
- Extracts potential location names from the user query.
62
- Works for both lowercase and capitalized names.
63
  """
64
- # Normalize text (title case ensures 'delhi' → 'Delhi')
65
- norm_text = text.title()
66
-
67
- # Look for words/sequences of words starting with capital letters
68
- matches = re.findall(r"\b([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*)\b", norm_text)
 
 
 
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: