-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample.py
More file actions
366 lines (309 loc) · 10 KB
/
example.py
File metadata and controls
366 lines (309 loc) · 10 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
import os
from typing import Tuple
import comfy.sd
import comfy.utils
import torch
import torch.nn.functional as F
from comfy.sd import CLIP
from diffusers import ConsistencyDecoderVAE
from folder_paths import get_folder_paths
from huggingface_hub import hf_hub_download
from torch import Tensor
def find_or_create_cache():
cwd = os.getcwd()
if os.path.exists(os.path.join(cwd, "ComfyUI")):
cwd = os.path.join(cwd, "ComfyUI")
if os.path.exists(os.path.join(cwd, "models")):
cwd = os.path.join(cwd, "models")
if not os.path.exists(os.path.join(cwd, "huggingface_cache")):
print("Creating huggingface_cache directory within comfy")
os.mkdir(os.path.join(cwd, "huggingface_cache"))
return str(os.path.join(cwd, "huggingface_cache"))
class ConsistencyDecoder:
@classmethod
def INPUT_TYPES(s):
return {"required": {"latent": ("LATENT",)}}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "decode"
CATEGORY = "latent"
def __init__(self):
self.vae = (
ConsistencyDecoderVAE.from_pretrained(
"openai/consistency-decoder",
torch_dtype=torch.float16,
variant="fp16",
use_safetensors=True,
cache_dir=find_or_create_cache(),
)
.eval()
.to("cuda")
)
def _decode(self, latent):
"""Used when patching another vae."""
return self.vae.decode(latent.half().cuda()).sample
def decode(self, latent):
"""Used for standalone decoding."""
sample = self._decode(latent["samples"])
sample = sample.clamp(-1, 1).movedim(1, -1).add(1.0).mul(0.5).cpu()
return (sample,)
class PatchDecoderTiled:
@classmethod
def INPUT_TYPES(s):
return {"required": {"vae": ("VAE",)}}
RETURN_TYPES = ("VAE",)
FUNCTION = "patch"
category = "vae"
def __init__(self):
self.vae = ConsistencyDecoder()
def patch(self, vae):
del vae.first_stage_model.decoder
vae.first_stage_model.decode = self.vae._decode
vae.decode = (
lambda x: vae.decode_tiled_(
x,
tile_x=512,
tile_y=512,
overlap=64,
)
.to("cuda")
.movedim(1, -1)
)
return (vae,)
# quick node to set SDXL-friendly aspect ratios in 1024^2
# adapted from throttlekitty
class SDXLAspectRatio:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
}
}
RETURN_TYPES = ("INT", "INT")
RETURN_NAMES = ("width", "height")
FUNCTION = "run"
CATEGORY = "image"
def run(self, image: Tensor) -> Tuple[int, int]:
_, height, width, _ = image.shape
aspect_ratio = width / height
aspect_ratios = (
(1 / 1, 1024, 1024),
(2 / 3, 832, 1216),
(3 / 4, 896, 1152),
(5 / 8, 768, 1216),
(9 / 16, 768, 1344),
(9 / 19, 704, 1472),
(9 / 21, 640, 1536),
(3 / 2, 1216, 832),
(4 / 3, 1152, 896),
(8 / 5, 1216, 768),
(16 / 9, 1344, 768),
(19 / 9, 1472, 704),
(21 / 9, 1536, 640),
)
# find the closest aspect ratio
closest = min(aspect_ratios, key=lambda x: abs(x[0] - aspect_ratio))
return (closest[1], closest[2])
class ImageToMultipleOf:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"multiple_of": (
"INT",
{
"default": 64,
"min": 1,
"max": 256,
"step": 16,
"display": "number",
},
),
"method": (["center crop", "rescale"],),
}
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "run"
CATEGORY = "image"
def run(self, image: Tensor, multiple_of: int, method: str) -> Tuple[Tensor]:
"""Center crop the image to a specific multiple of a number."""
_, height, width, _ = image.shape
new_height = height - (height % multiple_of)
new_width = width - (width % multiple_of)
if method == "rescale":
return (
F.interpolate(
image.unsqueeze(0),
size=(new_height, new_width),
mode="bilinear",
align_corners=False,
).squeeze(0),
)
else:
top = (height - new_height) // 2
left = (width - new_width) // 2
bottom = top + new_height
right = left + new_width
return (image[:, top:bottom, left:right, :],)
class HFHubLoraLoader:
def __init__(self):
self.loaded_lora = None
self.loaded_lora_path = None
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model": ("MODEL",),
"clip": ("CLIP",),
"repo_id": ("STRING", {"default": ""}),
"subfolder": ("STRING", {"default": ""}),
"filename": ("STRING", {"default": ""}),
"strength_model": (
"FLOAT",
{"default": 1.0, "min": -20.0, "max": 20.0, "step": 0.01},
),
"strength_clip": (
"FLOAT",
{"default": 1.0, "min": -20.0, "max": 20.0, "step": 0.01},
),
}
}
RETURN_TYPES = ("MODEL", "CLIP")
FUNCTION = "load_lora"
CATEGORY = "loaders"
def load_lora(
self,
model,
clip,
repo_id: str,
subfolder: str,
filename: str,
strength_model: float,
strength_clip: float,
):
if strength_model == 0 and strength_clip == 0:
return (model, clip)
lora_path = hf_hub_download(
repo_id=repo_id.strip(),
subfolder=(
None
if subfolder is None or subfolder.strip() == ""
else subfolder.strip()
),
filename=filename.strip(),
cache_dir=find_or_create_cache(),
)
lora = None
if self.loaded_lora is not None:
if self.loaded_lora_path == lora_path:
lora = self.loaded_lora
else:
self.loaded_lora = None
self.loaded_lora_path = None
if lora is None:
lora = comfy.utils.load_torch_file(lora_path, safe_load=True)
self.loaded_lora = lora
self.loaded_lora_path = lora_path
model_lora, clip_lora = comfy.sd.load_lora_for_models(
model, clip, lora, strength_model, strength_clip
)
return (model_lora, clip_lora)
class HFHubEmbeddingLoader:
"""Load a text model embedding from Huggingface Hub.
The connected CLIP model is not manipulated."""
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"clip": ("CLIP",),
"repo_id": ("STRING", {"default": ""}),
"subfolder": ("STRING", {"default": ""}),
"filename": ("STRING", {"default": ""}),
}
}
RETURN_TYPES = ("CLIP",)
FUNCTION = "download_embedding"
CATEGORY = "n/a"
def download_embedding(
self,
clip: CLIP, # added to signify it's best put in between nodes
repo_id: str,
subfolder: str,
filename: str,
):
hf_hub_download(
repo_id=repo_id.strip(),
subfolder=(
None
if subfolder is None or subfolder.strip() == ""
else subfolder.strip()
),
filename=filename.strip(),
local_dir=get_folder_paths("embeddings")[0],
)
return (clip,)
class GlifVariable:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"variable": (
[
"",
],
),
"fallback": (
"STRING",
{
"default": "",
"single_line": True,
},
),
}
}
RETURN_TYPES = ("STRING", "INT", "FLOAT")
FUNCTION = "do_it"
CATEGORY = "glif/variables"
@classmethod
def VALIDATE_INPUTS(cls, variable: str, fallback: str):
# Since we populate dynamically, comfy will report invalid inputs. Override to always return True
return True
def do_it(self, variable: str, fallback: str):
variable = variable.strip()
fallback = fallback.strip()
if variable == "" or (variable.startswith("{") and variable.endswith("}")):
variable = fallback
int_val = 0
float_val = 0.0
string_val = f"{variable}"
try:
int_val = int(variable)
except Exception:
pass
try:
float_val = float(variable)
except Exception:
pass
return (string_val, int_val, float_val)
NODE_CLASS_MAPPINGS = {
"GlifConsistencyDecoder": ConsistencyDecoder,
"GlifPatchConsistencyDecoderTiled": PatchDecoderTiled,
"SDXLAspectRatio": SDXLAspectRatio,
"ImageToMultipleOf": ImageToMultipleOf,
"HFHubLoraLoader": HFHubLoraLoader,
"HFHubEmbeddingLoader": HFHubEmbeddingLoader,
"GlifVariable": GlifVariable,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"GlifConsistencyDecoder": "Consistency VAE Decoder",
"GlifPatchConsistencyDecoderTiled": "Patch Consistency VAE Decoder",
"SDXLAspectRatio": "Image to SDXL compatible WH",
"ImageToMultipleOf": "Image to Multiple of",
"HFHubLoraLoader": "Load HF Lora",
"HFHubEmbeddingLoader": "Load HF Embedding",
"GlifVariable": "Glif Variable",
}