Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,6 @@ def scrape_website(url, max_pages=5):
|
|
| 13 |
Crawls and scrapes content from the given website URL.
|
| 14 |
Follows internal links and extracts meaningful information from up to `max_pages` pages.
|
| 15 |
"""
|
| 16 |
-
# Ensure URL has the correct format
|
| 17 |
if not url.startswith("http"):
|
| 18 |
url = f"https://{url}"
|
| 19 |
|
|
@@ -27,22 +26,20 @@ def scrape_website(url, max_pages=5):
|
|
| 27 |
continue
|
| 28 |
|
| 29 |
try:
|
| 30 |
-
response = requests.get(current_url)
|
| 31 |
response.raise_for_status()
|
| 32 |
soup = BeautifulSoup(response.content, "html.parser")
|
| 33 |
visited.add(current_url)
|
| 34 |
|
| 35 |
-
# Extract meaningful content
|
| 36 |
meta_description = soup.find("meta", {"name": "description"})
|
| 37 |
if meta_description and meta_description.get("content"):
|
| 38 |
all_content.append(meta_description["content"])
|
| 39 |
|
| 40 |
-
# Extract main text (e.g., headers, paragraphs)
|
| 41 |
paragraphs = soup.find_all("p")
|
| 42 |
for para in paragraphs:
|
| 43 |
all_content.append(para.get_text(strip=True))
|
| 44 |
|
| 45 |
-
# Extract internal links
|
| 46 |
links = soup.find_all("a", href=True)
|
| 47 |
for link in links:
|
| 48 |
full_url = urljoin(current_url, link["href"])
|
|
@@ -52,22 +49,14 @@ def scrape_website(url, max_pages=5):
|
|
| 52 |
except Exception as e:
|
| 53 |
st.warning(f"Error fetching {current_url}: {e}")
|
| 54 |
|
| 55 |
-
return " ".join(all_content[:3000])
|
| 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 |
You specialize in creating precise, highly actionable, and detailed 1-year marketing plans tailored to businesses' specific needs.
|
| 62 |
-
|
| 63 |
-
- Lists of specific keywords for blogs, videos, or SEO.
|
| 64 |
-
- Titles and topics for YouTube videos, blog posts, or other content.
|
| 65 |
-
- Ad campaign structures, including target audiences and platforms.
|
| 66 |
-
- Step-by-step implementation details for each suggestion.
|
| 67 |
-
- Measurable KPIs or success metrics.
|
| 68 |
-
|
| 69 |
-
Your advice must be execution-ready, requiring minimal further planning by the business owner.
|
| 70 |
-
Leverage the website information provided to deeply customize your suggestions, ensuring alignment with the business's goals and strengths."""
|
| 71 |
}]
|
| 72 |
|
| 73 |
def call_openai_api(messages):
|
|
@@ -82,27 +71,27 @@ 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):
|
| 86 |
"""
|
| 87 |
Generates a marketing plan based on website content, industry, and user goals.
|
| 88 |
"""
|
| 89 |
query = f"""
|
| 90 |
The user has provided the following details:
|
| 91 |
-
- Website content: {website_content}
|
| 92 |
- Industry: {industry}
|
| 93 |
- Goals for 2025: {goals}
|
| 94 |
- Marketing budget for 2025: ${budget}
|
| 95 |
-
|
| 96 |
Create a comprehensive, customized 1-year marketing plan for 2025.
|
| 97 |
Include:
|
| 98 |
-
1. **
|
| 99 |
-
2. **
|
| 100 |
-
3. **
|
| 101 |
-
4. **
|
| 102 |
-
5. **
|
| 103 |
-
6. **
|
| 104 |
-
|
| 105 |
-
Ensure all suggestions align with the business's goals and strengths, and include a quarterly timeline for implementation.
|
|
|
|
| 106 |
|
| 107 |
messages.append({"role": "user", "content": query})
|
| 108 |
return call_openai_api(messages)
|
|
@@ -128,17 +117,20 @@ with col1:
|
|
| 128 |
generate_button = st.button('Generate Marketing Plan')
|
| 129 |
|
| 130 |
# Process results on button click
|
| 131 |
-
if generate_button
|
| 132 |
with st.spinner("Analyzing website content..."):
|
| 133 |
-
website_content = scrape_website(website_url)
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
st.
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
# Display results if there is a reply in session state
|
| 141 |
if st.session_state["reply"]:
|
| 142 |
with col2:
|
| 143 |
st.markdown("<h2 style='text-align: center; color: black;'>Your 2025 Marketing Plan ⬇️</h2>", unsafe_allow_html=True)
|
| 144 |
-
st.
|
|
|
|
| 13 |
Crawls and scrapes content from the given website URL.
|
| 14 |
Follows internal links and extracts meaningful information from up to `max_pages` pages.
|
| 15 |
"""
|
|
|
|
| 16 |
if not url.startswith("http"):
|
| 17 |
url = f"https://{url}"
|
| 18 |
|
|
|
|
| 26 |
continue
|
| 27 |
|
| 28 |
try:
|
| 29 |
+
response = requests.get(current_url, timeout=10)
|
| 30 |
response.raise_for_status()
|
| 31 |
soup = BeautifulSoup(response.content, "html.parser")
|
| 32 |
visited.add(current_url)
|
| 33 |
|
| 34 |
+
# Extract meaningful content
|
| 35 |
meta_description = soup.find("meta", {"name": "description"})
|
| 36 |
if meta_description and meta_description.get("content"):
|
| 37 |
all_content.append(meta_description["content"])
|
| 38 |
|
|
|
|
| 39 |
paragraphs = soup.find_all("p")
|
| 40 |
for para in paragraphs:
|
| 41 |
all_content.append(para.get_text(strip=True))
|
| 42 |
|
|
|
|
| 43 |
links = soup.find_all("a", href=True)
|
| 44 |
for link in links:
|
| 45 |
full_url = urljoin(current_url, link["href"])
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
st.warning(f"Error fetching {current_url}: {e}")
|
| 51 |
|
| 52 |
+
return " ".join(all_content[:3000])
|
| 53 |
|
| 54 |
# Initial system message setup
|
| 55 |
initial_messages = [{
|
| 56 |
"role": "system",
|
| 57 |
"content": """You are a world-class marketing strategist trained by Neil Patel, David Ogilvy, and Seth Godin.
|
| 58 |
You specialize in creating precise, highly actionable, and detailed 1-year marketing plans tailored to businesses' specific needs.
|
| 59 |
+
Ensure all advice aligns with the user's goals and strengths."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
}]
|
| 61 |
|
| 62 |
def call_openai_api(messages):
|
|
|
|
| 71 |
)
|
| 72 |
return response["choices"][0]["message"]["content"]
|
| 73 |
|
| 74 |
+
def generate_marketing_plan(website_content, industry, goals, budget, messages, fallback=False):
|
| 75 |
"""
|
| 76 |
Generates a marketing plan based on website content, industry, and user goals.
|
| 77 |
"""
|
| 78 |
query = f"""
|
| 79 |
The user has provided the following details:
|
| 80 |
+
- Website content: {website_content if not fallback else "N/A (website content could not be retrieved)"}
|
| 81 |
- Industry: {industry}
|
| 82 |
- Goals for 2025: {goals}
|
| 83 |
- Marketing budget for 2025: ${budget}
|
|
|
|
| 84 |
Create a comprehensive, customized 1-year marketing plan for 2025.
|
| 85 |
Include:
|
| 86 |
+
1. **Overview**: Summarize the input details, highlighting any missing information.
|
| 87 |
+
2. **Keywords**: Provide a list of specific keywords to target for blogs, videos, and SEO.
|
| 88 |
+
3. **Content Topics**: Suggest blog and YouTube video topics with detailed titles.
|
| 89 |
+
4. **Social Media**: Recommend platforms, posting frequency, and campaign ideas with measurable goals.
|
| 90 |
+
5. **Advertising Campaigns**: Outline paid ad strategies, including platforms, target audiences, and budget allocation.
|
| 91 |
+
6. **SEO Improvements**: Suggest tools, techniques, and steps to improve search rankings.
|
| 92 |
+
7. **Execution Steps**: Provide actionable, step-by-step instructions for each recommendation.
|
| 93 |
+
Ensure all suggestions align with the business's goals and strengths, and include a quarterly timeline for implementation.
|
| 94 |
+
If fallback mode is active, note the lack of website content in the overview but still provide high-value recommendations based on the user's input."""
|
| 95 |
|
| 96 |
messages.append({"role": "user", "content": query})
|
| 97 |
return call_openai_api(messages)
|
|
|
|
| 117 |
generate_button = st.button('Generate Marketing Plan')
|
| 118 |
|
| 119 |
# Process results on button click
|
| 120 |
+
if generate_button:
|
| 121 |
with st.spinner("Analyzing website content..."):
|
| 122 |
+
website_content = scrape_website(website_url) if website_url else None
|
| 123 |
+
fallback_mode = not website_content
|
| 124 |
+
if fallback_mode:
|
| 125 |
+
st.warning("Unable to retrieve website content. Generating recommendations based on your input.")
|
| 126 |
+
messages = initial_messages.copy()
|
| 127 |
+
st.session_state["reply"] = generate_marketing_plan(
|
| 128 |
+
website_content if website_content else "N/A",
|
| 129 |
+
industry, goals, budget, messages, fallback=fallback_mode
|
| 130 |
+
)
|
| 131 |
|
| 132 |
# Display results if there is a reply in session state
|
| 133 |
if st.session_state["reply"]:
|
| 134 |
with col2:
|
| 135 |
st.markdown("<h2 style='text-align: center; color: black;'>Your 2025 Marketing Plan ⬇️</h2>", unsafe_allow_html=True)
|
| 136 |
+
st.markdown(st.session_state["reply"])
|