nniehaus commited on
Commit
bc46207
·
verified ·
1 Parent(s): e02d622

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -16
app.py CHANGED
@@ -1,10 +1,55 @@
1
  import streamlit as st
2
  import openai
 
 
 
3
  import os
4
 
5
  # Ensure your OpenAI API key is set in your environment variables
6
  openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # Initial system message setup
9
  initial_messages = [{
10
  "role": "system",
@@ -15,39 +60,41 @@ initial_messages = [{
15
 
16
  Your advice should feel like it is coming from a personal consultant who deeply understands the business. Go beyond generalities,
17
  and include specific suggestions for platforms, tools, campaigns, or techniques. If applicable, suggest measurable KPIs to track success.
18
- If the company is already doing well in some areas, suggest how they can take those efforts to the next level. Your recommendations are specific and contain
19
- suggestions. For example, if you suggest blogging you'll recommend the exact keywords they should incoporate. If you recommend using video you can give them
20
- several topic suggestions. The user should leave with a plan so precise that they don't need to do any further research."""
21
  }]
22
 
23
-
24
  def call_openai_api(messages):
25
  """
26
  Calls the OpenAI ChatCompletion API with the correct format.
27
  """
28
  response = openai.ChatCompletion.create(
29
- model="gpt-4", # Ensure you're using the correct model
30
  messages=messages,
31
  max_tokens=3000,
32
  temperature=0.7
33
  )
34
  return response["choices"][0]["message"]["content"]
35
 
36
- def generate_marketing_plan(website_info, industry, goals, budget, messages):
37
  """
38
- Generates a marketing plan based on website information, industry, and user goals.
39
  """
40
  query = f"""
41
  The user has provided the following details:
42
- - Website information: {website_info}
43
  - Industry: {industry}
44
  - Goals for 2025: {goals}
45
  - Marketing budget for 2025: ${budget}
46
 
47
- Create a comprehensive marketing plan for 2025. Include specific strategies
48
- (e.g., content marketing, social media, advertising, SEO) and a timeline for implementing them.
49
- Highlight how the website's strengths can be leveraged to achieve the stated goals.
50
- """
 
 
 
 
 
51
 
52
  messages.append({"role": "user", "content": query})
53
  return call_openai_api(messages)
@@ -66,16 +113,21 @@ st.markdown("<h1 style='text-align: center; color: black;'>2025 Marketing Planne
66
  col1, col2 = st.columns(2)
67
  with col1:
68
  st.markdown("<h2 style='text-align: center; color: black;'>Enter Business Details</h2>", unsafe_allow_html=True)
69
- website_info = st.text_input("Website Information", placeholder="Provide key details or a URL")
70
  industry = st.text_input("Industry", placeholder="E.g., Real Estate, Retail, Technology")
71
  goals = st.text_area("Goals for 2025", placeholder="E.g., increase brand awareness, drive online sales")
72
  budget = st.number_input("Marketing Budget for 2025 ($)", min_value=1000, step=1000)
73
  generate_button = st.button('Generate Marketing Plan')
74
 
75
  # Process results on button click
76
- if generate_button and website_info:
77
- messages = initial_messages.copy()
78
- st.session_state["reply"] = generate_marketing_plan(website_info, industry, goals, budget, messages)
 
 
 
 
 
79
 
80
  # Display results if there is a reply in session state
81
  if st.session_state["reply"]:
 
1
  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
 
8
  # Ensure your OpenAI API key is set in your environment variables
9
  openai.api_key = os.getenv("OPENAI_API_KEY")
10
 
11
+ def scrape_website(url, max_pages=5):
12
+ """
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
+ visited = set()
17
+ to_visit = [url]
18
+ all_content = []
19
+
20
+ while to_visit and len(visited) < max_pages:
21
+ current_url = to_visit.pop(0)
22
+ if current_url in visited:
23
+ continue
24
+
25
+ try:
26
+ response = requests.get(current_url)
27
+ response.raise_for_status()
28
+ soup = BeautifulSoup(response.content, "html.parser")
29
+ visited.add(current_url)
30
+
31
+ # Extract meaningful content (e.g., meta description, main text)
32
+ meta_description = soup.find("meta", {"name": "description"})
33
+ if meta_description and meta_description.get("content"):
34
+ all_content.append(meta_description["content"])
35
+
36
+ # Extract main text (e.g., headers, paragraphs)
37
+ paragraphs = soup.find_all("p")
38
+ for para in paragraphs:
39
+ all_content.append(para.get_text(strip=True))
40
+
41
+ # Extract internal links
42
+ links = soup.find_all("a", href=True)
43
+ for link in links:
44
+ full_url = urljoin(current_url, link["href"])
45
+ if url in full_url and full_url not in visited:
46
+ to_visit.append(full_url)
47
+
48
+ except Exception as e:
49
+ st.warning(f"Error fetching {current_url}: {e}")
50
+
51
+ return " ".join(all_content[:3000]) # Limit content length for OpenAI API
52
+
53
  # Initial system message setup
54
  initial_messages = [{
55
  "role": "system",
 
60
 
61
  Your advice should feel like it is coming from a personal consultant who deeply understands the business. Go beyond generalities,
62
  and include specific suggestions for platforms, tools, campaigns, or techniques. If applicable, suggest measurable KPIs to track success.
63
+ If the company is already doing well in some areas, suggest how they can take those efforts to the next level."""
 
 
64
  }]
65
 
 
66
  def call_openai_api(messages):
67
  """
68
  Calls the OpenAI ChatCompletion API with the correct format.
69
  """
70
  response = openai.ChatCompletion.create(
71
+ model="gpt-4",
72
  messages=messages,
73
  max_tokens=3000,
74
  temperature=0.7
75
  )
76
  return response["choices"][0]["message"]["content"]
77
 
78
+ def generate_marketing_plan(website_content, industry, goals, budget, messages):
79
  """
80
+ Generates a marketing plan based on website content, industry, and user goals.
81
  """
82
  query = f"""
83
  The user has provided the following details:
84
+ - Website content: {website_content}
85
  - Industry: {industry}
86
  - Goals for 2025: {goals}
87
  - Marketing budget for 2025: ${budget}
88
 
89
+ Based on this information, create a comprehensive, customized 1-year marketing plan for 2025.
90
+ Your output should include:
91
+ 1. **Content Marketing**: Suggestions for blogs, videos, or other content types. Provide 3-5 actionable steps for implementation.
92
+ 2. **Social Media Strategy**: Recommend specific platforms, posting frequency, and campaign ideas, with measurable goals or KPIs.
93
+ 3. **Advertising Campaigns**: Outline paid ad strategies (e.g., Google Ads, Facebook Ads). Include budget allocation and expected ROI.
94
+ 4. **Search Engine Optimization (SEO)**: Suggest improvements or new tactics, including tools or techniques they can use.
95
+ 5. **Innovative Approaches**: Any unique or industry-specific ideas that would differentiate this business.
96
+
97
+ For each strategy, explain how it aligns with the business's goals and utilizes its current strengths. Provide a quarterly timeline to help them implement these strategies effectively."""
98
 
99
  messages.append({"role": "user", "content": query})
100
  return call_openai_api(messages)
 
113
  col1, col2 = st.columns(2)
114
  with col1:
115
  st.markdown("<h2 style='text-align: center; color: black;'>Enter Business Details</h2>", unsafe_allow_html=True)
116
+ website_url = st.text_input("Enter your business website", placeholder="https://example.com")
117
  industry = st.text_input("Industry", placeholder="E.g., Real Estate, Retail, Technology")
118
  goals = st.text_area("Goals for 2025", placeholder="E.g., increase brand awareness, drive online sales")
119
  budget = st.number_input("Marketing Budget for 2025 ($)", min_value=1000, step=1000)
120
  generate_button = st.button('Generate Marketing Plan')
121
 
122
  # Process results on button click
123
+ if generate_button and website_url:
124
+ with st.spinner("Analyzing website content..."):
125
+ website_content = scrape_website(website_url)
126
+ if website_content:
127
+ messages = initial_messages.copy()
128
+ st.session_state["reply"] = generate_marketing_plan(website_content, industry, goals, budget, messages)
129
+ else:
130
+ st.warning("Unable to retrieve website content. Please check the URL or try again.")
131
 
132
  # Display results if there is a reply in session state
133
  if st.session_state["reply"]: