import streamlit as st import openai import requests from bs4 import BeautifulSoup from urllib.parse import urljoin import os # Ensure your OpenAI API key is set in your environment variables openai.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. """ if not url.startswith("http"): url = f"https://{url}" visited = set() to_visit = [url] all_content = [] scrape_successful = False while to_visit and len(visited) < max_pages: current_url = to_visit.pop(0) if current_url in visited: continue try: response = requests.get(current_url, timeout=10) response.raise_for_status() soup = BeautifulSoup(response.content, "html.parser") visited.add(current_url) scrape_successful = True # Extract meaningful content meta_description = soup.find("meta", {"name": "description"}) if meta_description and meta_description.get("content"): all_content.append(meta_description["content"]) paragraphs = soup.find_all("p") for para in paragraphs: all_content.append(para.get_text(strip=True)) links = 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: # Silently skip any errors during scraping continue return " ".join(all_content[:3000]), scrape_successful # Initial system message setup initial_messages = [{ "role": "system", "content": """You are a world-class marketing strategist trained by Neil Patel, David Ogilvy, and Seth Godin. Your task is to create highly customized and innovative marketing plans based on the provided details. Go beyond generic strategies and use advanced analysis to recommend: - Marketing tactics inspired by successful case studies from the same or similar industries. - Unique approaches that leverage emerging trends, tools, and platforms. - Recommendations that align with the business's budget and specific goals. Each strategy must: - Be actionable, with clear steps to execute. - Include measurable KPIs to track success. - Be specific to the business's industry, competitive landscape, and target audience. Ensure every suggestion feels fresh, creative, and deeply tailored to the business's needs.""" }] 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, fallback=False): """ 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 if not fallback else "N/A (website content could not be retrieved)"} - Industry: {industry} - Goals for 2025: {goals} - Marketing budget for 2025: ${budget} Create a comprehensive, customized 1-year marketing plan for 2025, including: - Advanced analysis based on successful case studies and trends. - Unique strategies for the business to stand out. - Actionable steps with measurable KPIs. Avoid generic suggestions; focus on innovative and practical ideas.""" messages.append({"role": "user", "content": query}) return call_openai_api(messages) # Streamlit setup st.set_page_config(layout="wide") # Initialize session state if "reply" not in st.session_state: st.session_state["reply"] = None if "show_notice" not in st.session_state: st.session_state["show_notice"] = False # Centered title st.markdown("