This repository has been archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
integrated_nodes.py
642 lines (498 loc) · 20 KB
/
integrated_nodes.py
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
import json
import os
import sys
import traceback
import yaml
from nodes import NODE_CLASS_MAPPINGS as GLOBAL_NODE_CLASS_MAPPINGS
from nodes import NODE_DISPLAY_NAME_MAPPINGS as GLOBAL_NODE_DISPLAY_NAME_MAPPINGS
from server import PromptServer
from aiohttp import web
NODE_CLASS_MAPPINGS = {}
NODE_DISPLAY_NAME_MAPPINGS = {}
WEB_DIRECTORY = 'web'
CONFIG_FILE = 'integrated_nodes.yaml'
CONFIG_FILE_FALLBACK = 'integrated_nodes.yaml.example'
DEFAULT_CATEGORY = 'integrated'
current_config = CONFIG_FILE
class Node(object):
def __init__(self, id, type, exported_inputs):
cls = GLOBAL_NODE_CLASS_MAPPINGS.get(type)
if cls is None:
cls = NODE_CLASS_MAPPINGS.get(type)
if cls is None:
raise Exception(f"Unknown node type {type}")
self.id = id
self.cls = cls
self.input_map = {}
self.inputs = []
self.outputs = []
self.output_types = []
self.output_names = []
types = getattr(self.cls, "RETURN_TYPES", ())
names = getattr(self.cls, "RETURN_NAMES", types)
for type, name in zip(types, names):
self.outputs.append([])
self.output_types.append(type)
self.output_names.append(name)
inputs = self.cls.INPUT_TYPES()
for name, descriptor in inputs.get("required", {}).items():
input = RequiredInput(register=new_register(), name=name, descriptor=descriptor)
self.input_map[name] = input.register
self.inputs.append(input)
for name, descriptor in inputs.get("optional", {}).items():
input = OptionalInput(register=new_register(), name=name, descriptor=descriptor)
self.input_map[name] = input.register
self.inputs.append(input)
for name, type in inputs.get("hidden", {}).items():
# Hidden inputs are always exported
if name not in exported_inputs:
exported_inputs[name] = HiddenInput(register=new_register(), descriptor=type)
elif exported_inputs[name].descriptor != type:
warn(f"Mismatched types for hidden input {name}: {type} and {exported_inputs[name].descriptor}")
self.input_map[name] = exported_inputs[name].register
@property
def output_node(self):
return hasattr(self.cls, "OUTPUT_NODE") and self.cls.OUTPUT_NODE
def input_by_name(self, name):
return next(input for input in self.inputs if input.name == name)
def assign_defaults_list(self, defaults):
skip_next = False
i = 0
for value in defaults:
if skip_next:
skip_next = False
continue
while True:
# Find the next input that isn't an input slot
input = self.inputs[i]
i += 1
if not input.link and (isinstance(input.descriptor[0], list) or len(input.descriptor) >= 2):
break
input.set_default_value(value)
if input.type == "INT" and input.name in ("seed", "denoise_seed"):
# Seed inputs get an additional control_after_generate widget, ignore its value
skip_next = True
elif len(input.descriptor) > 1 and input.descriptor[1].get("image_upload") is True:
# Image upload inputs get an additional IMAGEUPLOAD widget, ignore its value
skip_next = True
def assign_defaults_map(self, defaults):
for name, value in defaults.items():
if name == "choose file to upload":
continue
if isinstance(value, list) and len(value) == 2:
self.input_by_name(name).link = (value[0], value[1])
else:
self.input_by_name(name).set_default_value(value)
class Input(object):
def __init__(self, register, name, descriptor):
self.registers = [register]
self.name = name
self.descriptor = descriptor
self.link = None
def get_default_value(self):
if len(self.descriptor) >= 2 and "default" in self.descriptor[1]:
return self.descriptor[1]["default"]
elif self.descriptor[0] == "STRING":
return ""
elif self.descriptor[0] == "INT":
return 0
elif self.descriptor[0] == "FLOAT":
return 0.0
elif self.descriptor[0] == "BOOLEAN":
return False
elif isinstance(self.descriptor[0], list) and len(self.descriptor[0]) > 0:
return self.descriptor[0][0]
return None
def set_default_value(self, value):
if len(self.descriptor) == 1:
self.descriptor = (self.type, {"default": value})
else:
self.descriptor[1]["default"] = value
@property
def register(self):
return self.registers[0]
@property
def type(self):
return self.descriptor[0]
class RequiredInput(Input):
COLLECTION = "required"
class OptionalInput(Input):
COLLECTION = "optional"
class HiddenInput(object):
COLLECTION = "hidden"
def __init__(self, register, descriptor):
self.register = register
self.descriptor = descriptor
@property
def registers(self):
return [self.register]
class Output(object):
def __init__(self, register, name, type):
self.register = register
self.name = name
self.type = type
class NodeProcessor(object):
# Overwritten by subclasses
NODE = None
def __init__(self):
self.inner = self.NODE.cls()
@classmethod
def map_inputs(s, state):
params = {}
for name, register in s.NODE.input_map.items():
try:
params[name] = state[register]
except KeyError:
# ignore missing parameters, probably optional
pass
return params
@classmethod
def validate(s, state):
if hasattr(s.NODE.cls, "VALIDATE_INPUTS"):
return s.NODE.cls.VALIDATE_INPUTS(**s.map_inputs(state))
else:
return True
@classmethod
def has_is_changed(s):
return hasattr(s.NODE.cls, "IS_CHANGED")
@classmethod
def is_changed(s, state):
return s.NODE.cls.IS_CHANGED(**s.map_inputs(state))
def process(self, state, ui):
function_name = self.NODE.cls.FUNCTION
result = getattr(self.inner, function_name)(**self.map_inputs(state))
if isinstance(result, dict):
outputs = result.get("result", ())
for key, value in result.get("ui", {}).items():
ui.setdefault(key, []).extend(value)
else:
outputs = result
for register_ids, value in zip(self.NODE.outputs, outputs):
for register_id in register_ids:
state[register_id] = value
class IntegratedNode(object):
FUNCTION = "process"
# Overwritten by subclasses
PROCESSORS = None
INPUTS = None
OUTPUTS = None
INITIAL_STATE = None
def __init__(self):
self.processors = [processor() for processor in self.PROCESSORS]
pass
@classmethod
def construct_state(s, **kwargs):
state = s.INITIAL_STATE.copy()
for name, value in kwargs.items():
try:
input = s.INPUTS[name]
except KeyError:
raise Exception(f"Unexpected parameter {name}")
for register in input.registers:
state[register] = value
return state
@classmethod
def INPUT_TYPES(s):
types = {}
for name, input in s.INPUTS.items():
types.setdefault(input.COLLECTION, {})[name] = input.descriptor
return types
@classmethod
@property
def RETURN_TYPES(s):
return [output.type for output in s.OUTPUTS]
@classmethod
@property
def RETURN_NAMES(s):
return [output.name for output in s.OUTPUTS]
@classmethod
def VALIDATE_INPUTS(s, **kwargs):
state = s.construct_state(**kwargs)
for processor in s.PROCESSORS:
validation_result = processor.validate(state)
if validation_result is not True:
return validation_result
return True
@classmethod
def _IS_CHANGED(s, **kwargs):
state = s.construct_state(**kwargs)
result = []
for processor in s.PROCESSORS:
if processor.has_is_changed():
result.append(processor.is_changed(state))
return "".join(result)
def process(self, **kwargs):
state = self.construct_state(**kwargs)
ui = {}
for processor in self.processors:
processor.process(state, ui)
result = []
for output in self.OUTPUTS:
result.append(state[output.register])
return {
"result": result,
"ui": ui,
}
def warn(warning):
print(f"{current_config}: {warning}", file=sys.stderr)
max_register_id = 0
def new_register():
global max_register_id
max_register_id += 1
return max_register_id
def create_nodes(workflow):
if not isinstance(workflow, dict):
raise Exception("Unknown workflow format")
if isinstance(workflow.get("templates"), list):
# Got a node template file, extract the actual workflow
templates = workflow["templates"]
if len(templates) == 0:
raise Exception("Node templates file contains no templates")
if len(templates) > 1:
warn("Node templates file contains multiple templates, only the first one will be used")
template = templates[0]
if not isinstance(template, dict):
raise Exception("Provided node template is not a dictionary")
try:
workflow = json.loads(template["data"])
except:
traceback.print_exc()
raise Exception("Node template data isn't a valid JSON string")
nodes = []
exported_inputs = {}
if isinstance(workflow.get("nodes"), list):
# Got a workflow file
links = {}
for link in workflow.get("links", []):
if len(link) == 6:
# Workflow
_, from_id, from_slot, to_id, to_slot, _ = link
else:
# Node template
from_id, from_slot, to_id, to_slot, _ = link
links.setdefault(to_id, {})[to_slot] = [from_id, from_slot]
for id, node_workflow in enumerate(workflow["nodes"]):
node = Node(node_workflow.get("id", id), node_workflow["type"], exported_inputs)
for slot, link in links.get(node.id, {}).items():
node.input_by_name(node_workflow["inputs"][slot]["name"]).link = link
node.assign_defaults_list(node_workflow.get("widgets_values", []))
nodes.append(node)
else:
# Prompt API file
for id, node_workflow in workflow.items():
node = Node(id, node_workflow["class_type"], exported_inputs)
node.assign_defaults_map(node_workflow.get("inputs", {}))
nodes.append(node)
return nodes, exported_inputs
def connect_links(nodes):
def node_by_id(id):
return next(node for node in nodes if node.id == id)
linked_inputs = set()
dependencies = {}
for node in nodes:
for input in node.inputs:
if not input.link:
continue
from_id, from_slot = input.link
from_node = node_by_id(from_id)
dependencies.setdefault(node, set()).add(from_node)
output_type = from_node.output_types[from_slot]
if input.type != output_type:
raise Exception(f"Cannot connect input of type {input} to output of type {output_type}")
from_node.outputs[from_slot].append(input.register)
linked_inputs.add(input.register)
return linked_inputs, dependencies
def create_node_processor(node):
return type("NodeProcessor", (NodeProcessor,), {
"NODE": node,
})
def process_workflow(workflow, export_outputs, rename_outputs):
nodes, exported_inputs = create_nodes(workflow)
linked_inputs, dependencies = connect_links(nodes)
exported_outputs = []
for node in nodes:
for targets, name, type in zip(node.outputs, node.output_names, node.output_types):
output_id = f"{node.id} {name}"
# Use explicitly specified exports, fall back to exporting everything not linking anywhere
if (export_outputs is not None and output_id in export_outputs) or (export_outputs is None and len(targets) == 0):
output = Output(register=new_register(), name=rename_outputs.get(output_id, name), type=type)
exported_outputs.append(output)
targets.append(output.register)
# Inputs without incoming links are exported
for input in node.inputs:
if input.register in linked_inputs:
continue
# Find a non-conflicting name for the input
i = 1
name = input.name
while name in exported_inputs:
i += 1
name = f"{input.name}_{i}"
exported_inputs[name] = input
# Find a suitable execution order for the nodes
execution_order = []
while len(execution_order) != len(nodes):
for node in nodes:
if node in execution_order:
continue
if all(source in execution_order for source in dependencies.get(node, set())):
execution_order.append(node)
break
else:
raise Exception("Dependency loop detected")
processors = list(map(create_node_processor, execution_order))
is_output_node = any(node.output_node for node in nodes)
return (processors, exported_inputs, exported_outputs, is_output_node)
def hide_inputs(inputs, hidden):
initial_state = {}
for name in hidden:
try:
input = inputs[name]
except KeyError:
warn(f"Input {name} not found, not hiding")
continue
value = input.get_default_value()
if input.COLLECTION == "required" and value is None:
warn(f"Cannot hide input {name}, it is required and has no default value")
continue
for register in input.registers:
initial_state[register] = value
del inputs[name]
return initial_state
def merge_inputs(inputs, mapping):
if not isinstance(mapping, dict):
warn(f"merge_inputs entry should be a dictionary but got {mapping}, ignoring")
return
for target, sources in mapping.items():
if not isinstance(sources, list):
sources = [sources]
try:
target_input = inputs[target]
except KeyError:
warn(f"Target input {target} not found, merging skipped")
continue
for source in sources:
try:
source_input = inputs[source]
except KeyError:
warn(f"Source input {source} not found, merging skipped")
continue
if target_input.type != source_input.type:
warn(f"Cannot merge input {source} into {target}, type mismatch: {source_input.type} vs. {target_input.type}")
continue
target_input.registers += source_input.registers
del inputs[source]
def rename_inputs(inputs, mapping):
if not isinstance(mapping, dict):
warn(f"rename_inputs entry should be a dictionary but got {mapping}, ignoring")
return
for old, new in mapping.items():
if old not in inputs:
warn(f"Cannot rename input {old}, no input with this name exists")
continue
if new in inputs:
warn(f"Cannot rename input {old} to {new}, another input with this name already exists")
continue
inputs[new] = inputs[old]
del inputs[old]
def create_integrated_node(name, info):
if not isinstance(info, dict):
warn(f"Ignoring integrated node {name}, not a dictionary")
return
try:
workflow_path = info["workflow"]
except KeyError:
warn(f"Ignoring integrated node {name}, missing required workflow entry")
return
try:
with open(os.path.join(os.path.dirname(__file__), workflow_path)) as input:
workflow = json.load(input)
except:
traceback.print_exc()
warn(f"Ignoring integrated node {name}, failed loading workflow from file {workflow_path}")
return
export_outputs = info.get("export_outputs")
if export_outputs is not None and not isinstance(export_outputs, list):
warn(f"export_outputs entry should be a list but got {export_outputs}, ignoring")
export_outputs = None
if export_outputs is not None:
export_outputs = set(export_outputs)
rename_outputs = info.get("rename_outputs", {})
if not isinstance(rename_outputs, dict):
warn(f"rename_outputs entry should be a dictionary but got {rename_outputs}, ignoring")
rename_outputs = {}
try:
(processors, inputs, outputs, is_output_node) = process_workflow(workflow, export_outputs, rename_outputs)
except:
traceback.print_exc()
warn(f"Ignoring integrated node {name}, failed processing workflow")
return
merge_inputs(inputs, info.get("merge_inputs", {}))
initial_state = hide_inputs(inputs, info.get("hide_inputs", {}))
rename_inputs(inputs, info.get("rename_inputs", {}))
cls = type(name, (IntegratedNode,), {
"PROCESSORS": processors,
"INPUTS": inputs,
"OUTPUTS": outputs,
"INITIAL_STATE": initial_state,
"CATEGORY": info.get("category", DEFAULT_CATEGORY),
"OUTPUT_NODE": is_output_node,
})
if any(processor.has_is_changed() for processor in processors):
cls.IS_CHANGED = cls._IS_CHANGED
NODE_CLASS_MAPPINGS[name] = cls
NODE_DISPLAY_NAME_MAPPINGS[name] = info.get("display_name", name)
def load_config():
global current_config
basedir = os.path.dirname(__file__)
current_config = CONFIG_FILE
path = os.path.join(basedir, current_config)
if not os.path.exists(path):
current_config = CONFIG_FILE_FALLBACK
path = os.path.join(basedir, current_config)
with open(path, 'r') as input:
data = yaml.safe_load(input)
if not isinstance(data, dict):
warn("File does not contain a dictionary, ignoring")
return
for name, info in data.items():
create_integrated_node(name, info)
async def add_node(request):
global current_config
post = await request.post()
prompt = post.get('prompt')
name = post.get('name')
if prompt is None or name is None:
return web.Response(status=400)
display_name = post.get('displayName', name)
category = post.get('category', DEFAULT_CATEGORY)
basedir = os.path.dirname(__file__)
config_path = os.path.join(basedir, CONFIG_FILE)
if not os.path.exists(config_path):
config_path = os.path.join(basedir, CONFIG_FILE_FALLBACK)
with open(config_path, 'r') as input:
data = yaml.safe_load(input)
actual_name = name
i = 1
while actual_name in data or os.path.exists(os.path.join(basedir, f'{actual_name}.json')):
# Solve naming conflicts
i += 1
actual_name = f'{name}_{i}'
data[actual_name] = {
'workflow': f'{actual_name}.json'
}
if display_name:
data[actual_name]['display_name'] = display_name
if category:
data[actual_name]['category'] = category
with open(os.path.join(basedir, f'{actual_name}.json'), 'w') as output:
output.write(prompt)
with open(os.path.join(basedir, CONFIG_FILE), 'w') as output:
yaml.safe_dump(data, output, sort_keys=False)
current_config = CONFIG_FILE
create_integrated_node(actual_name, data[actual_name])
GLOBAL_NODE_CLASS_MAPPINGS[actual_name] = NODE_CLASS_MAPPINGS[actual_name]
GLOBAL_NODE_DISPLAY_NAME_MAPPINGS[actual_name] = NODE_DISPLAY_NAME_MAPPINGS[actual_name]
return web.Response(body=actual_name, content_type='text/plain')
load_config()
PromptServer.instance.routes.post('/integrated_nodes/add')(add_node)