nniehaus commited on
Commit
6ff8f56
·
verified ·
1 Parent(s): 8c614a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -35
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
- # Function to get neighborhood suggestions from OpenAI
9
- def suggest_neighborhoods(user_preferences, city=""):
10
- prompt = (f"Based on the following preferences, suggest neighborhoods in {city}: {user_preferences}. "
11
- "For each neighborhood, provide a brief description including its main attractions and why it would suit these preferences.")
12
- response = openai.Completion.create(
 
 
 
 
13
  model="gpt-3.5-turbo",
14
- prompt=prompt,
15
- max_tokens=150
16
  )
17
- return response.choices[0].text.strip()
18
-
19
- # Function to generate Zillow search link for a neighborhood
20
- def generate_zillow_link(neighborhood):
21
- neighborhood_query = urllib.parse.quote(neighborhood)
22
- return f"https://www.zillow.com/homes/{neighborhood_query}_rb/"
23
-
24
- # Streamlit Interface
25
- st.title("Ideal Neighborhood Finder")
26
- st.write("Describe what you're looking for in an ideal neighborhood, and we'll suggest neighborhoods with Zillow links for homes in those areas.")
27
-
28
- # User Inputs
29
- city = st.text_input("City", placeholder="Enter the city you want to search in")
30
- user_preferences = st.text_area("Describe Your Ideal Neighborhood", placeholder="E.g., close to schools, parks, public transit, vibrant nightlife, etc.")
31
-
32
- if st.button("Find Neighborhoods"):
33
- if city and user_preferences:
34
- # Get neighborhood suggestions from OpenAI
35
- neighborhoods = suggest_neighborhoods(user_preferences, city)
36
-
37
- # Display results
38
- st.subheader("Neighborhood Suggestions")
39
- for neighborhood in neighborhoods.splitlines():
40
- if neighborhood: # Filter out empty lines
41
- zillow_link = generate_zillow_link(neighborhood)
42
- st.markdown(f"- **{neighborhood}**: [View homes on Zillow]({zillow_link})")
43
- else:
44
- st.error("Please provide both a city and a description of your ideal neighborhood.")
 
 
 
 
 
 
 
 
 
 
 
 
 
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})")