Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,60 @@
|
|
1 |
import streamlit as st
|
2 |
import openai
|
|
|
3 |
import urllib.parse
|
4 |
|
5 |
# Ensure your OpenAI API key is set in your environment variables
|
6 |
openai.api_key = os.environ["OPENAI_API_KEY"]
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
13 |
model="gpt-3.5-turbo",
|
14 |
-
|
15 |
-
max_tokens=150
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import openai
|
3 |
+
import os
|
4 |
import urllib.parse
|
5 |
|
6 |
# Ensure your OpenAI API key is set in your environment variables
|
7 |
openai.api_key = os.environ["OPENAI_API_KEY"]
|
8 |
|
9 |
+
initial_messages = [{
|
10 |
+
"role": "system",
|
11 |
+
"content": """
|
12 |
+
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 in a specified city. Provide a short description of each neighborhood, highlighting its unique qualities.
|
13 |
+
"""
|
14 |
+
}]
|
15 |
+
|
16 |
+
def call_openai_api(messages):
|
17 |
+
return openai.ChatCompletion.create(
|
18 |
model="gpt-3.5-turbo",
|
19 |
+
messages=messages
|
|
|
20 |
)
|
21 |
+
|
22 |
+
def CustomChatGPT(city, preferences, messages):
|
23 |
+
query = f"User is looking for neighborhoods in {city} with these preferences: {preferences}. Suggest suitable neighborhoods and describe each briefly."
|
24 |
+
messages.append({"role": "user", "content": query})
|
25 |
+
response = call_openai_api(messages)
|
26 |
+
ChatGPT_reply = response["choices"][0]["message"]["content"]
|
27 |
+
messages.append({"role": "assistant", "content": ChatGPT_reply})
|
28 |
+
return ChatGPT_reply, messages
|
29 |
+
|
30 |
+
# Streamlit setup
|
31 |
+
st.set_page_config(layout="wide")
|
32 |
+
|
33 |
+
# Centered title
|
34 |
+
st.markdown("<h1 style='text-align: center; color: black;'>Ideal Neighborhood Finder</h1>", unsafe_allow_html=True)
|
35 |
+
|
36 |
+
# User inputs
|
37 |
+
col1, col2 = st.columns(2)
|
38 |
+
|
39 |
+
with col1:
|
40 |
+
st.markdown("<h2 style='text-align: center; color: black;'>Your Preferences</h2>", unsafe_allow_html=True)
|
41 |
+
city = st.text_input("City", placeholder="Enter the city you want to search in")
|
42 |
+
preferences = st.text_area("Describe your ideal neighborhood", placeholder="E.g., family-friendly, near parks, vibrant nightlife")
|
43 |
+
generate_button = st.button('Find Neighborhoods')
|
44 |
+
|
45 |
+
# Process results on button click
|
46 |
+
if generate_button and city and preferences:
|
47 |
+
messages = initial_messages.copy()
|
48 |
+
reply, _ = CustomChatGPT(city, preferences, messages)
|
49 |
+
|
50 |
+
# Display the results
|
51 |
+
with col2:
|
52 |
+
st.markdown("<h2 style='text-align: center; color: black;'>Recommended Neighborhoods ⬇️</h2>", unsafe_allow_html=True)
|
53 |
+
st.write(reply)
|
54 |
+
|
55 |
+
# Display Zillow links for each neighborhood
|
56 |
+
neighborhoods = reply.splitlines()
|
57 |
+
for neighborhood in neighborhoods:
|
58 |
+
neighborhood_query = urllib.parse.quote(neighborhood)
|
59 |
+
zillow_url = f"https://www.zillow.com/homes/{neighborhood_query}_rb/"
|
60 |
+
st.markdown(f"- [Search for homes in {neighborhood} on Zillow]({zillow_url})")
|