-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathworkflow_nodes.py
190 lines (157 loc) · 5.91 KB
/
workflow_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
import torch
import numpy as np
from PIL import Image
import hashlib
from torchvision import transforms
class AnyType(str):
"""A special class that is always equal in not equal comparisons. Credit to pythongosssss"""
def __eq__(self, _) -> bool:
return True
def __ne__(self, __value: object) -> bool:
return False
BOOLEAN = ("BOOLEAN", {"default": True})
STRING = ("STRING", {"default": ""})
any_input = AnyType("*")
node_type_list = ["none", "IMAGE", "MASK", "STRING", "INT", "FLOAT", "LATENT", "BOOLEAN", "CLIP", "CONDITIONING",
"MODEL", "VAE"]
class WorkflowContinue:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input": ("IMAGE", {"default": []}),
"type": (
["none", "IMAGE", "LATENT"],),
"continue_workflow": BOOLEAN,
}
}
RETURN_TYPES = (AnyType("*"),)
RETURN_NAMES = ("output",)
FUNCTION = "execute"
CATEGORY = "FlowChain ⛓️"
@classmethod
def IS_CHANGED(s, input, type, continue_workflow):
m = hashlib.sha256()
if input is None:
return "0"
else:
m.update(input.encode() + str(continue_workflow).encode())
return m.digest().hex()
def execute(self, input, type, continue_workflow):
print("WorkflowContinue", continue_workflow)
if continue_workflow:
if type == "LATENT":
ret = {"samples": input["samples"][0].unsqueeze(0)}
if "noise_mask" in input:
ret["noise_mask"] = input["noise_mask"][0].unsqueeze(0)
return (ret,)
else:
return (input,)
else:
return (input[0].unsqueeze(0),)
class WorkflowInput:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"Name": STRING,
"type": (node_type_list,),
"default": ("*",)
}}
RETURN_TYPES = (AnyType("*"),)
RETURN_NAMES = ("output",)
FUNCTION = "execute"
CATEGORY = "FlowChain ⛓️"
# OUTPUT_NODE = True
@classmethod
def IS_CHANGED(s, Name, type, default, **kwargs):
m = hashlib.sha256()
if default is not None:
m.update(str(default).encode())
else:
m.update(Name.encode() + type.encode())
return m.digest().hex()
@classmethod
def VALIDATE_INPUTS(cls, input_types):
return True
def execute(self, Name, type, default, **kwargs):
return (default,)
class WorkflowOutput:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {"required": {
"Name": STRING,
"type": (node_type_list,)
},
"hidden": {
"ui": BOOLEAN
}}
RETURN_TYPES = (AnyType("*"),)
RETURN_NAMES = ("output",)
FUNCTION = "execute"
CATEGORY = "FlowChain ⛓️"
OUTPUT_NODE = True
@classmethod
def IS_CHANGED(s, Name, type, ui=True, **kwargs):
m = hashlib.sha256()
m.update(Name.encode() + type.encode())
return m.digest().hex()
def execute(self, Name, type, ui=True, **kwargs):
if ui:
if kwargs["default"] is None:
return (torch.tensor([]),)
return (kwargs["default"],)
else:
if type in ["IMAGE", "MASK"]:
if kwargs["default"] is None:
black_image_np = np.zeros((255, 255, 3), dtype=np.uint8)
black_image_pil = Image.fromarray(black_image_np)
transform = transforms.ToTensor()
image_tensor = transform(black_image_pil)
image_tensor = image_tensor.permute(1, 2, 0)
image_tensor = image_tensor.unsqueeze(0)
return {"ui": {"default": image_tensor}}
return {"ui": {"default": kwargs["default"]}}
elif type == "LATENT":
if kwargs["default"] is None:
return {"ui": {"default": torch.tensor([])}}
return {"ui": {"default": kwargs["default"]}}
else:
ui = {"ui": {}}
ui["ui"]["default"] = kwargs["default"]
return ui
NODE_CLASS_MAPPINGS_NODES = {
"WorkflowInput": WorkflowInput,
"WorkflowOutput": WorkflowOutput,
# "WorkflowInputImage": WorkflowInputImage,
# "WorkflowInputString": WorkflowInputString,
# "WorkflowInputBoolean": WorkflowInputBoolean,
# "WorkflowInputInteger": WorkflowInputInteger,
# "WorkflowInputFloat": WorkflowInputFloat,
# "WorkflowOutputImage": WorkflowOutputImage,
# "WorkflowInputSwitch": WorkflowInputSwitch,
# "WorkflowContinueImage": WorkflowContinueImage,
# "WorkflowContinueLatent": WorkflowContinueLatent,
"WorkflowContinue": WorkflowContinue,
}
# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS_NODES = {
"WorkflowInput": "Workflow Input (FlowChain ⛓️)",
"WorkflowOutput": "Workflow Output (FlowChain ⛓️)",
# "WorkflowInputImage": "Workflow Input Image (Lipsync Studio)",
# "WorkflowInputString": "Workflow Input String (Lipsync Studio)",
# "WorkflowInputBoolean": "Workflow Input Boolean (Lipsync Studio)",
# "WorkflowInputInteger": "Workflow Input Integer (Lipsync Studio)",
# "WorkflowInputFloat": "Workflow Input Float (Lipsync Studio)",
# "WorkflowOutputImage": "Workflow Output Image (Lipsync Studio)",
# "WorkflowInputSwitch": "Workflow Input Switch (Lipsync Studio)",
# "WorkflowContinueImage": "Workflow Continue Image (Lipsync Studio)",
# "WorkflowContinueLatent": "Workflow Continue Latent (Lipsync Studio)",
"WorkflowContinue": "Workflow Continue (FlowChain ⛓️)",
# "VisualizeOpticalFlow": "Visualize optical flow",
}