Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from crewai import Agent, Task, Crew, Process
|
| 4 |
+
from langchain_openai import ChatOpenAI
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from crewai_tools import SerperDevTool
|
| 7 |
+
|
| 8 |
+
# Load environment variables from .env file
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
# Set up Streamlit page configuration
|
| 12 |
+
st.set_page_config(page_title="SEO Marketing Analysis", layout="wide")
|
| 13 |
+
|
| 14 |
+
# Streamlit UI
|
| 15 |
+
st.title("SEO Marketing Analysis")
|
| 16 |
+
|
| 17 |
+
# Get the API keys from environment variables
|
| 18 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 19 |
+
serper_api_key = os.getenv("SERPER_API_KEY")
|
| 20 |
+
|
| 21 |
+
if openai_api_key and serper_api_key:
|
| 22 |
+
# Create a ChatOpenAI instance with GPT-4
|
| 23 |
+
gpt4_model = ChatOpenAI(model_name="gpt-4")
|
| 24 |
+
|
| 25 |
+
# Create a SerperDevTool instance
|
| 26 |
+
search_tool = SerperDevTool()
|
| 27 |
+
|
| 28 |
+
# Define agents
|
| 29 |
+
seo_researcher = Agent(
|
| 30 |
+
role='SEO Research Analyst',
|
| 31 |
+
goal='Uncover the latest SEO trends and strategies',
|
| 32 |
+
backstory="""You are a seasoned SEO analyst with years of experience.
|
| 33 |
+
Your expertise lies in identifying emerging trends and effective strategies in search engine optimization.""",
|
| 34 |
+
verbose=True,
|
| 35 |
+
allow_delegation=False,
|
| 36 |
+
tools=[search_tool],
|
| 37 |
+
llm=gpt4_model
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
content_strategist = Agent(
|
| 41 |
+
role='Content Strategist',
|
| 42 |
+
goal='Develop effective SEO content strategies',
|
| 43 |
+
backstory="""You are a skilled content strategist known for your ability to create
|
| 44 |
+
SEO-friendly content that ranks well and engages readers.""",
|
| 45 |
+
verbose=True,
|
| 46 |
+
allow_delegation=False,
|
| 47 |
+
tools=[search_tool],
|
| 48 |
+
llm=gpt4_model
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Create tasks
|
| 52 |
+
research_task = Task(
|
| 53 |
+
description="""Conduct comprehensive research on the current SEO trends and strategies.
|
| 54 |
+
Identify the top 5 most effective SEO techniques for 2024.
|
| 55 |
+
Provide a brief overview of each technique and why it's considered effective.""",
|
| 56 |
+
expected_output="A detailed report listing 5 top SEO techniques with explanations of their effectiveness",
|
| 57 |
+
agent=seo_researcher
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
strategy_task = Task(
|
| 61 |
+
description="""Using the research provided, develop a comprehensive SEO content strategy.
|
| 62 |
+
The strategy should include content types, keyword strategies, and content distribution methods.
|
| 63 |
+
Provide practical examples and implementation tips.""",
|
| 64 |
+
expected_output="A well-structured SEO content strategy report of at least 4 paragraphs",
|
| 65 |
+
agent=content_strategist
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Instantiate crew
|
| 69 |
+
crew = Crew(
|
| 70 |
+
agents=[seo_researcher, content_strategist],
|
| 71 |
+
tasks=[research_task, strategy_task],
|
| 72 |
+
verbose=True,
|
| 73 |
+
process=Process.sequential
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# Button to start the analysis
|
| 77 |
+
if st.button("Start SEO Analysis"):
|
| 78 |
+
with st.spinner("Analyzing SEO trends and strategies..."):
|
| 79 |
+
result = crew.kickoff()
|
| 80 |
+
|
| 81 |
+
st.success("Analysis complete!")
|
| 82 |
+
|
| 83 |
+
# Display the result
|
| 84 |
+
st.markdown("## SEO Analysis Result")
|
| 85 |
+
st.markdown(str(result))
|
| 86 |
+
|
| 87 |
+
# Add a download button for the full report
|
| 88 |
+
st.download_button(
|
| 89 |
+
label="Download Full Report",
|
| 90 |
+
data=str(result),
|
| 91 |
+
file_name="seo_analysis_report.txt",
|
| 92 |
+
mime="text/plain"
|
| 93 |
+
)
|
| 94 |
+
else:
|
| 95 |
+
st.error("API keys not found in .env file. Please add OPENAI_API_KEY and SERPER_API_KEY and restart the app.")
|
| 96 |
+
|
| 97 |
+
# Instructions for running the Streamlit app
|
| 98 |
+
st.sidebar.header("How to Run")
|
| 99 |
+
st.sidebar.info(
|
| 100 |
+
"1. Ensure your OpenAI and Serper API Keys are in the .env file.\n"
|
| 101 |
+
"2. Click the 'Start SEO Analysis' button to begin the analysis.\n"
|
| 102 |
+
"3. Wait for the analysis to complete (this may take a few minutes).\n"
|
| 103 |
+
"4. View the results in the main panel.\n"
|
| 104 |
+
"5. Download the full report if desired."
|
| 105 |
+
)
|