-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworkflow_extractor.py
More file actions
771 lines (673 loc) · 28.3 KB
/
Copy pathworkflow_extractor.py
File metadata and controls
771 lines (673 loc) · 28.3 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
"""
Workflow Extractor for MetaHub Save Node
Auto-extracts sampler params, prompts, model, VAE, and LoRAs from ComfyUI prompt data.
"""
from typing import Any, Dict, List, Optional, Set, Tuple
class WorkflowExtractor:
SAMPLER_NODES = [
"KSampler",
"KSamplerAdvanced",
"SamplerCustom",
"SamplerCustomAdvanced",
"KSamplerSelect",
"KSampler (Efficient)",
"ImpactKSamplerBasicPipe",
"ImpactKSamplerAdvancedPipe",
"ImpactKSampler",
]
CHECKPOINT_NODES = [
"CheckpointLoaderSimple",
"CheckpointLoader",
"UNETLoader",
"UnetLoaderGGUF",
"DualDiffusionLoader",
"DiffusionModelLoader",
]
VAE_NODES = [
"VAELoader",
"CheckpointLoaderSimple",
"CheckpointLoader",
]
LORA_NODES = [
"LoraLoader",
"LoraLoaderModelOnly",
"LoraLoaderModelOnlyAdvanced",
"LoraLoaderAdvanced",
"LoraLoaderAny",
]
CLIP_NODES = [
"CLIPTextEncode",
"CLIPTextEncodeSDXL",
"CLIPTextEncodeSDXLPlus",
"CLIPTextEncodeSDXLRefiner",
"CLIPTextEncodeSD3",
]
VAE_DECODE_NODES = [
"VAEDecode",
"VAEDecodeTiled",
"VAEDecode (Tiled)",
]
LOAD_IMAGE_NODES = [
"LoadImage",
"LoadImageMask",
]
def __init__(self, prompt: Optional[Dict[str, Any]]):
self.prompt: Dict[str, Any] = {}
if isinstance(prompt, dict):
for node_id, node_data in prompt.items():
self.prompt[str(node_id)] = node_data
def extract(self, save_node_id: Optional[str] = None) -> Tuple[Dict[str, Any], Set[str]]:
data: Dict[str, Any] = {"lora_list": []}
missing: Set[str] = set()
sampler_node_id = self.find_sampler_for_save_node(save_node_id)
if sampler_node_id:
sampler_params = self.extract_sampler_params(sampler_node_id)
for key in ("seed", "steps", "cfg", "sampler_name", "scheduler", "denoise"):
value = sampler_params.get(key)
if value is None:
missing.add(key)
else:
data[key] = value
positive, negative = self.extract_prompts(sampler_node_id)
if positive is None:
missing.add("positive")
else:
data["positive"] = positive
if negative is None:
missing.add("negative")
else:
data["negative"] = negative
model_name = self.extract_model_name(sampler_node_id)
if model_name is None:
missing.add("model_name")
else:
data["model_name"] = model_name
else:
missing.update(
{
"seed",
"steps",
"cfg",
"sampler_name",
"scheduler",
"denoise",
"positive",
"negative",
"model_name",
}
)
vae_name = self.extract_vae_name(save_node_id)
if vae_name is None:
missing.add("vae_name")
else:
data["vae_name"] = vae_name
lora_list, has_lora_nodes = self.extract_loras()
if lora_list:
data["lora_list"] = lora_list
elif has_lora_nodes:
missing.add("loras")
lineage = self.extract_model_3d_lineage(save_node_id) or self.extract_lineage(sampler_node_id)
if lineage:
data.update(lineage)
return data, missing
def find_sampler_for_save_node(self, save_node_id: Optional[str]) -> Optional[str]:
sampler_nodes = self._find_nodes_by_type(self.SAMPLER_NODES)
if not sampler_nodes:
return None
if save_node_id:
save_node = self._get_node(save_node_id)
if save_node:
inputs = save_node.get("inputs", {})
for input_name in ("images", "model_3d"):
start_node_id = self._get_connection_node_id(inputs.get(input_name))
if start_node_id:
sampler_id = self._find_sampler_from_images_source(start_node_id)
if sampler_id:
return sampler_id
return sampler_nodes[0]
def extract_sampler_params(self, sampler_node_id: str) -> Dict[str, Any]:
node = self._get_node(sampler_node_id)
if not node:
return {}
inputs = node.get("inputs", {})
seed = self._coerce_int(self._get_literal_input(inputs, "seed"))
if seed is None:
seed = self._coerce_int(
self._resolve_scalar_from_connection(inputs.get("seed"), ("seed", "value"))
)
if seed is None:
seed = self._coerce_int(
self._resolve_scalar_from_connection(inputs.get("noise"), ("noise_seed", "seed", "value"))
)
if seed is None:
seed = self._coerce_int(
self._resolve_scalar_from_connection(inputs.get("sampler"), ("seed", "noise_seed", "value"))
)
steps = self._coerce_int(self._get_literal_input(inputs, "steps"))
if steps is None:
steps = self._coerce_int(
self._resolve_scalar_from_connection(inputs.get("sigmas"), ("steps", "value"))
)
cfg = self._coerce_float(self._get_first_literal(inputs, ("cfg", "cfg_scale")))
if cfg is None:
cfg = self._coerce_float(
self._resolve_scalar_from_connection(inputs.get("guider"), ("cfg", "cfg_scale", "scale"))
)
sampler_name = self._get_first_literal(inputs, ("sampler_name", "sampler"))
if sampler_name is None:
sampler_name = self._resolve_scalar_from_connection(
inputs.get("sampler"), ("sampler_name", "sampler", "name")
)
scheduler = self._get_first_literal(inputs, ("scheduler", "scheduler_name"))
if scheduler is None:
scheduler = self._resolve_scalar_from_connection(
inputs.get("sigmas"), ("scheduler", "scheduler_name", "name")
)
denoise = self._coerce_float(self._get_first_literal(inputs, ("denoise", "denoise_strength")))
if denoise is None:
denoise = self._coerce_float(
self._resolve_scalar_from_connection(inputs.get("sigmas"), ("denoise", "denoise_strength"))
)
return {
"seed": seed,
"steps": steps,
"cfg": cfg,
"sampler_name": sampler_name,
"scheduler": scheduler,
"denoise": denoise,
}
def extract_model_name(self, sampler_node_id: str) -> Optional[str]:
node = self._get_node(sampler_node_id)
if not node:
return None
inputs = node.get("inputs", {})
start_node_ids = []
for input_name in ("model", "guider", "sigmas"):
start_node_id = self._get_connection_node_id(inputs.get(input_name))
if start_node_id:
start_node_ids.append(start_node_id)
if not start_node_ids:
return None
checkpoint_node_id = self._bfs_upstream(
start_node_ids,
lambda n: self._class_type(n) in self.CHECKPOINT_NODES,
)
if not checkpoint_node_id:
return None
checkpoint_node = self._get_node(checkpoint_node_id)
if not checkpoint_node:
return None
return self._get_checkpoint_name(checkpoint_node)
def extract_vae_name(self, save_node_id: Optional[str]) -> Optional[str]:
vae_decode_id = self._find_vae_decode_node(save_node_id)
if not vae_decode_id:
return None
decode_node = self._get_node(vae_decode_id)
if not decode_node:
return None
vae_conn = decode_node.get("inputs", {}).get("vae")
start_node_id = self._get_connection_node_id(vae_conn)
if not start_node_id:
return None
vae_node_id = self._bfs_upstream(
[start_node_id],
lambda n: self._class_type(n) in self.VAE_NODES,
)
if not vae_node_id:
return None
vae_node = self._get_node(vae_node_id)
if not vae_node:
return None
return self._get_vae_name(vae_node)
def extract_prompts(self, sampler_node_id: str) -> Tuple[Optional[str], Optional[str]]:
node = self._get_node(sampler_node_id)
if not node:
return None, None
inputs = node.get("inputs", {})
positive_conn = inputs.get("positive") or inputs.get("positive_cond")
negative_conn = inputs.get("negative") or inputs.get("negative_cond")
positive = self._extract_text_from_connection(positive_conn)
negative = self._extract_text_from_connection(negative_conn)
if positive is None:
positive = self._extract_text_from_connection(inputs.get("guider"))
if negative is None:
negative = self._extract_text_from_connection(inputs.get("negative_conditioning"))
return positive, negative
def extract_loras(self) -> Tuple[List[Dict[str, Any]], bool]:
loras: List[Dict[str, Any]] = []
has_lora_nodes = False
for _, node in self.prompt.items():
class_type = self._class_type(node)
if not self._is_lora_node(class_type):
continue
has_lora_nodes = True
inputs = node.get("inputs", {})
stacked_loras = self._extract_lora_manager_entries(inputs)
if stacked_loras:
loras.extend(stacked_loras)
continue
dynamic_loras = self._extract_dynamic_lora_entries(inputs)
if dynamic_loras:
loras.extend(dynamic_loras)
continue
lora_name = self._get_first_literal(inputs, ("lora_name", "lora", "lora_name_1", "lora_1"))
if not isinstance(lora_name, str) or not lora_name.strip():
continue
strength_model = self._get_first_literal(
inputs,
("strength_model", "strength", "strength_unet"),
)
weight = self._coerce_float(strength_model)
if weight is None:
strength_clip = self._get_first_literal(inputs, ("strength_clip",))
weight = self._coerce_float(strength_clip)
if weight is None:
weight = 1.0
loras.append({"name": lora_name, "weight": float(weight)})
return loras, has_lora_nodes
def _extract_dynamic_lora_entries(self, inputs: Dict[str, Any]) -> List[Dict[str, Any]]:
loras: List[Dict[str, Any]] = []
for key, raw_lora in inputs.items():
if not key.lower().startswith("lora_") or not isinstance(raw_lora, dict):
continue
if raw_lora.get("on") is False or raw_lora.get("active") is False:
continue
name = raw_lora.get("lora") or raw_lora.get("lora_name") or raw_lora.get("name")
if not isinstance(name, str) or not name.strip():
continue
weight = self._coerce_float(
raw_lora.get("strength")
if raw_lora.get("strength") is not None
else raw_lora.get("modelStrength")
)
if weight is None:
weight = self._coerce_float(raw_lora.get("strength_model"))
if weight is None:
weight = 1.0
loras.append({"name": name, "weight": float(weight)})
return loras
def _extract_lora_manager_entries(self, inputs: Dict[str, Any]) -> List[Dict[str, Any]]:
raw_loras = inputs.get("loras")
if isinstance(raw_loras, dict) and isinstance(raw_loras.get("__value__"), list):
raw_loras = raw_loras["__value__"]
if not isinstance(raw_loras, list):
return []
loras: List[Dict[str, Any]] = []
for raw_lora in raw_loras:
if not isinstance(raw_lora, dict):
continue
if raw_lora.get("active") is False:
continue
name = raw_lora.get("name") or raw_lora.get("lora_name")
if not name:
continue
weight = self._coerce_float(
raw_lora.get("strength")
if raw_lora.get("strength") is not None
else raw_lora.get("modelStrength")
)
if weight is None:
weight = self._coerce_float(raw_lora.get("clipStrength"))
if weight is None:
weight = 1.0
loras.append({"name": str(name), "weight": float(weight)})
return loras
def extract_lineage(self, sampler_node_id: Optional[str]) -> Dict[str, Any]:
if not sampler_node_id:
return {}
node = self._get_node(sampler_node_id)
if not node:
return {}
inputs = node.get("inputs", {})
latent_conn = inputs.get("latent_image") or inputs.get("latent") or inputs.get("samples")
start_node_id = self._get_connection_node_id(latent_conn)
if not start_node_id:
return {}
queue = [start_node_id]
visited: Set[str] = set()
source_image: Optional[Dict[str, Any]] = None
has_load_image = False
has_inpaint = False
has_outpaint = False
while queue:
current_id = queue.pop(0)
if current_id in visited:
continue
visited.add(current_id)
current = self._get_node(current_id)
if not current:
continue
class_type = self._class_type(current)
class_lower = class_type.lower()
if class_type in self.LOAD_IMAGE_NODES or class_lower in {"loadimage", "loadimagemask"}:
has_load_image = True
if not source_image:
image_value = current.get("inputs", {}).get("image")
if isinstance(image_value, str) and image_value.strip():
normalized = image_value.replace("\\", "/")
source_image = {
"fileName": normalized.split("/")[-1],
"relativePath": normalized,
"nodeId": current_id,
"nodeType": class_type,
}
if "outpaint" in class_lower or class_lower in {"padforoutpaint", "imagepadforoutpaint"}:
has_outpaint = True
if "inpaint" in class_lower or class_lower == "setlatentnoisemask" or "mask" in class_lower:
has_inpaint = True
for input_value in current.get("inputs", {}).values():
upstream_id = self._get_connection_node_id(input_value)
if upstream_id and upstream_id not in visited:
queue.append(upstream_id)
if not has_load_image:
return {}
generation_type = "outpaint" if has_outpaint else "inpaint" if has_inpaint else "img2img"
return {
"generation_type": generation_type,
"source_image": source_image,
}
def extract_model_3d_lineage(self, save_node_id: Optional[str]) -> Dict[str, Any]:
if not save_node_id:
return {}
save_node = self._get_node(save_node_id)
if not save_node:
return {}
class_type = self._class_type(save_node).lower().replace("_", "").replace(" ", "")
if "save3d" not in class_type and "saveglb" not in class_type:
return {}
inputs = save_node.get("inputs", {})
model_conn = inputs.get("model_3d") or inputs.get("mesh") or inputs.get("model")
start_node_id = self._get_connection_node_id(model_conn)
if not start_node_id:
return {}
queue = [start_node_id]
visited: Set[str] = set()
while queue:
current_id = queue.pop(0)
if current_id in visited:
continue
visited.add(current_id)
current = self._get_node(current_id)
if not current:
continue
current_type = self._class_type(current)
if current_type in self.LOAD_IMAGE_NODES or current_type.lower() == "loadimage":
image_value = current.get("inputs", {}).get("image")
if isinstance(image_value, str) and image_value.strip():
normalized = image_value.replace("\\", "/")
return {
"generation_type": "image2model3d",
"source_image": {
"fileName": normalized.split("/")[-1],
"relativePath": normalized,
"nodeId": current_id,
"nodeType": current_type,
},
}
for input_value in current.get("inputs", {}).values():
upstream_id = self._get_connection_node_id(input_value)
if upstream_id and upstream_id not in visited:
queue.append(upstream_id)
return {}
def _find_sampler_from_images_source(self, start_node_id: str) -> Optional[str]:
start_node = self._get_node(start_node_id)
if not start_node:
return None
if self._class_type(start_node) in self.SAMPLER_NODES:
return start_node_id
if self._class_type(start_node) in self.VAE_DECODE_NODES:
samples_conn = start_node.get("inputs", {}).get("samples")
samples_node_id = self._get_connection_node_id(samples_conn)
if samples_node_id:
sampler_id = self._bfs_upstream(
[samples_node_id],
lambda n: self._class_type(n) in self.SAMPLER_NODES,
)
if sampler_id:
return sampler_id
return self._bfs_upstream(
[start_node_id],
lambda n: self._class_type(n) in self.SAMPLER_NODES,
)
def _find_vae_decode_node(self, save_node_id: Optional[str]) -> Optional[str]:
if not save_node_id:
return None
save_node = self._get_node(save_node_id)
if not save_node:
return None
inputs = save_node.get("inputs", {})
for input_name in ("images", "model_3d"):
start_node_id = self._get_connection_node_id(inputs.get(input_name))
if not start_node_id:
continue
vae_decode_id = self._bfs_upstream(
[start_node_id],
lambda n: self._class_type(n) in self.VAE_DECODE_NODES,
)
if vae_decode_id:
return vae_decode_id
return None
def _find_nodes_by_type(self, types: List[str]) -> List[str]:
matched = []
for node_id, node in self.prompt.items():
if self._class_type(node) in types:
matched.append(node_id)
return matched
def _extract_text_from_connection(self, conn: Any) -> Optional[str]:
node_id = self._get_connection_node_id(conn)
if not node_id:
return None
return self._extract_text_from_node(node_id)
def _extract_text_from_node(self, start_node_id: str) -> Optional[str]:
texts: List[str] = []
queue = [start_node_id]
visited: Set[str] = set()
while queue:
node_id = queue.pop(0)
if node_id in visited:
continue
visited.add(node_id)
node = self._get_node(node_id)
if not node:
continue
class_type = self._class_type(node)
if class_type == "ConditioningZeroOut":
return ""
if self._is_prompt_encoder(class_type):
text = self._get_clip_text(node)
if text is not None:
texts.append(text)
continue
for input_val in node.get("inputs", {}).values():
conn_id = self._get_connection_node_id(input_val)
if conn_id and conn_id not in visited:
queue.append(conn_id)
if texts:
return "\n".join(texts)
return None
def _get_clip_text(self, node: Dict[str, Any]) -> Optional[str]:
inputs = node.get("inputs", {})
text = self._get_literal_input(inputs, "text")
if text is not None and not isinstance(text, bool) and str(text).strip():
return str(text)
text_from_conn = self._resolve_string_from_connection(inputs.get("text"))
if text_from_conn is not None:
return text_from_conn
parts: List[str] = []
for key in ("text_g", "text_l"):
value = self._get_literal_input(inputs, key)
if value:
parts.append(str(value))
if parts:
return "\n".join(parts)
return None
def _resolve_string_from_connection(self, conn: Any) -> Optional[str]:
node_id = self._get_connection_node_id(conn)
if not node_id:
return None
return self._resolve_string_from_node(node_id, set())
def _resolve_string_from_node(self, node_id: str, visited: Set[str]) -> Optional[str]:
if node_id in visited:
return None
visited.add(node_id)
node = self._get_node(node_id)
if not node:
return None
inputs = node.get("inputs", {})
class_type = self._class_type(node)
class_lower = class_type.lower()
if class_type == "ConditioningZeroOut":
return ""
if self._is_prompt_encoder(class_type):
return self._get_clip_text(node)
if "switch" in class_lower and "on_false" in inputs and "on_true" in inputs:
switch_value = self._resolve_boolean_input(inputs.get("switch"))
branch = "on_true" if switch_value is True else "on_false"
value = self._resolve_string_from_connection_with_visited(inputs.get(branch), visited)
if value is not None:
return value
if class_lower == "joinstrings" or "join strings" in class_lower:
delimiter = self._get_literal_input(inputs, "delimiter")
joiner = str(delimiter) if delimiter is not None else " "
parts: List[str] = []
for key in ("string1", "string2", "string3", "string4"):
value = self._get_literal_input(inputs, key)
if value is None:
value = self._resolve_string_from_connection_with_visited(inputs.get(key), visited)
if value is not None and str(value).strip():
parts.append(str(value).strip())
return joiner.join(parts) if parts else ""
for key in ("text", "positive", "negative", "prompt", "string", "value"):
value = self._get_literal_input(inputs, key)
if value is not None and not isinstance(value, bool) and str(value).strip():
return str(value)
for key in ("positive", "text", "string1", "string2", "conditioning"):
value = self._resolve_string_from_connection_with_visited(inputs.get(key), visited)
if value is not None and str(value).strip():
return value
for input_val in inputs.values():
value = self._resolve_string_from_connection_with_visited(input_val, visited)
if value is not None and str(value).strip():
return value
return None
def _resolve_boolean_input(self, value: Any) -> Optional[bool]:
if isinstance(value, bool):
return value
node_id = self._get_connection_node_id(value)
if not node_id:
return None
node = self._get_node(node_id)
literal = node.get("inputs", {}).get("value") if node else None
return literal if isinstance(literal, bool) else None
def _resolve_string_from_connection_with_visited(self, conn: Any, visited: Set[str]) -> Optional[str]:
node_id = self._get_connection_node_id(conn)
if not node_id:
return None
return self._resolve_string_from_node(node_id, visited)
def _resolve_scalar_from_connection(self, conn: Any, keys: Tuple[str, ...]) -> Any:
node_id = self._get_connection_node_id(conn)
if not node_id:
return None
queue = [node_id]
visited: Set[str] = set()
while queue:
current_id = queue.pop(0)
if current_id in visited:
continue
visited.add(current_id)
node = self._get_node(current_id)
if not node:
continue
inputs = node.get("inputs", {})
for key in keys:
value = self._get_literal_input(inputs, key)
if value is not None:
return value
for input_val in inputs.values():
conn_id = self._get_connection_node_id(input_val)
if conn_id and conn_id not in visited:
queue.append(conn_id)
return None
def _get_checkpoint_name(self, node: Dict[str, Any]) -> Optional[str]:
inputs = node.get("inputs", {})
for key in ("ckpt_name", "checkpoint", "model_name", "unet_name", "diffusion_model_name", "model"):
value = self._get_literal_input(inputs, key)
if value:
return str(value)
return None
def _get_vae_name(self, node: Dict[str, Any]) -> Optional[str]:
inputs = node.get("inputs", {})
for key in ("vae_name", "ckpt_name", "model_name"):
value = self._get_literal_input(inputs, key)
if value:
return str(value)
return None
def _get_node(self, node_id: Optional[str]) -> Optional[Dict[str, Any]]:
if node_id is None:
return None
return self.prompt.get(str(node_id))
@staticmethod
def _class_type(node: Dict[str, Any]) -> str:
return str(node.get("class_type", ""))
@staticmethod
def _is_connection(value: Any) -> bool:
return isinstance(value, (list, tuple)) and len(value) >= 2
def _get_connection_node_id(self, value: Any) -> Optional[str]:
if not self._is_connection(value):
return None
return str(value[0])
def _get_literal_input(self, inputs: Dict[str, Any], key: str) -> Any:
value = inputs.get(key)
if self._is_connection(value):
return None
return value
def _get_first_literal(self, inputs: Dict[str, Any], keys: Tuple[str, ...]) -> Any:
for key in keys:
value = self._get_literal_input(inputs, key)
if value is not None:
return value
return None
def _bfs_upstream(self, start_ids: List[str], match_fn) -> Optional[str]:
queue = list(start_ids)
visited: Set[str] = set()
while queue:
node_id = queue.pop(0)
if node_id in visited:
continue
visited.add(node_id)
node = self._get_node(node_id)
if not node:
continue
if match_fn(node):
return node_id
for input_val in node.get("inputs", {}).values():
conn_id = self._get_connection_node_id(input_val)
if conn_id and conn_id not in visited:
queue.append(conn_id)
return None
@staticmethod
def _coerce_int(value: Any) -> Optional[int]:
if value is None:
return None
try:
return int(value)
except (TypeError, ValueError):
return None
@staticmethod
def _coerce_float(value: Any) -> Optional[float]:
if value is None:
return None
try:
return float(value)
except (TypeError, ValueError):
return None
@classmethod
def _is_lora_node(cls, class_type: str) -> bool:
if class_type in cls.LORA_NODES:
return True
return "lora" in class_type.lower()
@classmethod
def _is_prompt_encoder(cls, class_type: str) -> bool:
if class_type in cls.CLIP_NODES:
return True
return "promptencoder" in class_type.lower().replace(" ", "")