File size: 4,514 Bytes
cac9eab
24b72ba
68833e7
 
 
b00f915
 
24b72ba
68833e7
 
 
b00f915
 
 
 
 
 
 
68833e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fd43cb
972b3f4
68833e7
 
 
 
 
 
6137a92
 
 
 
68833e7
 
 
 
 
 
 
 
 
 
 
0dfd8da
 
 
6137a92
0dfd8da
6137a92
0dfd8da
 
6137a92
68833e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b00f915
 
 
 
 
 
 
68833e7
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import streamlit as st
import openai
import os
import folium
from streamlit_folium import st_folium
import urllib.parse
import subprocess

# Ensure your OpenAI API key is set in your environment variables
openai.api_key = os.environ["OPENAI_API_KEY"]

# Ensure folium is installed
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

# Set layout to wide
st.set_page_config(layout="wide")

# Initialize session state for storing the results
if "reply" not in st.session_state:
    st.session_state.reply = ""

# Centered title
st.markdown("<h1 style='text-align: center; color: black;'>Neighborhood Matchmaker</h1>", unsafe_allow_html=True)

# Create columns for input and output
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)
    st.session_state.reply = reply  # Store the reply in session state

if st.session_state.reply:
    with col2:
        st.markdown("<h2 style='text-align: center; color: black;'>Recommended Neighborhoods ⬇️</h2>", unsafe_allow_html=True)
        st.write(st.session_state.reply)

        # Add map integration
        st.markdown("<h2 style='text-align: center; color: black;'>Map of Suggested Neighborhoods ⬇️</h2>", unsafe_allow_html=True)
        map_center = [37.7749, -122.4194]  # Default to San Francisco coordinates
        m = folium.Map(location=map_center, zoom_start=12)

        # Placeholder coordinates for neighborhoods (these should be updated with actual data)
        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)

        # Add Zillow search links for neighborhoods
        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})")

# Contact capture form
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.")