Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| import pandas as pd | |
| # Set up Streamlit page | |
| st.set_page_config(page_title="π Geospatial Querying System", page_icon="π", layout="wide") | |
| # Custom Styling | |
| st.markdown(""" | |
| <style> | |
| .stButton > button { | |
| background-color: #ff5a5f; | |
| color: white; | |
| font-size: 16px; | |
| border-radius: 10px; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # Title & Instructions | |
| st.title("π Geospatial Querying System") | |
| st.write("π **Enter any query like:**") | |
| st.markdown(""" | |
| - `"Find hospitals in Delhi"` | |
| - `"Show malls near Bangalore"` | |
| - `"Where are the police stations in Mumbai?"` | |
| """) | |
| # User input | |
| query = st.text_input("π Enter your query (e.g., 'Find restaurants in Mumbai'):") | |
| # Buttons | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| find_location = st.button("π Find Location & Category") | |
| with col2: | |
| find_nearby = st.button("π Find Nearby Places") | |
| # Backend API URLs | |
| BASE_URL = "https://geospatial-api.onrender.com" | |
| # Global variables to store extracted data | |
| if "location" not in st.session_state: | |
| st.session_state["location"] = None | |
| if "category" not in st.session_state: | |
| st.session_state["category"] = None | |
| # Step 1: Extract location & category from query | |
| if find_location: | |
| if query: | |
| with st.spinner("π Extracting location & category..."): | |
| response = requests.post(f"{BASE_URL}/query", json={"text": query}) | |
| data = response.json() | |
| if "error" in data: | |
| st.error("β Could not extract location. Try again!") | |
| else: | |
| # Store extracted data in session state | |
| st.session_state["location"] = data["place"] | |
| st.session_state["category"] = data["category"] | |
| # Display extracted information | |
| st.success(f"β **Extracted Location:** `{data['place']}`") | |
| st.success(f"π **Extracted Category:** `{data['category']}`") | |
| st.write(f"π **Latitude:** `{data['latitude']}`") | |
| st.write(f"π **Longitude:** `{data['longitude']}`") | |
| # Google Maps Link | |
| maps_link = f"https://www.google.com/maps/search/?api=1&query={data['latitude']},{data['longitude']}" | |
| st.markdown(f"[π View on Google Maps]({maps_link})", unsafe_allow_html=True) | |
| # Step 2: Find Nearby Places | |
| if find_nearby: | |
| if st.session_state["location"] and st.session_state["category"]: | |
| with st.spinner("π Searching for nearby places..."): | |
| response = requests.post(f"{BASE_URL}/nearby", json={"place": st.session_state["location"], "category": st.session_state["category"]}) | |
| data = response.json() | |
| if "nearby_places" in data and data["nearby_places"]: | |
| st.success(f"β **Nearby {st.session_state['category']} found in {st.session_state['location']}:**") | |
| # Show Places in a Table | |
| places_df = pd.DataFrame(data["nearby_places"]) | |
| places_df = places_df.rename(columns={"name": "Place Name", "lat": "Latitude", "lon": "Longitude"}) | |
| st.dataframe(places_df) | |
| # Show Google Maps Links | |
| for place in data["nearby_places"]: | |
| maps_link = f"https://www.google.com/maps/search/?api=1&query={place['lat']},{place['lon']}" | |
| st.markdown(f"π **{place['name']}** [π View on Maps]({maps_link})", unsafe_allow_html=True) | |
| # Show on Interactive Map | |
| map_data = pd.DataFrame({ | |
| "lat": [p["lat"] for p in data["nearby_places"]], | |
| "lon": [p["lon"] for p in data["nearby_places"]] | |
| }) | |
| st.map(map_data) | |
| else: | |
| st.error("β No places found in the selected category.") | |
| else: | |
| st.error("β Please extract location & category first by clicking 'Find Location & Category'.") |