Update app.py
Browse files
app.py
CHANGED
|
@@ -3,46 +3,60 @@ from streamlit_javascript import st_javascript
|
|
| 3 |
import requests
|
| 4 |
import os
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
|
| 7 |
-
# Google API Key (Stored in Hugging Face
|
| 8 |
API_KEY = os.getenv("GOOGLE_PLACES_API_KEY")
|
| 9 |
|
| 10 |
st.title("π AI-Powered Restaurant & Museum Finder")
|
| 11 |
|
| 12 |
-
# JavaScript
|
| 13 |
js_code = """
|
| 14 |
navigator.geolocation.getCurrentPosition(
|
| 15 |
(position) => {
|
| 16 |
-
const coords = { latitude: position.coords.latitude, longitude: position.coords.longitude };
|
| 17 |
-
|
| 18 |
},
|
| 19 |
(error) => {
|
| 20 |
-
|
| 21 |
}
|
| 22 |
);
|
| 23 |
-
|
| 24 |
"""
|
| 25 |
|
| 26 |
location = st_javascript(js_code)
|
| 27 |
|
| 28 |
-
# Debugging:
|
| 29 |
st.write("π Raw Location Data:", location)
|
| 30 |
|
| 31 |
-
if location
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
else:
|
| 35 |
st.warning("β Unable to detect location. Please enable location services or enter manually.")
|
| 36 |
-
lat = st.number_input("Enter Latitude", value=40.7128) # Default:
|
| 37 |
-
lon = st.number_input("Enter Longitude", value=-74.0060) # Default:
|
| 38 |
|
|
|
|
| 39 |
def fetch_places(lat, lon, place_type="restaurant"):
|
| 40 |
radius = 3000 # 3km range
|
| 41 |
url = f"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={lat},{lon}&radius={radius}&type={place_type}&key={API_KEY}"
|
| 42 |
response = requests.get(url)
|
| 43 |
return response.json().get("results", [])
|
| 44 |
|
| 45 |
-
# Choose category
|
| 46 |
place_type = st.selectbox("Choose a Category", ["restaurant", "museum"])
|
| 47 |
places = fetch_places(lat, lon, place_type)
|
| 48 |
|
|
@@ -53,4 +67,4 @@ if places:
|
|
| 53 |
])
|
| 54 |
st.table(df)
|
| 55 |
else:
|
| 56 |
-
st.warning(f"No {place_type}s found nearby.")
|
|
|
|
| 3 |
import requests
|
| 4 |
import os
|
| 5 |
import pandas as pd
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
+
# Google API Key (Stored as a secret in Hugging Face Spaces)
|
| 9 |
API_KEY = os.getenv("GOOGLE_PLACES_API_KEY")
|
| 10 |
|
| 11 |
st.title("π AI-Powered Restaurant & Museum Finder")
|
| 12 |
|
| 13 |
+
# Run JavaScript to get geolocation
|
| 14 |
js_code = """
|
| 15 |
navigator.geolocation.getCurrentPosition(
|
| 16 |
(position) => {
|
| 17 |
+
const coords = JSON.stringify({ latitude: position.coords.latitude, longitude: position.coords.longitude });
|
| 18 |
+
localStorage.setItem('user_coords', coords);
|
| 19 |
},
|
| 20 |
(error) => {
|
| 21 |
+
localStorage.setItem('user_coords', JSON.stringify({ error: error.message }));
|
| 22 |
}
|
| 23 |
);
|
| 24 |
+
localStorage.getItem('user_coords');
|
| 25 |
"""
|
| 26 |
|
| 27 |
location = st_javascript(js_code)
|
| 28 |
|
| 29 |
+
# Debugging: Display raw location response
|
| 30 |
st.write("π Raw Location Data:", location)
|
| 31 |
|
| 32 |
+
# Check if location was successfully fetched
|
| 33 |
+
if location:
|
| 34 |
+
try:
|
| 35 |
+
loc_data = json.loads(location)
|
| 36 |
+
if "latitude" in loc_data:
|
| 37 |
+
lat, lon = loc_data["latitude"], loc_data["longitude"]
|
| 38 |
+
st.success(f"β
Detected Location: {lat}, {lon}")
|
| 39 |
+
else:
|
| 40 |
+
st.warning(f"β Error: {loc_data.get('error', 'Unknown issue')}")
|
| 41 |
+
lat = st.number_input("Enter Latitude", value=40.7128) # Default: NYC
|
| 42 |
+
lon = st.number_input("Enter Longitude", value=-74.0060) # Default: NYC
|
| 43 |
+
except json.JSONDecodeError:
|
| 44 |
+
st.error("β Error processing location data. Try refreshing the page.")
|
| 45 |
+
lat = st.number_input("Enter Latitude", value=40.7128)
|
| 46 |
+
lon = st.number_input("Enter Longitude", value=-74.0060)
|
| 47 |
else:
|
| 48 |
st.warning("β Unable to detect location. Please enable location services or enter manually.")
|
| 49 |
+
lat = st.number_input("Enter Latitude", value=40.7128) # Default: NYC
|
| 50 |
+
lon = st.number_input("Enter Longitude", value=-74.0060) # Default: NYC
|
| 51 |
|
| 52 |
+
# Fetch places from Google Places API
|
| 53 |
def fetch_places(lat, lon, place_type="restaurant"):
|
| 54 |
radius = 3000 # 3km range
|
| 55 |
url = f"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={lat},{lon}&radius={radius}&type={place_type}&key={API_KEY}"
|
| 56 |
response = requests.get(url)
|
| 57 |
return response.json().get("results", [])
|
| 58 |
|
| 59 |
+
# Choose category (restaurant or museum)
|
| 60 |
place_type = st.selectbox("Choose a Category", ["restaurant", "museum"])
|
| 61 |
places = fetch_places(lat, lon, place_type)
|
| 62 |
|
|
|
|
| 67 |
])
|
| 68 |
st.table(df)
|
| 69 |
else:
|
| 70 |
+
st.warning(f"β No {place_type}s found nearby. Try increasing the radius or entering a different location.")
|