|
|
import streamlit as st |
|
|
from streamlit_javascript import st_javascript |
|
|
import requests |
|
|
import os |
|
|
import pandas as pd |
|
|
import json |
|
|
|
|
|
|
|
|
API_KEY = os.getenv("GOOGLE_PLACES_API_KEY") |
|
|
|
|
|
st.title("π AI-Powered Restaurant & Museum Finder") |
|
|
|
|
|
|
|
|
js_code = """ |
|
|
navigator.geolocation.getCurrentPosition( |
|
|
(position) => { |
|
|
const coords = JSON.stringify({ latitude: position.coords.latitude, longitude: position.coords.longitude }); |
|
|
localStorage.setItem('user_coords', coords); |
|
|
}, |
|
|
(error) => { |
|
|
localStorage.setItem('user_coords', JSON.stringify({ error: error.message })); |
|
|
} |
|
|
); |
|
|
localStorage.getItem('user_coords'); |
|
|
""" |
|
|
|
|
|
location = st_javascript(js_code) |
|
|
|
|
|
|
|
|
st.write("π Raw Location Data:", location) |
|
|
|
|
|
|
|
|
if location: |
|
|
try: |
|
|
loc_data = json.loads(location) |
|
|
if "latitude" in loc_data: |
|
|
lat, lon = loc_data["latitude"], loc_data["longitude"] |
|
|
st.success(f"β
Detected Location: {lat}, {lon}") |
|
|
else: |
|
|
st.warning(f"β Error: {loc_data.get('error', 'Unknown issue')}") |
|
|
lat = st.number_input("Enter Latitude", value=40.7128) |
|
|
lon = st.number_input("Enter Longitude", value=-74.0060) |
|
|
except json.JSONDecodeError: |
|
|
st.error("β Error processing location data. Try refreshing the page.") |
|
|
lat = st.number_input("Enter Latitude", value=40.7128) |
|
|
lon = st.number_input("Enter Longitude", value=-74.0060) |
|
|
else: |
|
|
st.warning("β Unable to detect location. Please enable location services or enter manually.") |
|
|
lat = st.number_input("Enter Latitude", value=40.7128) |
|
|
lon = st.number_input("Enter Longitude", value=-74.0060) |
|
|
|
|
|
|
|
|
def fetch_places(lat, lon, place_type="restaurant"): |
|
|
radius = 3000 |
|
|
url = f"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={lat},{lon}&radius={radius}&type={place_type}&key={API_KEY}" |
|
|
response = requests.get(url) |
|
|
return response.json().get("results", []) |
|
|
|
|
|
|
|
|
place_type = st.selectbox("Choose a Category", ["restaurant", "museum"]) |
|
|
places = fetch_places(lat, lon, place_type) |
|
|
|
|
|
if places: |
|
|
df = pd.DataFrame([ |
|
|
{"Name": p["name"], "Rating": p.get("rating", "N/A"), "Address": p.get("vicinity", "Unknown")} |
|
|
for p in places[:10] |
|
|
]) |
|
|
st.table(df) |
|
|
else: |
|
|
st.warning(f"β No {place_type}s found nearby. Try increasing the radius or entering a different location.") |
|
|
|