Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from streamlit_javascript import st_javascript
|
3 |
+
import requests
|
4 |
+
import pandas as pd
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Load API key from environment variable
|
8 |
+
API_KEY = os.getenv("GOOGLE_PLACES_API_KEY")
|
9 |
+
|
10 |
+
st.title("📍 AI-Powered Restaurant & Museum Finder")
|
11 |
+
|
12 |
+
# Fetch user's location
|
13 |
+
location = st_javascript("navigator.geolocation.getCurrentPosition(position => position.coords);")
|
14 |
+
|
15 |
+
if location:
|
16 |
+
lat, lon = location["latitude"], location["longitude"]
|
17 |
+
st.success(f"Detected Location: {lat}, {lon}")
|
18 |
+
|
19 |
+
def fetch_places(lat, lon, place_type="restaurant"):
|
20 |
+
radius = 3000 # 3km range
|
21 |
+
url = f"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={lat},{lon}&radius={radius}&type={place_type}&key={API_KEY}"
|
22 |
+
response = requests.get(url)
|
23 |
+
return response.json().get("results", [])
|
24 |
+
|
25 |
+
# Select category
|
26 |
+
place_type = st.selectbox("Choose a Category", ["restaurant", "museum"])
|
27 |
+
|
28 |
+
places = fetch_places(lat, lon, place_type)
|
29 |
+
|
30 |
+
if places:
|
31 |
+
def rank_places(places):
|
32 |
+
return sorted(places, key=lambda p: (p.get("rating", 0) * 0.7) + (p.get("user_ratings_total", 0) * 0.3), reverse=True)
|
33 |
+
|
34 |
+
ranked_places = rank_places(places)
|
35 |
+
df = pd.DataFrame([
|
36 |
+
{"Name": p["name"], "Rating": p.get("rating", "N/A"), "Reviews": p.get("user_ratings_total", 0), "Address": p.get("vicinity", "Unknown")}
|
37 |
+
for p in ranked_places[:10]
|
38 |
+
])
|
39 |
+
st.table(df)
|
40 |
+
|
41 |
+
# Map Visualization
|
42 |
+
st.map(pd.DataFrame({"lat": [p["geometry"]["location"]["lat"] for p in ranked_places[:10]],
|
43 |
+
"lon": [p["geometry"]["location"]["lng"] for p in ranked_places[:10]]}))
|
44 |
+
else:
|
45 |
+
st.warning(f"No {place_type}s found nearby.")
|
46 |
+
else:
|
47 |
+
st.error("Unable to detect location. Please enable location services.")
|