Spaces:
Sleeping
Sleeping
File size: 6,074 Bytes
aa7d404 9ab509e aa7d404 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
import os
import streamlit as st
from crewai import Agent, Task, Crew
from langchain_openai import ChatOpenAI # Importing OpenAI-compatible LLM
# Streamlit App Configuration
st.set_page_config(
page_title="CrewAI + Together AI Content Generator",
page_icon="π€",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for Styling
st.markdown("""
<style>
/* General Styling */
body {
font-family: 'Helvetica', sans-serif;
}
.stButton > button {
background-color: #4CAF50;
color: white;
border-radius: 8px;
padding: 10px 20px;
font-size: 16px;
}
.stButton > button:hover {
background-color: #45a049;
}
.header {
font-size: 36px;
font-weight: bold;
color: #333;
text-align: center;
margin-bottom: 20px;
}
.subheader {
font-size: 24px;
font-weight: bold;
color: #555;
margin-bottom: 15px;
}
.output-box {
background-color: #f9f9f9;
border: 1px solid #ddd;
border-radius: 8px;
padding: 20px;
margin-top: 20px;
font-size: 16px;
line-height: 1.6;
}
</style>
""", unsafe_allow_html=True)
# Header
st.markdown('<div class="header">π€ CrewAI + Together AI Content Generator</div>', unsafe_allow_html=True)
st.markdown("Generate high-quality content using CrewAI and Together AI's Llama 3.3 model.")
# Sidebar for API Key Input
st.sidebar.markdown("### π οΈ Settings")
TOGETHER_API_KEY = st.sidebar.text_input("Enter Together AI API Key", type="password")
if not TOGETHER_API_KEY:
st.sidebar.warning("Please provide your Together AI API Key to proceed.")
else:
os.environ["TOGETHER_API_KEY"] = TOGETHER_API_KEY
# Main Content
st.markdown('<div class="subheader">π Enter a Topic</div>', unsafe_allow_html=True)
topic = st.text_input("Topic:", value="Artificial Intelligence", placeholder="e.g., Artificial Intelligence, Blockchain, etc.")
if st.button("Generate Content"):
if not TOGETHER_API_KEY:
st.error("Together AI API Key is missing. Please enter it in the sidebar.")
elif not topic.strip():
st.error("Please enter a valid topic.")
else:
# Configure the LLM to use Together AI's Llama 3.3
llm = ChatOpenAI(
openai_api_base="https://api.together.xyz/v1",
openai_api_key=os.getenv("TOGETHER_API_KEY"),
model_name="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free"
)
# Define Agents
planner = Agent(
role="Content Planner",
goal="Plan engaging and factually accurate content on {topic}",
backstory="You're working on planning a blog article...",
allow_delegation=False,
verbose=True,
llm=llm # Use Together AI for this agent
)
writer = Agent(
role="Content Writer",
goal="Write insightful and factually accurate opinion piece about the topic: {topic}",
backstory="You're working on writing a new opinion piece...",
allow_delegation=False,
verbose=True,
llm=llm # Use Together AI for this agent
)
editor = Agent(
role="Editor",
goal="Edit a given blog post to align with the writing style of the organization.",
backstory="You are an editor who receives a blog post...",
allow_delegation=False,
verbose=True,
llm=llm # Use Together AI for this agent
)
# Define Tasks
plan = Task(
description=(
"1. Prioritize the latest trends, key players, "
"and noteworthy news on {topic}.\n"
"2. Identify the target audience, considering "
"their interests and pain points.\n"
"3. Develop a detailed content outline including "
"an introduction, key points, and a call to action.\n"
"4. Include SEO keywords and relevant data or sources."
),
expected_output="A comprehensive content plan document "
"with an outline, audience analysis, "
"SEO keywords, and resources.",
agent=planner,
)
write = Task(
description=(
"1. Use the content plan to craft a compelling "
"blog post on {topic}.\n"
"2. Incorporate SEO keywords naturally.\n"
"3. Sections/Subtitles are properly named "
"in an engaging manner.\n"
"4. Ensure the post is structured with an "
"engaging introduction, insightful body, "
"and a summarizing conclusion.\n"
"5. Proofread for grammatical errors and "
"alignment with the brand's voice.\n"
),
expected_output="A well-written blog post "
"in markdown format, ready for publication, "
"each section should have 2 or 3 paragraphs.",
agent=writer,
)
edit = Task(
description=("Proofread the given blog post for "
"grammatical errors and "
"alignment with the brand's voice."),
expected_output="A well-written blog post in markdown format, "
"ready for publication, "
"each section should have 2 or 3 paragraphs.",
agent=editor
)
# Create Crew
crew = Crew(
agents=[planner, writer, editor],
tasks=[plan, write, edit],
verbose=True
)
# Generate Content with Progress Indicator
with st.spinner("Generating content... This may take a few moments."):
result = crew.kickoff(inputs={"topic": topic})
# Display Output
st.markdown('<div class="output-box">', unsafe_allow_html=True)
st.markdown("### β¨ Generated Content")
st.markdown(result, unsafe_allow_html=False)
st.markdown('</div>', unsafe_allow_html=True) |