Update app.py
Browse files1 unmodified line234567845 unmodified lines5455565758591 unmodified line616263646566676869707172737475767778798081828384858687888990919293949596972 unmodified lines10010110210310410525 unmodified lines1311321331341351361371383 unmodified lines1421431441451461471481491501511521531541 unmodified lineimport openaiimport requestsfrom bs4 import BeautifulSoupfrom urllib.parse import urljoinimport osimport re45 unmodified linesreturn " ".join(all_content[:3000]), scrape_successfuldef extract_location(content):"""Extract a possible location from the website content using regular expressions.1 unmodified linelocation_match = re.search(r'\b(?:serving|located in|offices in|based in)\s([\w\s,]+)', content, re.IGNORECASE)return location_match.group(1).strip() if location_match else None# Initial system message setupinitial_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 marketing plans based on user input. Incorporate any business locationor target areas explicitly mentioned in the website content or user-provided details into the recommendations.Go beyond generic suggestions, and include:- Specific, long-tail keywords to target.- Detailed content ideas, including blogs, videos, and social media campaigns.- Unique strategies tailored to the business's industry, goals, and location.- Innovative advertising campaigns and emerging platform recommendations.Ensure every suggestion is actionable and includes measurable KPIs."""}]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, location, messages, fallback=False):"""Generates a marketing plan based on website content, industry, goals, and budget."""location_info = f"The business is located in {location}." if location else "No specific location was mentioned."query = f"""The user has provided the following details:2 unmodified lines- Goals for 2025: {goals}- Marketing budget for 2025: ${budget}- {location_info}Create a detailed 1-year marketing plan that includes:1. **Advanced Keywords**: Long-tail keywords specific to the industry and location (if applicable).25 unmodified lineswith 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="e.g., 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')3 unmodified lineswith st.spinner("Analyzing website content and preparing your report..."):website_content, scrape_successful = scrape_website(website_url) if website_url else ("", False)location = extract_location(website_content) if scrape_successful else Nonefallback_mode = not scrape_successfulif fallback_mode:st.warning("Unable to retrieve website content. Generating recommendations based on your input.")messages = initial_messages.copy()st.session_state["reply"] = generate_marketing_plan(website_content if scrape_successful else "N/A",industry, goals, budget, location, messages, fallback=fallback_mode)st.session_state["show_notice"] = False # Remove the notice once the report is ready1 unmodified line234567845 unmodified lines545556575859606162636465666768697071727374757677787980811 unmodified line8384858687888990919293942 unmodified lines97989910010110210325 unmodified lines1291301311321331341351363 unmodified lines1401411421431441451461471481491501511521531 unmodified lineimport openaiimport requestsfrom bs4 import BeautifulSoupfrom urllib.parse import urljoin, urlparseimport osimport re45 unmodified linesreturn " ".join(all_content[:3000]), scrape_successfuldef infer_business_info_from_url(url):"""Infer business details from the domain name."""domain_name = urlparse(url).netlocinferred_info = openai.ChatCompletion.create(model="gpt-4",messages=[{"role": "system","content": "You are a business analyst. Based on domain names, generate likely information about a business, including its industry, target audience, and goals."},{"role": "user","content": f"The domain is {domain_name}. What can you infer about this business?"}],max_tokens=200,temperature=0.7)return inferred_info["choices"][0]["message"]["content"]def extract_location(content):"""Extract a possible location from the website content using regular expressions.1 unmodified linelocation_match = re.search(r'\b(?:serving|located in|offices in|based in)\s([\w\s,]+)', content, re.IGNORECASE)return location_match.group(1).strip() if location_match else Nonedef generate_marketing_plan(website_content, industry, goals, budget, location, inferred_info, messages, fallback=False):"""Generates a marketing plan based on website content, industry, goals, and budget."""location_info = f"The business is located in {location}." if location else "No specific location was mentioned."additional_info = f"Inferred details: {inferred_info}" if inferred_info else "No additional business details were inferred."query = f"""The user has provided the following details:2 unmodified lines- Goals for 2025: {goals}- Marketing budget for 2025: ${budget}- {location_info}- {additional_info}Create a detailed 1-year marketing plan that includes:1. **Advanced Keywords**: Long-tail keywords specific to the industry and location (if applicable).25 unmodified lineswith 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="e.g., https://example.com")industry = st.text_input("Industry (optional)", placeholder="E.g., Real Estate, Retail, Technology")goals = st.text_area("Goals for 2025 (optional)", 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')3 unmodified lineswith st.spinner("Analyzing website content and preparing your report..."):website_content, scrape_successful = scrape_website(website_url) if website_url else ("", False)location = extract_location(website_content) if scrape_successful else Noneinferred_info = infer_business_info_from_url(website_url) if not scrape_successful else Nonefallback_mode = not scrape_successfulif fallback_mode:st.warning("Unable to retrieve website content. Generating recommendations based on inferred details.")messages = initial_messages.copy()st.session_state["reply"] = generate_marketing_plan(website_content if scrape_successful else "N/A",industry, goals, budget, location, inferred_info, messages, fallback=fallback_mode)st.session_state["show_notice"] = False # Remove the notice once the report is ready
app.py+31-32