|
import streamlit as st |
|
import openai |
|
import os |
|
import folium |
|
from streamlit_folium import st_folium |
|
import urllib.parse |
|
import subprocess |
|
|
|
|
|
openai.api_key = os.environ["OPENAI_API_KEY"] |
|
|
|
|
|
try: |
|
import folium |
|
except ModuleNotFoundError: |
|
subprocess.check_call(["python", "-m", "pip", "install", "folium"]) |
|
import folium |
|
|
|
initial_messages = [{ |
|
"role": "system", |
|
"content": """ |
|
You are a neighborhood matchmaker. Given a person's preferences in terms of amenities, lifestyle, and priorities, suggest three neighborhoods that best match their needs. Provide a short description for each neighborhood, including key features such as nearby amenities, atmosphere, and suitability for the user's preferences. |
|
""" |
|
}] |
|
|
|
def call_openai_api(messages): |
|
return openai.ChatCompletion.create( |
|
model="gpt-3.5-turbo", |
|
messages=messages |
|
) |
|
|
|
def CustomChatGPT(preferences, messages): |
|
query = f"User preferences: {preferences}. Suggest suitable neighborhoods." |
|
messages.append({"role": "user", "content": query}) |
|
response = call_openai_api(messages) |
|
ChatGPT_reply = response['choices'][0]['message']['content'] |
|
messages.append({"role": "assistant", "content": ChatGPT_reply}) |
|
return ChatGPT_reply, messages |
|
|
|
|
|
st.set_page_config(layout="wide") |
|
|
|
|
|
st.markdown("<h1 style='text-align: center; color: black;'>Neighborhood Matchmaker</h1>", unsafe_allow_html=True) |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
|
with col1: |
|
st.markdown("<h2 style='text-align: center; color: black;'>Your Preferences</h2>", unsafe_allow_html=True) |
|
preferences = st.text_area("Describe your ideal neighborhood", placeholder="E.g., family-friendly, good schools, near parks, vibrant nightlife, public transportation, etc.") |
|
generate_button = st.button('Find Neighborhoods') |
|
|
|
if generate_button: |
|
messages = initial_messages.copy() |
|
reply, _ = CustomChatGPT(preferences, messages) |
|
|
|
with col2: |
|
st.markdown("<h2 style='text-align: center; color: black;'>Recommended Neighborhoods ⬇️</h2>", unsafe_allow_html=True) |
|
st.write(reply) |
|
|
|
|
|
st.markdown("<h2 style='text-align: center; color: black;'>Map of Suggested Neighborhoods ⬇️</h2>", unsafe_allow_html=True) |
|
map_center = [37.7749, -122.4194] |
|
m = folium.Map(location=map_center, zoom_start=12) |
|
|
|
|
|
neighborhoods = [ |
|
{"name": "Neighborhood 1", "coordinates": [37.7749, -122.4194]}, |
|
{"name": "Neighborhood 2", "coordinates": [37.7849, -122.4094]}, |
|
{"name": "Neighborhood 3", "coordinates": [37.7649, -122.4294]} |
|
] |
|
|
|
for neighborhood in neighborhoods: |
|
folium.Marker( |
|
location=neighborhood["coordinates"], |
|
popup=neighborhood["name"], |
|
icon=folium.Icon(color='blue') |
|
).add_to(m) |
|
|
|
st_folium(m, width=700, height=500) |
|
|
|
|
|
st.markdown("<h2 style='text-align: center; color: black;'>Search for Homes in Suggested Neighborhoods ⬇️</h2>", unsafe_allow_html=True) |
|
for neighborhood in neighborhoods: |
|
query = urllib.parse.quote(neighborhood["name"]) |
|
zillow_url = f"https://www.zillow.com/homes/{query}_rb/" |
|
st.markdown(f"[Search for homes in {neighborhood['name']} on Zillow]({zillow_url})") |
|
|
|
|
|
st.markdown("<h2 style='text-align: center; color: black;'>Get in Touch for More Details ⬇️</h2>", unsafe_allow_html=True) |
|
with st.form(key='contact_form'): |
|
name = st.text_input("Your Name", placeholder="Enter your name") |
|
email = st.text_input("Your Email", placeholder="Enter your email address") |
|
message = st.text_area("Your Message", placeholder="Let us know how we can assist you further") |
|
submit_button = st.form_submit_button(label='Submit') |
|
|
|
if submit_button: |
|
st.success("Thank you for getting in touch! We'll get back to you shortly.") |