CoolFace
Apppublic

Prathamesh1420/crewaigent

sourceHugging Faceupdated 2y agoView on Hugging Face
2likes
app.py108 linesDownload Raw Back to root
1import streamlit as st2from crewai import Agent, Task, Crew3from langchain_google_genai import ChatGoogleGenerativeAI4from dotenv import load_dotenv5import os6import nest_asyncio7 8# Load environment variables9load_dotenv()10 11# Apply nest_asyncio to allow nested event loops12nest_asyncio.apply()13 14# Initialize Google Gemini AI15llm = ChatGoogleGenerativeAI(16    api_key=os.getenv('GOOGLE_API_KEY'),17    model="models/gemini-pro"  # Replace with the correct model name18)19 20# Define Agents21planner = Agent(22    role="Content Planner",23    goal="Plan engaging and factually accurate content on {topic}",24    backstory="You're working on planning a blog article about the topic: {topic}. You collect information that helps the audience learn something and make informed decisions. Your work is the basis for the Content Writer to write an article on this topic.",25    llm=llm,26    allow_delegation=False,27    verbose=True28)29 30writer = Agent(31    role="Content Writer",32    goal="Write a compelling and well-structured blog post on {topic}.",33    backstory="You're a writer who uses the content plan to create a detailed blog post. Ensure it aligns with SEO best practices and the brand's voice.",34    llm=llm,35    allow_delegation=False,36    verbose=True37)38 39editor = Agent(40    role="Editor",41    goal="Edit a given blog post to align with the writing style of the organization.",42    backstory="You are an editor who receives a blog post from the Content Writer. Your goal is to review the blog post to ensure that it follows journalistic best practices, provides balanced viewpoints when providing opinions or assertions, and also avoids major controversial topics or opinions when possible.",43    llm=llm,44    allow_delegation=False,45    verbose=True46)47 48# Define Tasks49plan = Task(50    description=(51        "1. Prioritize the latest trends, key players, and noteworthy news on {topic}.\n"52        "2. Identify the target audience, considering their interests and pain points.\n"53        "3. Develop a detailed content outline including an introduction, key points, and a call to action.\n"54        "4. Include SEO keywords and relevant data or sources.\n"55        "5. The plan should be detailed and cover all aspects of the topic."56    ),57    expected_output="A comprehensive content plan document with an outline, audience analysis, SEO keywords, and resources.",58    agent=planner,59)60 61write = Task(62    description=(63        "1. Use the content plan to craft a compelling blog post on {topic}.\n"64        "2. Incorporate SEO keywords naturally.\n"65        "3. Sections/Subtitles are properly named in an engaging manner.\n"66        "4. Ensure the post is structured with an engaging introduction, insightful body, and a summarizing conclusion.\n"67        "5. Proofread for grammatical errors and alignment with the brand's voice.\n"68        "6. Each section should have 2 or 3 paragraphs.\n"69        "7. The entire blog post should be at least 1000 words."70    ),71    expected_output="A well-written blog post in markdown format, ready for publication.",72    agent=writer,73)74 75edit = Task(76    description=("Proofread the given blog post for grammatical errors and alignment with the brand's voice."),77    expected_output="A polished and error-free blog post in markdown format, ready for publication.",78    agent=editor79)80 81# Define Crew82crew = Crew(83    agents=[planner, writer, editor],84    tasks=[plan, write, edit],85    verbose=286)87 88# Streamlit App89def main():90    st.title("Content Creation Assistant")91 92    # Input for topic93    topic = st.text_input("Enter the topic for the blog post:")94 95    if st.button("Generate Content"):96        if topic:97            with st.spinner('Generating content...'):98                try:99                    result = crew.kickoff(inputs={"topic": topic})100                    st.markdown(result)101                except Exception as e:102                    st.error(f"An error occurred: {e}")103        else:104            st.error("Please enter a topic.")105 106if __name__ == "__main__":107    main()108