-
Notifications
You must be signed in to change notification settings - Fork 8
/
comfyui_rc.py
197 lines (156 loc) · 4.66 KB
/
comfyui_rc.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
import base64
import json
import lzma
from io import BytesIO
import torch
NODE_CLASS_MAPPINGS = {}
NODE_DISPLAY_NAME_MAPPINGS = {}
def register_node(identifier: str, display_name: str):
def decorator(cls):
NODE_CLASS_MAPPINGS[identifier] = cls
NODE_DISPLAY_NAME_MAPPINGS[identifier] = display_name
return cls
return decorator
def compress(x: bytes):
comp = lzma.LZMACompressor()
out = comp.compress(x)
return out + comp.flush()
def decompress(x: bytes):
decomp = lzma.LZMADecompressor()
return decomp.decompress(x)
def base85_encode(x: bytes):
return base64.b85encode(x)
def base85_decode(x: bytes):
return base64.b85decode(x)
def torch_save_to_bytes(obj):
with BytesIO() as f:
torch.save(obj, f)
return f.getvalue()
def torch_load_from_bytes(text: bytes):
with BytesIO(text) as f:
return torch.load(f)
def torch_save_to_blob(obj):
return base85_encode(compress(torch_save_to_bytes(obj)))
def torch_load_from_blob(text: bytes):
return torch_load_from_bytes(decompress(base85_decode(text)))
@register_node("RCReceiveLatent", "Remote Call: Receive Latent")
class _:
CATEGORY = "jamesWalker55/rc"
INPUT_TYPES = lambda: {
"required": {
"key": (
"STRING",
{"default": "input_latent", "multiline": False},
),
"value": (
"STRING",
{"default": "Don't touch this field!", "multiline": False},
),
}
}
RETURN_TYPES = ("LATENT",)
FUNCTION = "execute"
def execute(self, key: str, value: str):
latent = torch_load_from_blob(value)
val = {"samples": latent}
# { "samples": <Latent: [1, 4, 64, 64]> }
return (val,)
@register_node("RCReceiveInt", "Remote Call: Receive Integer")
class _:
CATEGORY = "jamesWalker55/rc"
INPUT_TYPES = lambda: {
"required": {
"key": (
"STRING",
{"default": "input_integer", "multiline": False},
),
"value": ("INT", {"default": 0, "min": -99999999999, "max": 99999999999}),
}
}
RETURN_TYPES = ("INT",)
FUNCTION = "execute"
def execute(self, key: str, value):
return (value,)
@register_node("RCReceiveFloat", "Remote Call: Receive Float")
class _:
CATEGORY = "jamesWalker55/rc"
INPUT_TYPES = lambda: {
"required": {
"key": (
"STRING",
{"default": "input_float", "multiline": False},
),
"value": ("FLOAT", {"default": 0, "min": -99999999999, "max": 99999999999}),
}
}
RETURN_TYPES = ("FLOAT",)
FUNCTION = "execute"
def execute(self, key: str, value):
return (value,)
@register_node("RCReceiveIntList", "Remote Call: Receive Integer List")
class _:
CATEGORY = "jamesWalker55/rc"
INPUT_TYPES = lambda: {
"required": {
"key": (
"STRING",
{"default": "input_integer_list", "multiline": False},
),
"value": (
"STRING",
{"default": "[1, 2, 3]", "multiline": False},
),
}
}
RETURN_TYPES = ("INT_LIST",)
FUNCTION = "execute"
def execute(self, key: str, value):
value = json.loads(value)
return (value,)
@register_node("RCReceiveFloatList", "Remote Call: Receive Float List")
class _:
CATEGORY = "jamesWalker55/rc"
INPUT_TYPES = lambda: {
"required": {
"key": (
"STRING",
{"default": "input_float_list", "multiline": False},
),
"value": (
"STRING",
{"default": "[1.0, 2.0, 3.0]", "multiline": False},
),
}
}
RETURN_TYPES = ("FLOAT_LIST",)
FUNCTION = "execute"
def execute(self, key: str, value):
value = json.loads(value)
return (value,)
@register_node("RCSendLatent", "Remote Call: Send Latent")
class _:
CATEGORY = "jamesWalker55/rc"
INPUT_TYPES = lambda: {
"required": {
"key": (
"STRING",
{"default": "input_latent", "multiline": False},
),
"latent": ("LATENT",),
}
}
FUNCTION = "execute"
RETURN_TYPES = ()
OUTPUT_NODE = True
def execute(self, key: str, latent: str):
blob = torch_save_to_blob(latent["samples"])
return {
"ui": {
"jw_rc": (
{
"type": "latent",
"value": blob.decode(),
},
),
}
}