-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgraph.py
More file actions
255 lines (211 loc) · 8.89 KB
/
Copy pathgraph.py
File metadata and controls
255 lines (211 loc) · 8.89 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from langgraph.graph import StateGraph, END
from rich.console import Console
from .state import MERRState
console = Console()
def route_by_processing_type(state: MERRState) -> str:
"""Routes flow based on the user's chosen processing type."""
proc_type = state["processing_type"]
if state.get("verbose", True):
console.log(f"Routing based on processing type: [yellow]{proc_type}[/yellow]")
if state.get("error"):
return "handle_error"
if proc_type == "AU":
return "au_pipeline"
if proc_type == "audio":
return "audio_pipeline"
if proc_type == "video":
return "video_pipeline"
if proc_type == "MER":
return "full_pipeline"
if proc_type == "image":
return "image_pipeline"
return "handle_error"
def route_after_emotion_filter(state: MERRState) -> str:
"""Routes flow after emotion filtering. Both AU and MER find peak info."""
if state.get("error"):
return "handle_error"
proc_type = state["processing_type"]
if proc_type in ["AU", "MER"]:
return "find_overall_peak_au"
return "handle_error"
def route_after_peak_au(state: MERRState) -> str:
"""Routes based on pipeline after finding peak AUs."""
if state.get("error"):
return "handle_error"
proc_type = state["processing_type"]
if proc_type == "AU":
return "save_au_results"
elif proc_type == "MER":
return "extract_peak_image"
return "handle_error"
def route_after_audio_generation(state: MERRState) -> str:
"""Routes flow after audio description generation based on the pipeline."""
if state.get("error"):
return "handle_error"
proc_type = state["processing_type"]
if proc_type == "audio":
return "save_audio_results"
elif proc_type == "MER":
return "generate_video_description"
return "handle_error"
def route_after_video_generation(state: MERRState) -> str:
"""Routes flow after video description generation based on the pipeline."""
if state.get("error"):
return "handle_error"
proc_type = state["processing_type"]
if proc_type == "video":
return "save_video_results"
elif proc_type == "MER":
return "generate_peak_frame_visual_description"
return "handle_error"
def route_gate_agent(state: MERRState) -> str:
"""Routes flow based on the Gate Agent's decision."""
if state.get("error"):
return "handle_error"
decision = state.get("gate_decision", "pass")
if decision == "pass":
return "synthesize_summary"
retry_target = state.get("retry_target")
if retry_target:
if state.get("verbose", True):
console.log(f"[yellow]Gate Agent triggering retry at: {retry_target}[/yellow]")
return retry_target
return "synthesize_summary" # Fallback
def create_graph(use_sync_nodes: bool = False, use_gate_agent: bool = False):
"""
Creates and compiles the modular MERR construction graph.
It can create either an asynchronous or a synchronous graph based on the flag.
"""
if use_sync_nodes:
console.log(
"Creating a [bold yellow]synchronous[/bold yellow] graph for Hugging Face model."
)
from .nodes import sync_nodes as nodes
else:
console.log("Creating an [bold green]asynchronous[/bold green] graph.")
from .nodes import async_nodes as nodes
if use_gate_agent:
from .nodes.gate_agent import GateAgent
workflow = StateGraph(MERRState)
# --- Node Definitions ---
workflow.add_node("setup_paths", nodes.setup_paths)
workflow.add_node("handle_error", nodes.handle_error)
# Shared nodes for AU and MER pipelines
workflow.add_node("run_au_extraction", nodes.run_au_extraction)
workflow.add_node("filter_by_emotion", nodes.filter_by_emotion)
workflow.add_node("find_overall_peak_au", nodes.find_overall_peak_au)
# AU Pipeline
workflow.add_node("save_au_results", nodes.save_au_results)
# Audio Pipeline
workflow.add_node("generate_audio_description", nodes.generate_audio_description)
workflow.add_node("save_audio_results", nodes.save_audio_results)
# Video Pipeline
workflow.add_node("generate_video_description", nodes.generate_video_description)
workflow.add_node("save_video_results", nodes.save_video_results)
# MER Pipeline
workflow.add_node("extract_full_features", nodes.extract_full_features)
workflow.add_node("extract_peak_image", nodes.extract_peak_image)
workflow.add_node(
"generate_peak_frame_visual_description",
nodes.generate_peak_frame_visual_description,
)
if use_gate_agent and not use_sync_nodes:
gate_agent_node = GateAgent()
workflow.add_node("gate_agent", gate_agent_node.run)
workflow.add_node("synthesize_summary", nodes.synthesize_summary)
workflow.add_node("save_mer_results", nodes.save_mer_results)
# Image Pipeline
workflow.add_node("run_image_analysis", nodes.run_image_analysis)
workflow.add_node("synthesize_image_summary", nodes.synthesize_image_summary)
workflow.add_node("save_image_results", nodes.save_image_results)
# --- Define Graph Structure ---
workflow.set_entry_point("setup_paths")
# 1. Main router from setup to pipeline entry points
workflow.add_conditional_edges(
"setup_paths",
route_by_processing_type,
{
"au_pipeline": "run_au_extraction",
"audio_pipeline": "generate_audio_description",
"video_pipeline": "generate_video_description",
"full_pipeline": "extract_full_features",
"image_pipeline": "run_image_analysis",
"handle_error": "handle_error",
},
)
# 2. Define shared paths and routers
# Both AU and MER pipelines run emotion filtering.
workflow.add_edge("run_au_extraction", "filter_by_emotion")
workflow.add_edge("extract_full_features", "filter_by_emotion")
# After emotion filtering, both pipelines find the overall peak AUs.
workflow.add_conditional_edges(
"filter_by_emotion",
route_after_emotion_filter,
{
"find_overall_peak_au": "find_overall_peak_au",
"handle_error": "handle_error",
},
)
# After finding peak AUs, route to save (AU) or more processing (MER).
workflow.add_conditional_edges(
"find_overall_peak_au",
route_after_peak_au,
{
"save_au_results": "save_au_results",
"extract_peak_image": "extract_peak_image",
"handle_error": "handle_error",
},
)
# After audio generation, route based on the original pipeline choice.
workflow.add_conditional_edges(
"generate_audio_description",
route_after_audio_generation,
{
"save_audio_results": "save_audio_results",
"generate_video_description": "generate_video_description",
"handle_error": "handle_error",
},
)
# After video generation, route based on the original pipeline choice.
workflow.add_conditional_edges(
"generate_video_description",
route_after_video_generation,
{
"save_video_results": "save_video_results",
"generate_peak_frame_visual_description": "generate_peak_frame_visual_description",
"handle_error": "handle_error",
},
)
# 3. Define MER pipeline sequence
workflow.add_edge("extract_peak_image", "generate_audio_description")
if use_gate_agent and not use_sync_nodes:
workflow.add_edge("generate_peak_frame_visual_description", "gate_agent")
# Gate Agent routing
workflow.add_conditional_edges(
"gate_agent",
route_gate_agent,
{
"synthesize_summary": "synthesize_summary",
"generate_audio_description": "generate_audio_description",
"generate_video_description": "generate_video_description",
"generate_peak_frame_visual_description": "generate_peak_frame_visual_description",
"handle_error": "handle_error",
},
)
else:
workflow.add_edge("generate_peak_frame_visual_description", "synthesize_summary")
workflow.add_edge("synthesize_summary", "save_mer_results")
# 4. Define Image pipeline sequence
workflow.add_edge("run_image_analysis", "synthesize_image_summary")
workflow.add_edge("synthesize_image_summary", "save_image_results")
# 5. Define terminal nodes for all pipelines
workflow.add_edge("save_au_results", END)
workflow.add_edge("save_audio_results", END)
workflow.add_edge("save_video_results", END)
workflow.add_edge("save_mer_results", END)
workflow.add_edge("save_image_results", END)
workflow.add_edge("handle_error", END)
app = workflow.compile()
console.log("Modular graph compiled successfully.")
# print(app.get_graph().draw_mermaid())
return app