Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
# This works even better
|
2 |
from crewai import Agent, Task, Crew
|
3 |
import gradio as gr
|
4 |
from gradio import ChatMessage
|
@@ -9,7 +8,7 @@ from typing import List, Generator
|
|
9 |
import os
|
10 |
from dotenv import load_dotenv
|
11 |
import threading
|
12 |
-
|
13 |
|
14 |
class OutputParser:
|
15 |
def __init__(self):
|
@@ -119,8 +118,19 @@ class StreamingCapture:
|
|
119 |
pass
|
120 |
|
121 |
class ArticleCrew:
|
122 |
-
def __init__(self):
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
self.planner = Agent(
|
125 |
role="Content Planner",
|
126 |
goal="Plan engaging and factually accurate content on {topic}",
|
@@ -128,7 +138,8 @@ class ArticleCrew:
|
|
128 |
"You collect information that helps the audience learn something "
|
129 |
"and make informed decisions.",
|
130 |
allow_delegation=False,
|
131 |
-
verbose=True
|
|
|
132 |
)
|
133 |
|
134 |
self.writer = Agent(
|
@@ -137,7 +148,8 @@ class ArticleCrew:
|
|
137 |
backstory="You're working on writing a new opinion piece about the topic: {topic}. "
|
138 |
"You base your writing on the work of the Content Planner.",
|
139 |
allow_delegation=False,
|
140 |
-
verbose=True
|
|
|
141 |
)
|
142 |
|
143 |
self.editor = Agent(
|
@@ -145,7 +157,8 @@ class ArticleCrew:
|
|
145 |
goal="Edit a given blog post to align with the writing style",
|
146 |
backstory="You are an editor who receives a blog post from the Content Writer.",
|
147 |
allow_delegation=False,
|
148 |
-
verbose=True
|
|
|
149 |
)
|
150 |
|
151 |
self.output_parser = OutputParser()
|
@@ -234,30 +247,40 @@ class ArticleCrew:
|
|
234 |
sys.stdout = original_stdout
|
235 |
|
236 |
def create_demo():
|
237 |
-
article_crew =
|
238 |
|
239 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
240 |
gr.Markdown("# 📝 AI Article Writing Crew")
|
241 |
-
gr.Markdown("Watch as our AI crew collaborates to create your article!")
|
242 |
-
|
|
|
|
|
|
|
|
|
|
|
243 |
chatbot = gr.Chatbot(
|
244 |
label="Writing Process",
|
245 |
avatar_images=(None, "https://avatars.githubusercontent.com/u/170677839?v=4"),
|
246 |
height=700,
|
247 |
type="messages",
|
248 |
-
show_label=True
|
|
|
249 |
)
|
250 |
-
|
251 |
|
252 |
with gr.Row(equal_height=True):
|
253 |
topic = gr.Textbox(
|
254 |
label="Article Topic",
|
255 |
placeholder="Enter the topic you want an article about...",
|
256 |
-
|
257 |
-
|
258 |
)
|
259 |
|
260 |
-
async def process_input(topic, history):
|
|
|
|
|
|
|
|
|
|
|
261 |
history.append(ChatMessage(role="user", content=f"Write an article about: {topic}"))
|
262 |
yield history
|
263 |
|
@@ -265,11 +288,25 @@ def create_demo():
|
|
265 |
history.extend(messages)
|
266 |
yield history
|
267 |
|
268 |
-
btn = gr.Button("Write Article", variant="primary", scale=1)
|
269 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
btn.click(
|
271 |
process_input,
|
272 |
-
inputs=[topic, chatbot],
|
273 |
outputs=[chatbot]
|
274 |
)
|
275 |
|
@@ -278,4 +315,4 @@ def create_demo():
|
|
278 |
if __name__ == "__main__":
|
279 |
demo = create_demo()
|
280 |
demo.queue()
|
281 |
-
demo.launch(
|
|
|
|
|
1 |
from crewai import Agent, Task, Crew
|
2 |
import gradio as gr
|
3 |
from gradio import ChatMessage
|
|
|
8 |
import os
|
9 |
from dotenv import load_dotenv
|
10 |
import threading
|
11 |
+
from langchain_openai import ChatOpenAI
|
12 |
|
13 |
class OutputParser:
|
14 |
def __init__(self):
|
|
|
118 |
pass
|
119 |
|
120 |
class ArticleCrew:
|
121 |
+
def __init__(self, api_key: str = None):
|
122 |
+
self.api_key = api_key
|
123 |
+
self.initialize_agents()
|
124 |
+
|
125 |
+
def initialize_agents(self):
|
126 |
+
# Create a ChatOpenAI instance with the API key
|
127 |
+
llm = ChatOpenAI(
|
128 |
+
openai_api_key=self.api_key,
|
129 |
+
temperature=0.7,
|
130 |
+
model="gpt-4"
|
131 |
+
)
|
132 |
+
|
133 |
+
# Initialize agents with the LLM
|
134 |
self.planner = Agent(
|
135 |
role="Content Planner",
|
136 |
goal="Plan engaging and factually accurate content on {topic}",
|
|
|
138 |
"You collect information that helps the audience learn something "
|
139 |
"and make informed decisions.",
|
140 |
allow_delegation=False,
|
141 |
+
verbose=True,
|
142 |
+
llm=llm
|
143 |
)
|
144 |
|
145 |
self.writer = Agent(
|
|
|
148 |
backstory="You're working on writing a new opinion piece about the topic: {topic}. "
|
149 |
"You base your writing on the work of the Content Planner.",
|
150 |
allow_delegation=False,
|
151 |
+
verbose=True,
|
152 |
+
llm=llm
|
153 |
)
|
154 |
|
155 |
self.editor = Agent(
|
|
|
157 |
goal="Edit a given blog post to align with the writing style",
|
158 |
backstory="You are an editor who receives a blog post from the Content Writer.",
|
159 |
allow_delegation=False,
|
160 |
+
verbose=True,
|
161 |
+
llm=llm
|
162 |
)
|
163 |
|
164 |
self.output_parser = OutputParser()
|
|
|
247 |
sys.stdout = original_stdout
|
248 |
|
249 |
def create_demo():
|
250 |
+
article_crew = None # Initialize as None
|
251 |
|
252 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
253 |
gr.Markdown("# 📝 AI Article Writing Crew")
|
254 |
+
gr.Markdown("Watch as our AI crew collaborates to create your article! This app uses CrewAI agents: Content Planner, Content Writer, and Content Editor to write Aticle on any topic of your choice. To start, enter your OpenAI API Key below and pres Enter!")
|
255 |
+
openai_api_key = gr.Textbox(
|
256 |
+
label='OpenAI API Key',
|
257 |
+
type='password',
|
258 |
+
placeholder='Type your OpenAI API key and press Enter!',
|
259 |
+
interactive=True)
|
260 |
+
|
261 |
chatbot = gr.Chatbot(
|
262 |
label="Writing Process",
|
263 |
avatar_images=(None, "https://avatars.githubusercontent.com/u/170677839?v=4"),
|
264 |
height=700,
|
265 |
type="messages",
|
266 |
+
show_label=True,
|
267 |
+
visible=False
|
268 |
)
|
|
|
269 |
|
270 |
with gr.Row(equal_height=True):
|
271 |
topic = gr.Textbox(
|
272 |
label="Article Topic",
|
273 |
placeholder="Enter the topic you want an article about...",
|
274 |
+
scale=4,
|
275 |
+
visible=False
|
276 |
)
|
277 |
|
278 |
+
async def process_input(topic, history, openai_api_key):
|
279 |
+
nonlocal article_crew
|
280 |
+
# Initialize ArticleCrew with the API key if not already initialized
|
281 |
+
if article_crew is None:
|
282 |
+
article_crew = ArticleCrew(api_key=openai_api_key)
|
283 |
+
|
284 |
history.append(ChatMessage(role="user", content=f"Write an article about: {topic}"))
|
285 |
yield history
|
286 |
|
|
|
288 |
history.extend(messages)
|
289 |
yield history
|
290 |
|
291 |
+
btn = gr.Button("Write Article", variant="primary", scale=1, visible=False)
|
292 |
+
|
293 |
+
def show_interface():
|
294 |
+
return {
|
295 |
+
openai_api_key: gr.Textbox(visible=False),
|
296 |
+
chatbot: gr.Chatbot(visible=True),
|
297 |
+
topic: gr.Textbox(visible=True),
|
298 |
+
btn: gr.Button(visible=True)
|
299 |
+
}
|
300 |
+
|
301 |
+
openai_api_key.submit(
|
302 |
+
show_interface,
|
303 |
+
None,
|
304 |
+
[openai_api_key, chatbot, topic, btn]
|
305 |
+
)
|
306 |
+
|
307 |
btn.click(
|
308 |
process_input,
|
309 |
+
inputs=[topic, chatbot, openai_api_key],
|
310 |
outputs=[chatbot]
|
311 |
)
|
312 |
|
|
|
315 |
if __name__ == "__main__":
|
316 |
demo = create_demo()
|
317 |
demo.queue()
|
318 |
+
demo.launch()
|