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(""" """, 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'.")