kimappl commited on
Commit
464579e
Β·
verified Β·
1 Parent(s): 16233a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -14
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 Secrets)
8
  API_KEY = os.getenv("GOOGLE_PLACES_API_KEY")
9
 
10
  st.title("πŸ“ AI-Powered Restaurant & Museum Finder")
11
 
12
- # JavaScript function to get geolocation (with error handling)
13
  js_code = """
14
  navigator.geolocation.getCurrentPosition(
15
  (position) => {
16
- const coords = { latitude: position.coords.latitude, longitude: position.coords.longitude };
17
- document.body.setAttribute('data-coords', JSON.stringify(coords));
18
  },
19
  (error) => {
20
- document.body.setAttribute('data-coords', JSON.stringify({ error: error.message }));
21
  }
22
  );
23
- document.body.getAttribute('data-coords');
24
  """
25
 
26
  location = st_javascript(js_code)
27
 
28
- # Debugging: Show raw location response
29
  st.write("πŸ“Œ Raw Location Data:", location)
30
 
31
- if location and "latitude" in location:
32
- lat, lon = location["latitude"], location["longitude"]
33
- st.success(f"Detected Location: {lat}, {lon}")
 
 
 
 
 
 
 
 
 
 
 
 
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: New York
37
- lon = st.number_input("Enter Longitude", value=-74.0060) # Default: New York
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.")