Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from crewai import Agent, Task, Crew
|
| 2 |
import gradio as gr
|
| 3 |
from gradio import ChatMessage
|
|
@@ -14,18 +15,48 @@ class OutputParser:
|
|
| 14 |
def __init__(self):
|
| 15 |
self.buffer = ""
|
| 16 |
self.current_agent = None
|
| 17 |
-
self.final_article_sent = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def parse_output(self, text: str) -> List[ChatMessage]:
|
| 20 |
messages = []
|
| 21 |
-
# Clean ANSI codes
|
| 22 |
cleaned_text = re.sub(r'\x1B\[[0-9;]*[mK]', '', text)
|
| 23 |
|
| 24 |
# Look for working agent declarations
|
| 25 |
agent_match = re.search(r'\[DEBUG\]: == Working Agent: (.*?)(?=\n|$)', cleaned_text)
|
| 26 |
if agent_match:
|
| 27 |
self.current_agent = agent_match.group(1)
|
| 28 |
-
|
| 29 |
role="assistant",
|
| 30 |
content=f"Starting work...",
|
| 31 |
metadata={"title": f"π€ {self.current_agent}"}
|
|
@@ -35,38 +66,45 @@ class OutputParser:
|
|
| 35 |
task_match = re.search(r'\[INFO\]: == Starting Task: (.*?)(?=\n\n|\n> Entering|$)', cleaned_text, re.DOTALL)
|
| 36 |
if task_match and self.current_agent:
|
| 37 |
task_content = task_match.group(1).strip()
|
| 38 |
-
|
| 39 |
role="assistant",
|
| 40 |
content=task_content,
|
| 41 |
metadata={"title": f"π Task for {self.current_agent}"}
|
| 42 |
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
if self.current_agent == "Editor" and not self.final_article_sent:
|
| 51 |
-
# This is the final article - send only once
|
| 52 |
-
messages.append(ChatMessage(
|
| 53 |
-
role="assistant",
|
| 54 |
-
content="Final Article",
|
| 55 |
-
metadata={"title": "π Final Article"}
|
| 56 |
-
))
|
| 57 |
-
messages.append(ChatMessage(
|
| 58 |
-
role="assistant",
|
| 59 |
-
content=answer_content
|
| 60 |
-
))
|
| 61 |
-
self.final_article_sent = True
|
| 62 |
-
elif self.current_agent != "Editor":
|
| 63 |
-
# This is an intermediate output (Planner or Writer)
|
| 64 |
-
messages.append(ChatMessage(
|
| 65 |
-
role="assistant",
|
| 66 |
-
content=answer_content,
|
| 67 |
-
metadata={"title": f"π‘ Output from {self.current_agent}"}
|
| 68 |
-
))
|
| 69 |
-
|
| 70 |
return messages
|
| 71 |
|
| 72 |
class StreamingCapture:
|
|
@@ -236,4 +274,4 @@ def create_demo():
|
|
| 236 |
if __name__ == "__main__":
|
| 237 |
demo = create_demo()
|
| 238 |
demo.queue()
|
| 239 |
-
demo.launch(debug=True, share=True)
|
|
|
|
| 1 |
+
# This works even better
|
| 2 |
from crewai import Agent, Task, Crew
|
| 3 |
import gradio as gr
|
| 4 |
from gradio import ChatMessage
|
|
|
|
| 15 |
def __init__(self):
|
| 16 |
self.buffer = ""
|
| 17 |
self.current_agent = None
|
| 18 |
+
self.final_article_sent = False
|
| 19 |
+
self.message_queue = {
|
| 20 |
+
"Content Planner": [],
|
| 21 |
+
"Content Writer": [],
|
| 22 |
+
"Editor": []
|
| 23 |
+
}
|
| 24 |
+
self.agent_sequence = ["Content Planner", "Content Writer", "Editor"]
|
| 25 |
|
| 26 |
+
def format_output(self, raw_content: str, agent_name: str) -> str:
|
| 27 |
+
"""Format the output content based on agent type."""
|
| 28 |
+
if agent_name == "Content Planner":
|
| 29 |
+
# Clean up the planner's output to make it more readable
|
| 30 |
+
lines = raw_content.split('\n')
|
| 31 |
+
formatted_lines = []
|
| 32 |
+
for line in lines:
|
| 33 |
+
# Remove number prefixes and clean up
|
| 34 |
+
line = re.sub(r'^\d+\.\s*', '', line.strip())
|
| 35 |
+
# Make text size normal by removing markdown formatting
|
| 36 |
+
line = re.sub(r'^#+\s*', '', line)
|
| 37 |
+
if line:
|
| 38 |
+
formatted_lines.append(line)
|
| 39 |
+
return '\n\n'.join(formatted_lines)
|
| 40 |
+
|
| 41 |
+
elif agent_name == "Content Writer":
|
| 42 |
+
# Clean up writer's output to make it more readable
|
| 43 |
+
# Remove markdown headers but keep the text
|
| 44 |
+
content = re.sub(r'^#+\s*(.+)$', r'\1', raw_content, flags=re.MULTILINE)
|
| 45 |
+
# Remove multiple newlines
|
| 46 |
+
content = re.sub(r'\n{3,}', '\n\n', content)
|
| 47 |
+
return content.strip()
|
| 48 |
+
|
| 49 |
+
return raw_content.strip()
|
| 50 |
+
|
| 51 |
def parse_output(self, text: str) -> List[ChatMessage]:
|
| 52 |
messages = []
|
|
|
|
| 53 |
cleaned_text = re.sub(r'\x1B\[[0-9;]*[mK]', '', text)
|
| 54 |
|
| 55 |
# Look for working agent declarations
|
| 56 |
agent_match = re.search(r'\[DEBUG\]: == Working Agent: (.*?)(?=\n|$)', cleaned_text)
|
| 57 |
if agent_match:
|
| 58 |
self.current_agent = agent_match.group(1)
|
| 59 |
+
self.message_queue[self.current_agent].append(ChatMessage(
|
| 60 |
role="assistant",
|
| 61 |
content=f"Starting work...",
|
| 62 |
metadata={"title": f"π€ {self.current_agent}"}
|
|
|
|
| 66 |
task_match = re.search(r'\[INFO\]: == Starting Task: (.*?)(?=\n\n|\n> Entering|$)', cleaned_text, re.DOTALL)
|
| 67 |
if task_match and self.current_agent:
|
| 68 |
task_content = task_match.group(1).strip()
|
| 69 |
+
self.message_queue[self.current_agent].append(ChatMessage(
|
| 70 |
role="assistant",
|
| 71 |
content=task_content,
|
| 72 |
metadata={"title": f"π Task for {self.current_agent}"}
|
| 73 |
))
|
| 74 |
+
|
| 75 |
+
# Look for agent outputs in debug messages
|
| 76 |
+
debug_match = re.search(r'\[DEBUG\]: == \[(.*?)\] Task output: (.*?)(?=\[DEBUG\]|$)', cleaned_text, re.DOTALL)
|
| 77 |
+
if debug_match:
|
| 78 |
+
agent_name = debug_match.group(1)
|
| 79 |
+
output_content = debug_match.group(2).strip()
|
| 80 |
+
|
| 81 |
+
# Format the output content
|
| 82 |
+
formatted_content = self.format_output(output_content, agent_name)
|
| 83 |
+
|
| 84 |
+
if agent_name == "Editor" and not self.final_article_sent:
|
| 85 |
+
self.message_queue[agent_name].append(ChatMessage(
|
| 86 |
+
role="assistant",
|
| 87 |
+
content="Final article is ready!",
|
| 88 |
+
metadata={"title": "π Final Article"}
|
| 89 |
+
))
|
| 90 |
+
self.message_queue[agent_name].append(ChatMessage(
|
| 91 |
+
role="assistant",
|
| 92 |
+
content=formatted_content
|
| 93 |
+
))
|
| 94 |
+
self.final_article_sent = True
|
| 95 |
+
elif agent_name != "Editor":
|
| 96 |
+
self.message_queue[agent_name].append(ChatMessage(
|
| 97 |
+
role="assistant",
|
| 98 |
+
content=formatted_content,
|
| 99 |
+
metadata={"title": f"π‘ Output from {agent_name}"}
|
| 100 |
+
))
|
| 101 |
|
| 102 |
+
# Return messages in the correct sequence
|
| 103 |
+
for agent in self.agent_sequence:
|
| 104 |
+
if self.message_queue[agent]:
|
| 105 |
+
messages.extend(self.message_queue[agent])
|
| 106 |
+
self.message_queue[agent] = []
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
return messages
|
| 109 |
|
| 110 |
class StreamingCapture:
|
|
|
|
| 274 |
if __name__ == "__main__":
|
| 275 |
demo = create_demo()
|
| 276 |
demo.queue()
|
| 277 |
+
demo.launch(debug=True, share=True)
|