Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import requests
|
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
from urllib.parse import urljoin
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
# Ensure your OpenAI API key is set in your environment variables
|
| 9 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
@@ -49,25 +50,29 @@ def scrape_website(url, max_pages=5):
|
|
| 49 |
to_visit.append(full_url)
|
| 50 |
|
| 51 |
except Exception:
|
| 52 |
-
# Silently skip any errors during scraping
|
| 53 |
continue
|
| 54 |
|
| 55 |
return " ".join(all_content[:3000]), scrape_successful
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# Initial system message setup
|
| 58 |
initial_messages = [{
|
| 59 |
"role": "system",
|
| 60 |
"content": """You are a world-class marketing strategist trained by Neil Patel, David Ogilvy, and Seth Godin.
|
| 61 |
-
Your task is to create highly customized
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
-
|
| 65 |
-
-
|
| 66 |
-
|
| 67 |
-
-
|
| 68 |
-
|
| 69 |
-
- Be specific to the business's industry, competitive landscape, and target audience.
|
| 70 |
-
Ensure every suggestion feels fresh, creative, and deeply tailored to the business's needs."""
|
| 71 |
}]
|
| 72 |
|
| 73 |
def call_openai_api(messages):
|
|
@@ -82,21 +87,29 @@ def call_openai_api(messages):
|
|
| 82 |
)
|
| 83 |
return response["choices"][0]["message"]["content"]
|
| 84 |
|
| 85 |
-
def generate_marketing_plan(website_content, industry, goals, budget, messages, fallback=False):
|
| 86 |
"""
|
| 87 |
-
Generates a marketing plan based on website content, industry, and
|
| 88 |
"""
|
|
|
|
|
|
|
| 89 |
query = f"""
|
| 90 |
The user has provided the following details:
|
| 91 |
- Website content: {website_content if not fallback else "N/A (website content could not be retrieved)"}
|
| 92 |
- Industry: {industry}
|
| 93 |
- Goals for 2025: {goals}
|
| 94 |
- Marketing budget for 2025: ${budget}
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
messages.append({"role": "user", "content": query})
|
| 102 |
return call_openai_api(messages)
|
|
@@ -128,13 +141,14 @@ if generate_button:
|
|
| 128 |
st.session_state["show_notice"] = True
|
| 129 |
with st.spinner("Analyzing website content and preparing your report..."):
|
| 130 |
website_content, scrape_successful = scrape_website(website_url) if website_url else ("", False)
|
|
|
|
| 131 |
fallback_mode = not scrape_successful
|
| 132 |
if fallback_mode:
|
| 133 |
st.warning("Unable to retrieve website content. Generating recommendations based on your input.")
|
| 134 |
messages = initial_messages.copy()
|
| 135 |
st.session_state["reply"] = generate_marketing_plan(
|
| 136 |
website_content if scrape_successful else "N/A",
|
| 137 |
-
industry, goals, budget, messages, fallback=fallback_mode
|
| 138 |
)
|
| 139 |
st.session_state["show_notice"] = False # Remove the notice once the report is ready
|
| 140 |
|
|
|
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
from urllib.parse import urljoin
|
| 6 |
import os
|
| 7 |
+
import re
|
| 8 |
|
| 9 |
# Ensure your OpenAI API key is set in your environment variables
|
| 10 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 50 |
to_visit.append(full_url)
|
| 51 |
|
| 52 |
except Exception:
|
|
|
|
| 53 |
continue
|
| 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.
|
| 60 |
+
"""
|
| 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 |
# Initial system message setup
|
| 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):
|
|
|
|
| 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:
|
| 98 |
- Website content: {website_content if not fallback else "N/A (website content could not be retrieved)"}
|
| 99 |
- Industry: {industry}
|
| 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).
|
| 106 |
+
2. **Content Topics**: Blog and YouTube video topics that target the business's goals and location.
|
| 107 |
+
3. **Social Media Strategies**: Platform recommendations, post frequency, and campaign ideas tailored to the location.
|
| 108 |
+
4. **Advertising Campaigns**: Target audience, platforms, and budget breakdowns, integrating location-specific targeting.
|
| 109 |
+
5. **Emerging Platforms**: Recommendations for new or underutilized platforms.
|
| 110 |
+
6. **SEO Improvements**: Tools, techniques, and steps to improve search rankings.
|
| 111 |
+
7. **Execution Plan**: Actionable, step-by-step instructions for implementation with quarterly timelines.
|
| 112 |
+
Ensure all suggestions align with the business's strengths, and avoid generic or obvious recommendations."""
|
| 113 |
|
| 114 |
messages.append({"role": "user", "content": query})
|
| 115 |
return call_openai_api(messages)
|
|
|
|
| 141 |
st.session_state["show_notice"] = True
|
| 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 your input.")
|
| 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 |
|