Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import streamlit as st
|
|
2 |
import openai
|
3 |
import requests
|
4 |
from bs4 import BeautifulSoup
|
5 |
-
from urllib.parse import urljoin
|
6 |
import os
|
7 |
import re
|
8 |
|
@@ -54,6 +54,28 @@ def scrape_website(url, max_pages=5):
|
|
54 |
|
55 |
return " ".join(all_content[:3000]), scrape_successful
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
def extract_location(content):
|
58 |
"""
|
59 |
Extract a possible location from the website content using regular expressions.
|
@@ -61,37 +83,12 @@ def extract_location(content):
|
|
61 |
location_match = re.search(r'\b(?:serving|located in|offices in|based in)\s([\w\s,]+)', content, re.IGNORECASE)
|
62 |
return location_match.group(1).strip() if location_match else None
|
63 |
|
64 |
-
|
65 |
-
initial_messages = [{
|
66 |
-
"role": "system",
|
67 |
-
"content": """You are a world-class marketing strategist trained by Neil Patel, David Ogilvy, and Seth Godin.
|
68 |
-
Your task is to create highly customized marketing plans based on user input. Incorporate any business location
|
69 |
-
or target areas explicitly mentioned in the website content or user-provided details into the recommendations.
|
70 |
-
Go beyond generic suggestions, and include:
|
71 |
-
- Specific, long-tail keywords to target.
|
72 |
-
- Detailed content ideas, including blogs, videos, and social media campaigns.
|
73 |
-
- Unique strategies tailored to the business's industry, goals, and location.
|
74 |
-
- Innovative advertising campaigns and emerging platform recommendations.
|
75 |
-
Ensure every suggestion is actionable and includes measurable KPIs."""
|
76 |
-
}]
|
77 |
-
|
78 |
-
def call_openai_api(messages):
|
79 |
-
"""
|
80 |
-
Calls the OpenAI ChatCompletion API with the correct format.
|
81 |
-
"""
|
82 |
-
response = openai.ChatCompletion.create(
|
83 |
-
model="gpt-4",
|
84 |
-
messages=messages,
|
85 |
-
max_tokens=3000,
|
86 |
-
temperature=0.7
|
87 |
-
)
|
88 |
-
return response["choices"][0]["message"]["content"]
|
89 |
-
|
90 |
-
def generate_marketing_plan(website_content, industry, goals, budget, location, messages, fallback=False):
|
91 |
"""
|
92 |
Generates a marketing plan based on website content, industry, goals, and budget.
|
93 |
"""
|
94 |
location_info = f"The business is located in {location}." if location else "No specific location was mentioned."
|
|
|
95 |
|
96 |
query = f"""
|
97 |
The user has provided the following details:
|
@@ -100,6 +97,7 @@ def generate_marketing_plan(website_content, industry, goals, budget, location,
|
|
100 |
- Goals for 2025: {goals}
|
101 |
- Marketing budget for 2025: ${budget}
|
102 |
- {location_info}
|
|
|
103 |
|
104 |
Create a detailed 1-year marketing plan that includes:
|
105 |
1. **Advanced Keywords**: Long-tail keywords specific to the industry and location (if applicable).
|
@@ -131,8 +129,8 @@ col1, col2 = st.columns(2)
|
|
131 |
with col1:
|
132 |
st.markdown("<h2 style='text-align: center; color: black;'>Enter Business Details</h2>", unsafe_allow_html=True)
|
133 |
website_url = st.text_input("Enter your business website", placeholder="e.g., https://example.com")
|
134 |
-
industry = st.text_input("Industry", placeholder="E.g., Real Estate, Retail, Technology")
|
135 |
-
goals = st.text_area("Goals for 2025", placeholder="E.g., increase brand awareness, drive online sales")
|
136 |
budget = st.number_input("Marketing Budget for 2025 ($)", min_value=1000, step=1000)
|
137 |
generate_button = st.button('Generate Marketing Plan')
|
138 |
|
@@ -142,13 +140,14 @@ if generate_button:
|
|
142 |
with st.spinner("Analyzing website content and preparing your report..."):
|
143 |
website_content, scrape_successful = scrape_website(website_url) if website_url else ("", False)
|
144 |
location = extract_location(website_content) if scrape_successful else None
|
|
|
145 |
fallback_mode = not scrape_successful
|
146 |
if fallback_mode:
|
147 |
-
st.warning("Unable to retrieve website content. Generating recommendations based on
|
148 |
messages = initial_messages.copy()
|
149 |
st.session_state["reply"] = generate_marketing_plan(
|
150 |
website_content if scrape_successful else "N/A",
|
151 |
-
industry, goals, budget, location, messages, fallback=fallback_mode
|
152 |
)
|
153 |
st.session_state["show_notice"] = False # Remove the notice once the report is ready
|
154 |
|
|
|
2 |
import openai
|
3 |
import requests
|
4 |
from bs4 import BeautifulSoup
|
5 |
+
from urllib.parse import urljoin, urlparse
|
6 |
import os
|
7 |
import re
|
8 |
|
|
|
54 |
|
55 |
return " ".join(all_content[:3000]), scrape_successful
|
56 |
|
57 |
+
def infer_business_info_from_url(url):
|
58 |
+
"""
|
59 |
+
Infer business details from the domain name.
|
60 |
+
"""
|
61 |
+
domain_name = urlparse(url).netloc
|
62 |
+
inferred_info = openai.ChatCompletion.create(
|
63 |
+
model="gpt-4",
|
64 |
+
messages=[
|
65 |
+
{
|
66 |
+
"role": "system",
|
67 |
+
"content": "You are a business analyst. Based on domain names, generate likely information about a business, including its industry, target audience, and goals."
|
68 |
+
},
|
69 |
+
{
|
70 |
+
"role": "user",
|
71 |
+
"content": f"The domain is {domain_name}. What can you infer about this business?"
|
72 |
+
}
|
73 |
+
],
|
74 |
+
max_tokens=200,
|
75 |
+
temperature=0.7
|
76 |
+
)
|
77 |
+
return inferred_info["choices"][0]["message"]["content"]
|
78 |
+
|
79 |
def extract_location(content):
|
80 |
"""
|
81 |
Extract a possible location from the website content using regular expressions.
|
|
|
83 |
location_match = re.search(r'\b(?:serving|located in|offices in|based in)\s([\w\s,]+)', content, re.IGNORECASE)
|
84 |
return location_match.group(1).strip() if location_match else None
|
85 |
|
86 |
+
def generate_marketing_plan(website_content, industry, goals, budget, location, inferred_info, messages, fallback=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
"""
|
88 |
Generates a marketing plan based on website content, industry, goals, and budget.
|
89 |
"""
|
90 |
location_info = f"The business is located in {location}." if location else "No specific location was mentioned."
|
91 |
+
additional_info = f"Inferred details: {inferred_info}" if inferred_info else "No additional business details were inferred."
|
92 |
|
93 |
query = f"""
|
94 |
The user has provided the following details:
|
|
|
97 |
- Goals for 2025: {goals}
|
98 |
- Marketing budget for 2025: ${budget}
|
99 |
- {location_info}
|
100 |
+
- {additional_info}
|
101 |
|
102 |
Create a detailed 1-year marketing plan that includes:
|
103 |
1. **Advanced Keywords**: Long-tail keywords specific to the industry and location (if applicable).
|
|
|
129 |
with col1:
|
130 |
st.markdown("<h2 style='text-align: center; color: black;'>Enter Business Details</h2>", unsafe_allow_html=True)
|
131 |
website_url = st.text_input("Enter your business website", placeholder="e.g., https://example.com")
|
132 |
+
industry = st.text_input("Industry (optional)", placeholder="E.g., Real Estate, Retail, Technology")
|
133 |
+
goals = st.text_area("Goals for 2025 (optional)", placeholder="E.g., increase brand awareness, drive online sales")
|
134 |
budget = st.number_input("Marketing Budget for 2025 ($)", min_value=1000, step=1000)
|
135 |
generate_button = st.button('Generate Marketing Plan')
|
136 |
|
|
|
140 |
with st.spinner("Analyzing website content and preparing your report..."):
|
141 |
website_content, scrape_successful = scrape_website(website_url) if website_url else ("", False)
|
142 |
location = extract_location(website_content) if scrape_successful else None
|
143 |
+
inferred_info = infer_business_info_from_url(website_url) if not scrape_successful else None
|
144 |
fallback_mode = not scrape_successful
|
145 |
if fallback_mode:
|
146 |
+
st.warning("Unable to retrieve website content. Generating recommendations based on inferred details.")
|
147 |
messages = initial_messages.copy()
|
148 |
st.session_state["reply"] = generate_marketing_plan(
|
149 |
website_content if scrape_successful else "N/A",
|
150 |
+
industry, goals, budget, location, inferred_info, messages, fallback=fallback_mode
|
151 |
)
|
152 |
st.session_state["show_notice"] = False # Remove the notice once the report is ready
|
153 |
|