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