ysharma HF staff commited on
Commit
ce25288
Β·
verified Β·
1 Parent(s): 8bbbd7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -36
app.py CHANGED
@@ -9,12 +9,12 @@ import os
9
  from dotenv import load_dotenv
10
  import threading
11
 
12
- #load_dotenv()
13
 
14
  class OutputParser:
15
  def __init__(self):
16
  self.buffer = ""
17
  self.current_agent = None
 
18
 
19
  def parse_output(self, text: str) -> List[ChatMessage]:
20
  messages = []
@@ -31,7 +31,7 @@ class OutputParser:
31
  metadata={"title": f"πŸ€– {self.current_agent}"}
32
  ))
33
 
34
- # Look for task information with full task list
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()
@@ -41,43 +41,31 @@ class OutputParser:
41
  metadata={"title": f"πŸ“‹ Task for {self.current_agent}"}
42
  ))
43
 
44
- # Look for thought processes
45
- thought_match = re.search(r'Thought: (.*?)(?=\nAction:|$)', cleaned_text, re.DOTALL)
46
- if thought_match and self.current_agent:
47
- thought_content = thought_match.group(1).strip()
48
- messages.append(ChatMessage(
49
- role="assistant",
50
- content=thought_content,
51
- metadata={"title": f"πŸ’­ {self.current_agent}'s Thoughts"}
52
- ))
53
-
54
- # Look for final answers from non-Editor agents
55
- if "Final Answer:" in cleaned_text and self.current_agent != "Editor":
56
  answer_match = re.search(r'Final Answer:\s*(.*?)(?=\n> Finished chain|$)', cleaned_text, re.DOTALL)
57
  if answer_match:
58
  answer_content = answer_match.group(1).strip()
59
- messages.append(ChatMessage(
60
- role="assistant",
61
- content=answer_content,
62
- metadata={"title": f"πŸ’‘ Output from {self.current_agent}"}
63
- ))
64
-
65
- # Special handling for Editor's final answer (the final article)
66
- elif "Final Answer:" in cleaned_text and self.current_agent == "Editor":
67
- answer_match = re.search(r'Final Answer:\s*(.*?)(?=\n> Finished chain|$)', cleaned_text, re.DOTALL)
68
- if answer_match:
69
- answer_content = answer_match.group(1).strip()
70
- # First send the metadata marker
71
- messages.append(ChatMessage(
72
- role="assistant",
73
- content="Final article is ready!",
74
- metadata={"title": "πŸ“ Final Article"}
75
- ))
76
- # Then send the actual content without metadata
77
- messages.append(ChatMessage(
78
- role="assistant",
79
- content=answer_content
80
- ))
81
 
82
  return messages
83
 
 
9
  from dotenv import load_dotenv
10
  import threading
11
 
 
12
 
13
  class OutputParser:
14
  def __init__(self):
15
  self.buffer = ""
16
  self.current_agent = None
17
+ self.final_article_sent = False # Add this flag
18
 
19
  def parse_output(self, text: str) -> List[ChatMessage]:
20
  messages = []
 
31
  metadata={"title": f"πŸ€– {self.current_agent}"}
32
  ))
33
 
34
+ # Look for task information
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()
 
41
  metadata={"title": f"πŸ“‹ Task for {self.current_agent}"}
42
  ))
43
 
44
+ # Look for final answers from all agents
45
+ if "Final Answer:" in cleaned_text:
 
 
 
 
 
 
 
 
 
 
46
  answer_match = re.search(r'Final Answer:\s*(.*?)(?=\n> Finished chain|$)', cleaned_text, re.DOTALL)
47
  if answer_match:
48
  answer_content = answer_match.group(1).strip()
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