Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,101 +1,37 @@
|
|
|
|
|
| 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 |
-
#
|
| 10 |
-
STATE_ABBREVIATIONS = {
|
| 11 |
-
'alabama': 'al', 'alaska': 'ak', 'arizona': 'az', 'arkansas': 'ar', 'california': 'ca',
|
| 12 |
-
'colorado': 'co', 'connecticut': 'ct', 'delaware': 'de', 'florida': 'fl', 'georgia': 'ga',
|
| 13 |
-
'hawaii': 'hi', 'idaho': 'id', 'illinois': 'il', 'indiana': 'in', 'iowa': 'ia',
|
| 14 |
-
'kansas': 'ks', 'kentucky': 'ky', 'louisiana': 'la', 'maine': 'me', 'maryland': 'md',
|
| 15 |
-
'massachusetts': 'ma', 'michigan': 'mi', 'minnesota': 'mn', 'mississippi': 'ms', 'missouri': 'mo',
|
| 16 |
-
'montana': 'mt', 'nebraska': 'ne', 'nevada': 'nv', 'new hampshire': 'nh', 'new jersey': 'nj',
|
| 17 |
-
'new mexico': 'nm', 'new york': 'ny', 'north carolina': 'nc', 'north dakota': 'nd',
|
| 18 |
-
'ohio': 'oh', 'oklahoma': 'ok', 'oregon': 'or', 'pennsylvania': 'pa', 'rhode island': 'ri',
|
| 19 |
-
'south carolina': 'sc', 'south dakota': 'sd', 'tennessee': 'tn', 'texas': 'tx',
|
| 20 |
-
'utah': 'ut', 'vermont': 'vt', 'virginia': 'va', 'washington': 'wa', 'west virginia': 'wv',
|
| 21 |
-
'wisconsin': 'wi', 'wyoming': 'wy'
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
initial_messages = [{
|
| 25 |
"role": "system",
|
| 26 |
"content": """
|
| 27 |
-
You are
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
followed by the full location in the format: 'Neighborhood, City, State:' then provide
|
| 31 |
-
a brief description on the next line.
|
| 32 |
"""
|
| 33 |
}]
|
| 34 |
|
| 35 |
def call_openai_api(messages):
|
| 36 |
return openai.ChatCompletion.create(
|
| 37 |
-
model="gpt-
|
| 38 |
messages=messages,
|
| 39 |
-
max_tokens=
|
| 40 |
)
|
| 41 |
|
| 42 |
-
def
|
| 43 |
-
"""
|
| 44 |
-
Parse the response text to extract neighborhood information.
|
| 45 |
-
Returns a list of dictionaries containing neighborhood info.
|
| 46 |
-
"""
|
| 47 |
-
neighborhoods = []
|
| 48 |
-
lines = response_text.strip().split('\n')
|
| 49 |
-
for line in lines:
|
| 50 |
-
line = line.strip()
|
| 51 |
-
if line and (line.startswith('1.') or line.startswith('2.') or line.startswith('3.')):
|
| 52 |
-
if ':' in line:
|
| 53 |
-
location_parts = line.split(':', 1)[0].split(',')
|
| 54 |
-
if len(location_parts) >= 3:
|
| 55 |
-
neighborhood = location_parts[0].split('.', 1)[1].strip()
|
| 56 |
-
city = location_parts[1].strip()
|
| 57 |
-
state = location_parts[2].strip()
|
| 58 |
-
neighborhoods.append({
|
| 59 |
-
'neighborhood': neighborhood,
|
| 60 |
-
'city': city,
|
| 61 |
-
'state': state,
|
| 62 |
-
'full': f"{neighborhood}, {city}, {state}"
|
| 63 |
-
})
|
| 64 |
-
return neighborhoods
|
| 65 |
-
|
| 66 |
-
def format_zillow_search(neighborhood_info):
|
| 67 |
-
"""
|
| 68 |
-
Format the location string for Zillow search using 'neighborhood, city, state' format with commas and _rb at the end.
|
| 69 |
-
Fallback to a city-level search if the neighborhood is not recognized.
|
| 70 |
-
"""
|
| 71 |
-
# Prepare each component with correct capitalization and format for Zillow
|
| 72 |
-
neighborhood = urllib.parse.quote(neighborhood_info['neighborhood'])
|
| 73 |
-
city = urllib.parse.quote(neighborhood_info['city'])
|
| 74 |
-
|
| 75 |
-
# Convert full state name to abbreviation if needed
|
| 76 |
-
state = neighborhood_info['state'].lower()
|
| 77 |
-
state_abbr = STATE_ABBREVIATIONS.get(state, state[:2]).upper() # Ensure state is uppercase
|
| 78 |
-
|
| 79 |
-
# Primary search path with neighborhood
|
| 80 |
-
search_path = f"{neighborhood},-{city},-{state_abbr}_rb"
|
| 81 |
-
url = f"https://www.zillow.com/homes/{search_path}/"
|
| 82 |
-
|
| 83 |
-
# Check URL - fallback to city-level URL if Zillow cannot interpret it
|
| 84 |
-
if not neighborhood: # If neighborhood is empty or unrecognized, return a city-level search URL
|
| 85 |
-
search_path = f"{city},-{state_abbr}_rb"
|
| 86 |
-
url = f"https://www.zillow.com/homes/{search_path}/"
|
| 87 |
-
|
| 88 |
-
return url
|
| 89 |
-
|
| 90 |
-
def CustomChatGPT(city, preferences, messages):
|
| 91 |
query = f"""
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
2. Provide the full location as 'Neighborhood, City, State:'
|
| 96 |
-
3. Add a brief description on the next line
|
| 97 |
-
"""
|
| 98 |
|
|
|
|
|
|
|
|
|
|
| 99 |
messages.append({"role": "user", "content": query})
|
| 100 |
response = call_openai_api(messages)
|
| 101 |
ChatGPT_reply = response["choices"][0]["message"]["content"]
|
|
@@ -110,34 +46,26 @@ if "reply" not in st.session_state:
|
|
| 110 |
st.session_state["reply"] = None
|
| 111 |
|
| 112 |
# Centered title
|
| 113 |
-
st.markdown("<h1 style='text-align: center; color: black;'>
|
| 114 |
|
| 115 |
# User inputs
|
|
|
|
|
|
|
| 116 |
col1, col2 = st.columns(2)
|
|
|
|
| 117 |
with col1:
|
| 118 |
-
st.
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
|
| 123 |
# Process results on button click
|
| 124 |
-
if
|
| 125 |
messages = initial_messages.copy()
|
| 126 |
-
st.session_state["reply"], _ =
|
| 127 |
|
| 128 |
# Display results if there is a reply in session state
|
| 129 |
if st.session_state["reply"]:
|
| 130 |
with col2:
|
| 131 |
-
st.markdown("<h2 style='text-align: center; color: black;'>
|
| 132 |
st.write(st.session_state["reply"])
|
| 133 |
-
|
| 134 |
-
# Extract and display Zillow links
|
| 135 |
-
neighborhoods = parse_neighborhoods(st.session_state["reply"])
|
| 136 |
-
|
| 137 |
-
if neighborhoods:
|
| 138 |
-
st.markdown("### 🏠 Zillow Search Links")
|
| 139 |
-
for hood in neighborhoods:
|
| 140 |
-
zillow_url = format_zillow_search(hood)
|
| 141 |
-
st.markdown(f"- [Search homes in {hood['neighborhood']}, {hood['city']}, {hood['state']}]({zillow_url})")
|
| 142 |
-
else:
|
| 143 |
-
st.warning("Unable to generate Zillow links. Please try again.")
|
|
|
|
| 1 |
+
|
| 2 |
import streamlit as st
|
| 3 |
import openai
|
| 4 |
+
import os
|
|
|
|
| 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 system message setup for the video idea generator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
initial_messages = [{
|
| 11 |
"role": "system",
|
| 12 |
"content": """
|
| 13 |
+
You are an assistant that helps small businesses generate creative video ideas.
|
| 14 |
+
Based on the business description and their ideal customer profile, provide 10 actionable and relevant video content ideas.
|
| 15 |
+
Focus on ideas that align with the business goals and resonate with their target audience.
|
|
|
|
|
|
|
| 16 |
"""
|
| 17 |
}]
|
| 18 |
|
| 19 |
def call_openai_api(messages):
|
| 20 |
return openai.ChatCompletion.create(
|
| 21 |
+
model="gpt-4",
|
| 22 |
messages=messages,
|
| 23 |
+
max_tokens=700 # Adjust based on the expected length of the video ideas
|
| 24 |
)
|
| 25 |
|
| 26 |
+
def generate_video_ideas(business_description, ideal_customer, messages):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
query = f"""
|
| 28 |
+
Generate 10 video ideas for a small business based on the following information:
|
| 29 |
+
- Business Description: {business_description}
|
| 30 |
+
- Ideal Customer: {ideal_customer}
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
Provide actionable and creative video ideas that the business can use to attract and engage their target audience.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
messages.append({"role": "user", "content": query})
|
| 36 |
response = call_openai_api(messages)
|
| 37 |
ChatGPT_reply = response["choices"][0]["message"]["content"]
|
|
|
|
| 46 |
st.session_state["reply"] = None
|
| 47 |
|
| 48 |
# Centered title
|
| 49 |
+
st.markdown("<h1 style='text-align: center; color: black;'>Small Business Video Idea Generator</h1>", unsafe_allow_html=True)
|
| 50 |
|
| 51 |
# User inputs
|
| 52 |
+
st.markdown("<h2 style='text-align: center; color: black;'>Describe Your Business and Ideal Customer</h2>", unsafe_allow_html=True)
|
| 53 |
+
|
| 54 |
col1, col2 = st.columns(2)
|
| 55 |
+
|
| 56 |
with col1:
|
| 57 |
+
business_description = st.text_area("Describe your business:", placeholder="Enter your business description...")
|
| 58 |
+
ideal_customer = st.text_area("Describe your ideal customer:", placeholder="Enter details about your ideal customer...")
|
| 59 |
+
|
| 60 |
+
generate_ideas_button = st.button('Generate Video Ideas')
|
| 61 |
|
| 62 |
# Process results on button click
|
| 63 |
+
if generate_ideas_button and business_description and ideal_customer:
|
| 64 |
messages = initial_messages.copy()
|
| 65 |
+
st.session_state["reply"], _ = generate_video_ideas(business_description, ideal_customer, messages)
|
| 66 |
|
| 67 |
# Display results if there is a reply in session state
|
| 68 |
if st.session_state["reply"]:
|
| 69 |
with col2:
|
| 70 |
+
st.markdown("<h2 style='text-align: center; color: black;'>Your Video Ideas ⬇️</h2>", unsafe_allow_html=True)
|
| 71 |
st.write(st.session_state["reply"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|