Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,154 +1,133 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
-
import
|
|
|
|
| 5 |
import re
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Configure
|
| 9 |
-
DEEPSEEK_API_KEY = os.getenv("
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
sources = {
|
| 15 |
"Niche": f"https://www.niche.com/places-to-live/search/{query}",
|
| 16 |
"AreaVibes": f"https://www.areavibes.com/search/?query={query}",
|
| 17 |
-
"Walkscore": f"https://www.walkscore.com/score/{query}"
|
| 18 |
}
|
| 19 |
|
| 20 |
-
results =
|
| 21 |
for source, url in sources.items():
|
| 22 |
try:
|
| 23 |
-
response = requests.get(url, timeout=
|
| 24 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 25 |
|
| 26 |
if source == "Niche":
|
| 27 |
listings = soup.find_all('div', class_='search-results__list__item')
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# Similar parsing for other sources
|
| 36 |
-
...
|
| 37 |
-
|
| 38 |
except Exception as e:
|
| 39 |
continue
|
| 40 |
|
| 41 |
return results
|
| 42 |
|
| 43 |
-
def
|
| 44 |
-
"""
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
- Important amenities
|
| 50 |
-
- Commute considerations
|
| 51 |
-
- Lifestyle priorities
|
| 52 |
-
- Budget constraints
|
| 53 |
-
"""
|
| 54 |
-
|
| 55 |
-
response = ds.generate(
|
| 56 |
-
model="neighborhood-matcher",
|
| 57 |
-
prompt=prompt,
|
| 58 |
-
max_tokens=500
|
| 59 |
-
)
|
| 60 |
-
return response['choices'][0]['text']
|
| 61 |
-
|
| 62 |
-
def generate_recommendations(criteria, locations):
|
| 63 |
-
"""Generate neighborhood recommendations with Deepseek's analysis"""
|
| 64 |
-
base_prompt = f"""
|
| 65 |
-
Based on these verified neighborhood data:
|
| 66 |
-
{locations}
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
1.
|
| 73 |
-
2.
|
| 74 |
-
3.
|
| 75 |
-
4.
|
| 76 |
-
5.
|
| 77 |
|
| 78 |
-
|
| 79 |
-
- Key strengths
|
| 80 |
-
- Potential drawbacks
|
| 81 |
-
- Notable amenities
|
| 82 |
-
- Average home prices
|
| 83 |
-
- Commute times
|
| 84 |
-
- Unique character
|
| 85 |
"""
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
# Streamlit UI
|
| 96 |
-
st.set_page_config(layout="wide")
|
| 97 |
-
st.title("
|
| 98 |
|
| 99 |
-
with st.
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
"Student", "Remote Worker", "Outdoor Enthusiast"
|
| 113 |
-
])
|
| 114 |
-
safety = st.slider("Safety Priority (1-10)", 1, 10, 8)
|
| 115 |
-
extra = st.text_input("Special Requirements", placeholder="e.g., Dog parks, historic district")
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
with st.spinner("Analyzing preferences and searching neighborhoods..."):
|
| 120 |
# Collect preferences
|
| 121 |
preferences = {
|
| 122 |
-
"
|
| 123 |
-
"
|
|
|
|
| 124 |
"amenities": amenities,
|
| 125 |
-
"lifestyle": lifestyle
|
| 126 |
-
"safety": safety,
|
| 127 |
-
"extra": extra
|
| 128 |
}
|
| 129 |
|
| 130 |
-
#
|
| 131 |
-
|
| 132 |
|
| 133 |
-
#
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
# Generate final recommendations
|
| 137 |
-
recommendations = generate_recommendations(preferences, location_data)
|
| 138 |
|
| 139 |
# Display results
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
| 151 |
|
| 152 |
-
# Disclaimer
|
| 153 |
st.markdown("---")
|
| 154 |
-
st.caption("Recommendations
|
|
|
|
| 1 |
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 |
|
| 12 |
+
# Configure environment
|
| 13 |
+
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_KEY")
|
| 14 |
+
API_ENDPOINT = "https://api.deepseek.com/v1/chat/completions"
|
| 15 |
|
| 16 |
+
# Cache expensive operations
|
| 17 |
+
@st.cache_data
|
| 18 |
+
def scrape_location_data(query):
|
| 19 |
+
"""Scrape location data from public sources"""
|
| 20 |
sources = {
|
| 21 |
"Niche": f"https://www.niche.com/places-to-live/search/{query}",
|
| 22 |
"AreaVibes": f"https://www.areavibes.com/search/?query={query}",
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
+
results = []
|
| 26 |
for source, url in sources.items():
|
| 27 |
try:
|
| 28 |
+
response = requests.get(url, timeout=15)
|
| 29 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 30 |
|
| 31 |
if source == "Niche":
|
| 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 |
+
"""Generate neighborhood recommendations using Deepseek API"""
|
| 47 |
+
headers = {
|
| 48 |
+
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
|
| 49 |
+
"Content-Type": "application/json"
|
| 50 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
prompt = f"""
|
| 53 |
+
Create a neighborhood recommendation report based on these preferences:
|
| 54 |
+
{preferences}
|
| 55 |
|
| 56 |
+
Include these sections:
|
| 57 |
+
1. Top 5 Neighborhood Matches
|
| 58 |
+
2. Hidden Gem Recommendation
|
| 59 |
+
3. Key Amenities Analysis
|
| 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:
|
| 67 |
+
response = requests.post(
|
| 68 |
+
API_ENDPOINT,
|
| 69 |
+
json={
|
| 70 |
+
"model": "deepseek-chat",
|
| 71 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 72 |
+
"temperature": 0.7,
|
| 73 |
+
"max_tokens": 1500
|
| 74 |
+
},
|
| 75 |
+
headers=headers,
|
| 76 |
+
timeout=30
|
| 77 |
+
)
|
| 78 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 79 |
+
except Exception as e:
|
| 80 |
+
st.error(f"API Error: {str(e)}")
|
| 81 |
+
return None
|
| 82 |
|
| 83 |
# Streamlit UI
|
| 84 |
+
st.set_page_config(layout="wide", page_icon="🏡")
|
| 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 |
+
if st.button("Find My Neighborhood"):
|
| 102 |
+
with st.spinner("Analyzing locations..."):
|
|
|
|
| 103 |
# Collect preferences
|
| 104 |
preferences = {
|
| 105 |
+
"city": city,
|
| 106 |
+
"budget": f"${budget}/mo",
|
| 107 |
+
"max_commute": commute,
|
| 108 |
"amenities": amenities,
|
| 109 |
+
"lifestyle": lifestyle
|
|
|
|
|
|
|
| 110 |
}
|
| 111 |
|
| 112 |
+
# Get location data
|
| 113 |
+
location_data = scrape_location_data(city)
|
| 114 |
|
| 115 |
+
# Generate recommendations
|
| 116 |
+
report = generate_recommendations(preferences)
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
# Display results
|
| 119 |
+
if report:
|
| 120 |
+
st.subheader("Your Personalized Neighborhood Report")
|
| 121 |
+
st.markdown(report)
|
| 122 |
+
|
| 123 |
+
# Show map
|
| 124 |
+
try:
|
| 125 |
+
geolocator = Nominatim(user_agent="neighborhood_finder")
|
| 126 |
+
location = geolocator.geocode(city)
|
| 127 |
+
m = folium.Map(location=[location.latitude, location.longitude], zoom_start=12)
|
| 128 |
+
folium_static(m, width=1200, height=500)
|
| 129 |
+
except Exception as e:
|
| 130 |
+
st.warning("Couldn't generate map visualization")
|
| 131 |
|
|
|
|
| 132 |
st.markdown("---")
|
| 133 |
+
st.caption("Note: Recommendations generated by AI. Verify with local experts.")
|