Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,7 @@ import streamlit as st
|
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
from geopy.geocoders import Nominatim
|
| 5 |
-
from urllib.parse import urljoin, urlparse
|
| 6 |
-
import re
|
| 7 |
import os
|
| 8 |
-
import pandas as pd
|
| 9 |
import folium
|
| 10 |
from streamlit_folium import folium_static
|
| 11 |
|
|
@@ -13,54 +10,41 @@ from streamlit_folium import folium_static
|
|
| 13 |
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_KEY")
|
| 14 |
API_ENDPOINT = "https://api.deepseek.com/v1/chat/completions"
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
def scrape_location_data(query):
|
| 19 |
-
"""Scrape location data
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
listings = soup.find_all('div', class_='search-results__list__item')
|
| 33 |
-
for item in listings[:3]:
|
| 34 |
-
results.append({
|
| 35 |
-
'name': item.find('h2').text.strip(),
|
| 36 |
-
'details': item.find('div', class_='search-result-tagline').text.strip(),
|
| 37 |
-
'score': item.find('div', class_='search-result-grade').text.strip()
|
| 38 |
-
})
|
| 39 |
-
|
| 40 |
-
except Exception as e:
|
| 41 |
-
continue
|
| 42 |
-
|
| 43 |
-
return results
|
| 44 |
|
| 45 |
def generate_recommendations(preferences):
|
| 46 |
-
"""
|
| 47 |
headers = {
|
| 48 |
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
|
| 49 |
"Content-Type": "application/json"
|
| 50 |
}
|
| 51 |
|
| 52 |
prompt = f"""
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
4. Commute Times Overview
|
| 61 |
-
5. Safety & Community Insights
|
| 62 |
-
|
| 63 |
-
Format with markdown headers and bullet points. Keep sections concise.
|
| 64 |
"""
|
| 65 |
|
| 66 |
try:
|
|
@@ -70,10 +54,10 @@ def generate_recommendations(preferences):
|
|
| 70 |
"model": "deepseek-chat",
|
| 71 |
"messages": [{"role": "user", "content": prompt}],
|
| 72 |
"temperature": 0.7,
|
| 73 |
-
"max_tokens":
|
| 74 |
},
|
| 75 |
headers=headers,
|
| 76 |
-
timeout=
|
| 77 |
)
|
| 78 |
return response.json()["choices"][0]["message"]["content"]
|
| 79 |
except Exception as e:
|
|
@@ -81,53 +65,43 @@ def generate_recommendations(preferences):
|
|
| 81 |
return None
|
| 82 |
|
| 83 |
# Streamlit UI
|
| 84 |
-
st.
|
| 85 |
-
st.title("Neighborhood Matchmaker")
|
| 86 |
-
|
| 87 |
-
with st.sidebar:
|
| 88 |
-
st.header("Search Preferences")
|
| 89 |
-
city = st.text_input("City/Region", "New York, NY")
|
| 90 |
-
budget = st.slider("Monthly Housing Budget ($)", 1000, 10000, 3000)
|
| 91 |
-
commute = st.selectbox("Max Commute Time", ["15 mins", "30 mins", "45 mins", "1 hour"])
|
| 92 |
-
amenities = st.multiselect("Must-Have Amenities", [
|
| 93 |
-
"Good Schools", "Parks", "Public Transport",
|
| 94 |
-
"Nightlife", "Shopping", "Healthcare"
|
| 95 |
-
])
|
| 96 |
-
lifestyle = st.selectbox("Lifestyle Preference", [
|
| 97 |
-
"Family-Friendly", "Urban Professional", "Retirement",
|
| 98 |
-
"Student", "Remote Worker", "Outdoor Enthusiast"
|
| 99 |
-
])
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
st.subheader("Your Personalized Neighborhood Report")
|
| 121 |
-
st.markdown(report)
|
| 122 |
|
| 123 |
-
#
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
from geopy.geocoders import Nominatim
|
|
|
|
|
|
|
| 5 |
import os
|
|
|
|
| 6 |
import folium
|
| 7 |
from streamlit_folium import folium_static
|
| 8 |
|
|
|
|
| 10 |
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_KEY")
|
| 11 |
API_ENDPOINT = "https://api.deepseek.com/v1/chat/completions"
|
| 12 |
|
| 13 |
+
# Docker-compatible settings
|
| 14 |
+
st.set_page_config(layout="wide", page_icon="🏡")
|
| 15 |
+
geolocator = Nominatim(user_agent="docker_neighborhood_finder")
|
| 16 |
+
|
| 17 |
+
@st.cache_data(show_spinner=False)
|
| 18 |
def scrape_location_data(query):
|
| 19 |
+
"""Scrape location data with Docker-friendly timeout"""
|
| 20 |
+
try:
|
| 21 |
+
response = requests.get(
|
| 22 |
+
f"https://www.niche.com/places-to-live/search/{query}",
|
| 23 |
+
timeout=10
|
| 24 |
+
)
|
| 25 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 26 |
+
return [
|
| 27 |
+
item.find('h2').text.strip()
|
| 28 |
+
for item in soup.find_all('div', class_='search-results__list__item')[:3]
|
| 29 |
+
]
|
| 30 |
+
except Exception:
|
| 31 |
+
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
def generate_recommendations(preferences):
|
| 34 |
+
"""Docker-optimized API call"""
|
| 35 |
headers = {
|
| 36 |
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
|
| 37 |
"Content-Type": "application/json"
|
| 38 |
}
|
| 39 |
|
| 40 |
prompt = f"""
|
| 41 |
+
Analyze these preferences: {preferences}
|
| 42 |
+
Recommend neighborhoods with:
|
| 43 |
+
- Top 3 matches
|
| 44 |
+
- 1 hidden gem
|
| 45 |
+
- Key amenities
|
| 46 |
+
- Safety insights
|
| 47 |
+
Format with markdown bullet points
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
"""
|
| 49 |
|
| 50 |
try:
|
|
|
|
| 54 |
"model": "deepseek-chat",
|
| 55 |
"messages": [{"role": "user", "content": prompt}],
|
| 56 |
"temperature": 0.7,
|
| 57 |
+
"max_tokens": 1000
|
| 58 |
},
|
| 59 |
headers=headers,
|
| 60 |
+
timeout=20 # Docker-friendly timeout
|
| 61 |
)
|
| 62 |
return response.json()["choices"][0]["message"]["content"]
|
| 63 |
except Exception as e:
|
|
|
|
| 65 |
return None
|
| 66 |
|
| 67 |
# Streamlit UI
|
| 68 |
+
st.title("🏡 Neighborhood Finder (Docker)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
with st.form("preferences"):
|
| 71 |
+
col1, col2 = st.columns(2)
|
| 72 |
+
with col1:
|
| 73 |
+
city = st.text_input("City/Region", "New York, NY")
|
| 74 |
+
budget = st.slider("Monthly Budget ($)", 1000, 10000, 3000)
|
| 75 |
+
with col2:
|
| 76 |
+
commute = st.selectbox("Max Commute", ["15m", "30m", "45m", "1h"])
|
| 77 |
+
lifestyle = st.selectbox("Lifestyle", ["Family", "Urban", "Remote"])
|
| 78 |
+
|
| 79 |
+
if st.form_submit_button("Find Neighborhoods"):
|
| 80 |
+
with st.spinner("Searching..."):
|
| 81 |
+
# Get data
|
| 82 |
+
locations = scrape_location_data(city)
|
| 83 |
+
preferences = {
|
| 84 |
+
"city": city,
|
| 85 |
+
"budget": budget,
|
| 86 |
+
"commute": commute,
|
| 87 |
+
"lifestyle": lifestyle
|
| 88 |
+
}
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
# Generate report
|
| 91 |
+
report = generate_recommendations(preferences)
|
| 92 |
+
|
| 93 |
+
# Show results
|
| 94 |
+
if report:
|
| 95 |
+
st.markdown(report)
|
| 96 |
+
|
| 97 |
+
# Docker-compatible map
|
| 98 |
+
try:
|
| 99 |
+
location = geolocator.geocode(city)
|
| 100 |
+
m = folium.Map(
|
| 101 |
+
location=[location.latitude, location.longitude],
|
| 102 |
+
zoom_start=12,
|
| 103 |
+
tiles="CartoDB positron"
|
| 104 |
+
)
|
| 105 |
+
folium_static(m, width=1000, height=400)
|
| 106 |
+
except Exception:
|
| 107 |
+
st.warning("Map unavailable - location not found")
|