Update app.py
Browse files123456789104 unmodified lines15161718192021222324252627282930313233343536373839404142434445464748495051525312 unmodified lines66676869707172737475767778798081import streamlit as stimport openaiimport os# Ensure your OpenAI API key is set in your environment variablesopenai.api_key = os.getenv("OPENAI_API_KEY")# Initial system message setupinitial_messages = [{"role": "system",4 unmodified linesYour advice should feel like it is coming from a personal consultant who deeply understands the business. Go beyond generalities,and include specific suggestions for platforms, tools, campaigns, or techniques. If applicable, suggest measurable KPIs to track success.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 containsuggestions. For example, if you suggest blogging you'll recommend the exact keywords they should incoporate. If you recommend using video you can give themseveral topic suggestions. The user should leave with a plan so precise that they don't need to do any further research."""}]def call_openai_api(messages):"""Calls the OpenAI ChatCompletion API with the correct format."""response = openai.ChatCompletion.create(model="gpt-4", # Ensure you're using the correct modelmessages=messages,max_tokens=3000,temperature=0.7)return response["choices"][0]["message"]["content"]def generate_marketing_plan(website_info, industry, goals, budget, messages):"""Generates a marketing plan based on website information, industry, and user goals."""query = f"""The user has provided the following details:- Website information: {website_info}- Industry: {industry}- Goals for 2025: {goals}- Marketing budget for 2025: ${budget}Create a comprehensive marketing plan for 2025. Include specific strategies(e.g., content marketing, social media, advertising, SEO) and a timeline for implementing them.Highlight how the website's strengths can be leveraged to achieve the stated goals."""messages.append({"role": "user", "content": query})return call_openai_api(messages)12 unmodified linescol1, col2 = st.columns(2)with col1:st.markdown("<h2 style='text-align: center; color: black;'>Enter Business Details</h2>", unsafe_allow_html=True)website_info = st.text_input("Website Information", placeholder="Provide key details or a URL")industry = st.text_input("Industry", placeholder="E.g., Real Estate, Retail, Technology")goals = st.text_area("Goals for 2025", placeholder="E.g., increase brand awareness, drive online sales")budget = st.number_input("Marketing Budget for 2025 ($)", min_value=1000, step=1000)generate_button = st.button('Generate Marketing Plan')# Process results on button clickif generate_button and website_info:messages = initial_messages.copy()st.session_state["reply"] = generate_marketing_plan(website_info, industry, goals, budget, messages)# Display results if there is a reply in session stateif st.session_state["reply"]:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354554 unmodified lines6061626364656667686970717273747576777879808182838485868788899091929394959697989910012 unmodified lines113114115116117118119120121122123124125126127128129130131132133import streamlit as stimport openaiimport requestsfrom bs4 import BeautifulSoupfrom urllib.parse import urljoinimport os# Ensure your OpenAI API key is set in your environment variablesopenai.api_key = os.getenv("OPENAI_API_KEY")def scrape_website(url, max_pages=5):"""Crawls and scrapes content from the given website URL.Follows internal links and extracts meaningful information from up to `max_pages` pages."""visited = set()to_visit = [url]all_content = []while to_visit and len(visited) < max_pages:current_url = to_visit.pop(0)if current_url in visited:continuetry:response = requests.get(current_url)response.raise_for_status()soup = BeautifulSoup(response.content, "html.parser")visited.add(current_url)# Extract meaningful content (e.g., meta description, main text)meta_description = soup.find("meta", {"name": "description"})if meta_description and meta_description.get("content"):all_content.append(meta_description["content"])# Extract main text (e.g., headers, paragraphs)paragraphs = soup.find_all("p")for para in paragraphs:all_content.append(para.get_text(strip=True))# Extract internal linkslinks = soup.find_all("a", href=True)for link in links:full_url = urljoin(current_url, link["href"])if url in full_url and full_url not in visited:to_visit.append(full_url)except Exception as e:st.warning(f"Error fetching {current_url}: {e}")return " ".join(all_content[:3000]) # Limit content length for OpenAI API# Initial system message setupinitial_messages = [{"role": "system",4 unmodified linesYour advice should feel like it is coming from a personal consultant who deeply understands the business. Go beyond generalities,and include specific suggestions for platforms, tools, campaigns, or techniques. If applicable, suggest measurable KPIs to track success.If the company is already doing well in some areas, suggest how they can take those efforts to the next level."""}]def call_openai_api(messages):"""Calls the OpenAI ChatCompletion API with the correct format."""response = openai.ChatCompletion.create(model="gpt-4",messages=messages,max_tokens=3000,temperature=0.7)return response["choices"][0]["message"]["content"]def generate_marketing_plan(website_content, industry, goals, budget, messages):"""Generates a marketing plan based on website content, industry, and user goals."""query = f"""The user has provided the following details:- Website content: {website_content}- Industry: {industry}- Goals for 2025: {goals}- Marketing budget for 2025: ${budget}Based on this information, create a comprehensive, customized 1-year marketing plan for 2025.Your output should include:1. **Content Marketing**: Suggestions for blogs, videos, or other content types. Provide 3-5 actionable steps for implementation.2. **Social Media Strategy**: Recommend specific platforms, posting frequency, and campaign ideas, with measurable goals or KPIs.3. **Advertising Campaigns**: Outline paid ad strategies (e.g., Google Ads, Facebook Ads). Include budget allocation and expected ROI.4. **Search Engine Optimization (SEO)**: Suggest improvements or new tactics, including tools or techniques they can use.5. **Innovative Approaches**: Any unique or industry-specific ideas that would differentiate this business.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."""messages.append({"role": "user", "content": query})return call_openai_api(messages)12 unmodified linescol1, col2 = st.columns(2)with col1:st.markdown("<h2 style='text-align: center; color: black;'>Enter Business Details</h2>", unsafe_allow_html=True)website_url = st.text_input("Enter your business website", placeholder="https://example.com")industry = st.text_input("Industry", placeholder="E.g., Real Estate, Retail, Technology")goals = st.text_area("Goals for 2025", placeholder="E.g., increase brand awareness, drive online sales")budget = st.number_input("Marketing Budget for 2025 ($)", min_value=1000, step=1000)generate_button = st.button('Generate Marketing Plan')# Process results on button clickif generate_button and website_url:with st.spinner("Analyzing website content..."):website_content = scrape_website(website_url)if website_content:messages = initial_messages.copy()st.session_state["reply"] = generate_marketing_plan(website_content, industry, goals, budget, messages)else:st.warning("Unable to retrieve website content. Please check the URL or try again.")# Display results if there is a reply in session stateif st.session_state["reply"]:
app.py+68-16