File size: 9,149 Bytes
c3fdfdd
 
 
 
 
 
 
 
 
f9bf036
c3fdfdd
 
f9bf036
 
 
 
ce25288
f9bf036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce25288
f9bf036
 
 
 
 
 
 
 
 
ce25288
 
f9bf036
 
 
ce25288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9bf036
 
 
 
 
 
 
 
 
 
 
 
 
 
c3fdfdd
 
f9bf036
c3fdfdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9bf036
c3fdfdd
f9bf036
c3fdfdd
 
f9bf036
 
c3fdfdd
f9bf036
 
 
 
 
 
c3fdfdd
f9bf036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3fdfdd
 
 
 
 
 
 
 
 
 
 
 
 
8bbbd7a
c3fdfdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9bf036
c3fdfdd
 
 
 
 
 
 
 
 
 
f9bf036
c3fdfdd
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from crewai import Agent, Task, Crew
import gradio as gr
from gradio import ChatMessage
import asyncio
import re
import sys
from typing import List, Generator
import os
from dotenv import load_dotenv
import threading


class OutputParser:
    def __init__(self):
        self.buffer = ""
        self.current_agent = None
        self.final_article_sent = False  # Add this flag
        
    def parse_output(self, text: str) -> List[ChatMessage]:
        messages = []
        # Clean ANSI codes
        cleaned_text = re.sub(r'\x1B\[[0-9;]*[mK]', '', text)
        
        # Look for working agent declarations
        agent_match = re.search(r'\[DEBUG\]: == Working Agent: (.*?)(?=\n|$)', cleaned_text)
        if agent_match:
            self.current_agent = agent_match.group(1)
            messages.append(ChatMessage(
                role="assistant",
                content=f"Starting work...",
                metadata={"title": f"πŸ€– {self.current_agent}"}
            ))
        
        # Look for task information
        task_match = re.search(r'\[INFO\]: == Starting Task: (.*?)(?=\n\n|\n> Entering|$)', cleaned_text, re.DOTALL)
        if task_match and self.current_agent:
            task_content = task_match.group(1).strip()
            messages.append(ChatMessage(
                role="assistant",
                content=task_content,
                metadata={"title": f"πŸ“‹ Task for {self.current_agent}"}
            ))
        
        # Look for final answers from all agents
        if "Final Answer:" in cleaned_text:
            answer_match = re.search(r'Final Answer:\s*(.*?)(?=\n> Finished chain|$)', cleaned_text, re.DOTALL)
            if answer_match:
                answer_content = answer_match.group(1).strip()
                
                if self.current_agent == "Editor" and not self.final_article_sent:
                    # This is the final article - send only once
                    messages.append(ChatMessage(
                        role="assistant",
                        content="Final Article",
                        metadata={"title": "πŸ“ Final Article"}
                    ))
                    messages.append(ChatMessage(
                        role="assistant",
                        content=answer_content
                    ))
                    self.final_article_sent = True
                elif self.current_agent != "Editor":
                    # This is an intermediate output (Planner or Writer)
                    messages.append(ChatMessage(
                        role="assistant",
                        content=answer_content,
                        metadata={"title": f"πŸ’‘ Output from {self.current_agent}"}
                    ))
        
        return messages

class StreamingCapture:
    def __init__(self):
        self.buffer = ""
    
    def write(self, text):
        self.buffer += text
        return len(text)
    
    def flush(self):
        pass

class ArticleCrew:
    def __init__(self):
        # Initialize agents
        self.planner = Agent(
            role="Content Planner",
            goal="Plan engaging and factually accurate content on {topic}",
            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.",
            allow_delegation=False,
            verbose=True
        )
        
        self.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 about the topic: {topic}. "
                     "You base your writing on the work of the Content Planner.",
            allow_delegation=False,
            verbose=True
        )
        
        self.editor = Agent(
            role="Editor",
            goal="Edit a given blog post to align with the writing style",
            backstory="You are an editor who receives a blog post from the Content Writer.",
            allow_delegation=False,
            verbose=True
        )
        
        self.output_parser = OutputParser()

    def create_tasks(self, topic: str):
        plan_task = Task(
            description=(
                f"1. Prioritize the latest trends, key players, and noteworthy news on {topic}.\n"
                f"2. Identify the target audience, considering their interests and pain points.\n"
                f"3. Develop a detailed content outline including introduction, key points, and call to action.\n"
                f"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=self.planner
        )

        write_task = Task(
            description=(
                "1. Use the content plan to craft a compelling blog post.\n"
                "2. Incorporate SEO keywords naturally.\n"
                "3. Sections/Subtitles are properly named in an engaging manner.\n"
                "4. Ensure proper structure with introduction, body, and conclusion.\n"
                "5. Proofread for grammatical errors."
            ),
            expected_output="A well-written blog post in markdown format, ready for publication.",
            agent=self.writer
        )

        edit_task = 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.",
            agent=self.editor
        )

        return [plan_task, write_task, edit_task]

    async def process_article(self, topic: str) -> Generator[List[ChatMessage], None, None]:
        crew = Crew(
            agents=[self.planner, self.writer, self.editor],
            tasks=self.create_tasks(topic),
            verbose=2
        )

        capture = StreamingCapture()
        original_stdout = sys.stdout
        sys.stdout = capture
        
        try:
            # Start the crew task in a separate thread to not block streaming
            result_container = []
            
            def run_crew():
                try:
                    result = crew.kickoff(inputs={"topic": topic})
                    result_container.append(result)
                except Exception as e:
                    result_container.append(e)
            
            thread = threading.Thread(target=run_crew)
            thread.start()
            
            # Stream output while the crew is working
            last_processed = 0
            while thread.is_alive() or last_processed < len(capture.buffer):
                if len(capture.buffer) > last_processed:
                    new_content = capture.buffer[last_processed:]
                    messages = self.output_parser.parse_output(new_content)
                    if messages:
                        for msg in messages:
                            yield [msg]
                    last_processed = len(capture.buffer)
                await asyncio.sleep(0.1)
            
            # Check if we got a result or an error
            if result_container and not isinstance(result_container[0], Exception):
                # Final messages already sent by the parser
                pass
            else:
                yield [ChatMessage(
                    role="assistant",
                    content="An error occurred while generating the article.",
                    metadata={"title": "❌ Error"}
                )]
        
        finally:
            sys.stdout = original_stdout

def create_demo():
    article_crew = ArticleCrew()
    
    with gr.Blocks(theme=gr.themes.Soft()) as demo:
        gr.Markdown("# πŸ“ AI Article Writing Crew")
        gr.Markdown("Watch as our AI crew collaborates to create your article!")
        
        chatbot = gr.Chatbot(
            label="Writing Process",
            avatar_images=(None, "https://avatars.githubusercontent.com/u/170677839?v=4"),
            height=700,
            type="messages",
            show_label=True
        )
        
        topic = gr.Textbox(
            label="Article Topic",
            placeholder="Enter the topic you want an article about...",
            lines=2
        )
        
        async def process_input(topic, history):
            history.append(ChatMessage(role="user", content=f"Write an article about: {topic}"))
            yield history
            
            async for messages in article_crew.process_article(topic):
                history.extend(messages)
                yield history
        
        btn = gr.Button("Write Article", variant="primary")
        btn.click(
            process_input,
            inputs=[topic, chatbot],
            outputs=[chatbot]
        )

    return demo

if __name__ == "__main__":
    demo = create_demo()
    demo.queue()
    demo.launch(debug=True, share=True)