GeoSAPI / app.py
Predator911's picture
Update app.py
1474320 verified
Raw
History Blame Contribute Delete
3.91 kB
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'.")