-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnodes.py
857 lines (727 loc) · 29.1 KB
/
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
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
import os
import re
from glob import glob
import torch
from torchvision.transforms import InterpolationMode
import torchvision.transforms.v2.functional as F
from llama_cpp import Llama
from scipy import ndimage
import numpy as np
import node_helpers
import folder_paths
import comfy
class PadAndResize:
@classmethod
def INPUT_TYPES(cls):
return {
'required': {
'image': ('IMAGE',),
'latent': ('LATENT',),
'mode': (['resize', 'crop', 'fit'],),
'left': ('FLOAT', {'min': 0, 'step': .01, 'default': 0}),
'top': ('FLOAT', {'min': 0, 'step': .01, 'default': 0}),
'right': ('FLOAT', {'min': 0, 'step': .01, 'default': 0}),
'bottom': ('FLOAT', {'min': 0, 'step': .01, 'default': 0}),
}
}
RETURN_TYPES = ('IMAGE',)
FUNCTION = 'run'
CATEGORY = 'davcha'
def run(self, image, latent, mode, left, top, right, bottom):
image = image.permute(0,3,1,2)
image = self.pad(image, left, top, right, bottom)
image = self.resize(image, latent, mode)
image = image.permute(0,2,3,1)
return (image,)
def resize(self, image, latent, mode):
b, c, sh, sw = image.shape
_, _, th, tw = latent['samples'].shape
th, tw = th * 8, tw * 8
if (th, tw) == (sh, sw):
return image
if mode == 'resize':
result = F.resize(image, (th, tw), InterpolationMode.NEAREST_EXACT)
else:
fun = max if mode == 'crop' else min
scale = fun(torch.tensor((th/sh,tw/sw)))
shape = tuple(int(x * scale) for x in [sh,sw])
mask = torch.ones((1, sh, sw))
mask = F.resize(mask, shape, InterpolationMode.NEAREST_EXACT)
result = F.resize(image, shape, InterpolationMode.NEAREST_EXACT)
phw = (
(tw - int(sw*scale))//2,
(th - int(sh*scale))//2,
(tw - int(sw*scale)+1)//2,
(th - int(sh*scale)+1)//2,
)
mask = F.pad(mask, phw, fill=0, padding_mode='constant')
dist = ndimage.distance_transform_edt(mask)
soft_m = np.minimum(dist / 32, 1)
soft_m = torch.from_numpy(soft_m).type(torch.float32)
mask = soft_m
result = F.pad(result, phw, fill=0.5, padding_mode='edge')
if c != 4:
result = torch.cat((result, mask.unsqueeze(1)), 1)
else:
result[:,3,:,:] *= mask
return result
def pad(self, image, left, top, right, bottom):
b, c, h, w = image.shape
left, right = [int(x * w) for x in [left, right]]
top, bottom = [int(x * h) for x in [top, bottom]]
mask = torch.ones(b, h, w, dtype=torch.float32)
mask = F.pad(mask, (left, top, right, bottom), 0, padding_mode='constant')
dist = ndimage.distance_transform_edt(mask)
soft_m = np.minimum(dist / 32, 1)
soft_m = torch.from_numpy(soft_m).type(torch.float32)
mask = soft_m
image = F.pad(image, (left, top, right, bottom), 0.5, padding_mode='edge')
if c != 4:
image = torch.cat((image, mask.unsqueeze(1)), 1)
else:
image[:,3,...] *= mask
return image
class PercentPadding:
@classmethod
def INPUT_TYPES(cls):
return {
'required': {
'image': ('IMAGE',),
'left': ('FLOAT', {'min': 0, 'step': .01, 'default': 0}),
'top': ('FLOAT', {'min': 0, 'step': .01, 'default': 0}),
'right': ('FLOAT', {'min': 0, 'step': .01, 'default': 0}),
'bottom': ('FLOAT', {'min': 0, 'step': .01, 'default': 0}),
}
}
RETURN_TYPES = ('IMAGE',)
FUNCTION = 'run'
CATEGORY = 'davcha'
def run(self, image, left, top, right, bottom):
image = image.permute(0,3,1,2)
b, c, h, w = image.shape
left, right = [int(x * w) for x in [left, right]]
top, bottom = [int(x * h) for x in [top, bottom]]
mask = torch.ones(b, h, w, dtype=torch.float32)
mask = F.pad(mask, (left, top, right, bottom), 0, padding_mode='constant')
dist = ndimage.distance_transform_edt(mask)
soft_m = np.minimum(dist / 32, 1)
soft_m = torch.from_numpy(soft_m).type(torch.float32)
mask = soft_m
image = F.pad(image, (left, top, right, bottom), 0.5, padding_mode='constant')
if c != 4:
image = torch.cat((image, mask.unsqueeze(1)), 1)
else:
image[:,3,...] *= mask
image = image.permute(0,2,3,1)
return (image, )
class StringScheduleHelper:
@classmethod
def INPUT_TYPES(cls):
return {
'required': {
'text': ('STRING', {'multiline': True, 'dynamicPrompts': True}),
}
}
RETURN_NAMES = ('base_prompt', 'schedule', 'has_schedule')
RETURN_TYPES = ('STRING', 'STRING', 'BOOLEAN')
FUNCTION = 'run'
CATEGORY = 'davcha'
def run(self, text):
base = []
sched = []
for a,b,c in re.findall(r"(\d+):(.+)|(.+)", text):
if a == '':
base.append(c.strip())
else:
sched.append(f'"{a}": "{b.strip()}"')
base_prompt = '\n'.join(base)
schedule = ',\n'.join(sched)
has_schedule = len(sched) > 0
if not has_schedule:
schedule = '"0": ""'
return (base_prompt, schedule, has_schedule)
class SmartMask:
@classmethod
def INPUT_TYPES(cls):
return {
'required': {
'mask': ('MASK',),
}
}
RETURN_TYPES = ('MASK',)
FUNCTION = 'mask'
CATEGORY = 'davcha'
def mask(self, mask):
if torch.sum(mask) > 0:
return (mask, )
else:
return (1 - mask, )
class ResizeCropFit:
RETURN_TYPES = ('IMAGE',)
FUNCTION = 'run'
CATEGORY = 'davcha'
@classmethod
def INPUT_TYPES(cls):
return {
'required': {
'image': ('IMAGE',),
'latent': ('LATENT',),
'mode': (['resize', 'crop', 'fit'],)
}
}
def run(self, image, latent, mode):
image = image.permute(0,3,1,2)
b, c, sh, sw = image.shape
_, _, th, tw = latent['samples'].shape
th, tw = th * 8, tw * 8
if (th, tw) == (sh, sw):
return (image.permute(0,2,3,1), )
if mode == 'resize':
result = F.resize(image, (th, tw), InterpolationMode.NEAREST_EXACT)
else:
fun = max if mode == 'crop' else min
scale = fun(torch.tensor((th/sh,tw/sw)))
shape = tuple(int(x * scale) for x in [sh,sw])
mask = torch.ones((1, sh, sw))
mask = F.resize(mask, shape, InterpolationMode.NEAREST_EXACT)
result = F.resize(image, shape, InterpolationMode.NEAREST_EXACT)
phw = (
(tw - int(sw*scale))//2,
(th - int(sh*scale))//2,
(tw - int(sw*scale)+1)//2,
(th - int(sh*scale)+1)//2,
)
mask = F.pad(mask, phw, fill=0, padding_mode='constant')
dist = ndimage.distance_transform_edt(mask)
soft_m = np.minimum(dist / 32, 1)
soft_m = torch.from_numpy(soft_m).type(torch.float32)
mask = soft_m
result = F.pad(result, phw, fill=0.5, padding_mode='constant')
if c != 4:
result = torch.cat((result, mask.unsqueeze(1)), 1)
# result[:,3,:,:] = 1
else:
result[:,3,:,:] *= mask
result = result.permute(0,2,3,1)
return (result, )
class SoftErosion:
RETURN_TYPES = ('MASK',)
FUNCTION = 'run'
CATEGORY = 'davcha'
@classmethod
def INPUT_TYPES(cls):
return {
'required': {
'mask': ('MASK',),
'size': ('INT', {'min': 0, 'default': 5}),
}
}
def run(self, mask, size):
if size == 0:
return (mask, )
soft_mask = []
for m in mask:
dist = ndimage.distance_transform_edt(m)
soft_m = np.minimum(dist / size, 1)
soft_m = torch.from_numpy(soft_m)
soft_mask.append(soft_m)
soft_mask = torch.from_numpy(np.array(soft_mask)).type(torch.FloatTensor)
return (soft_mask, )
class ApplyMask:
RETURN_TYPES = ('IMAGE',)
FUNCTION = 'run'
CATEGORY = 'davcha'
@classmethod
def INPUT_TYPES(cls):
return {
'required': {
'image': ('IMAGE',),
'mask': ('MASK',),
}
}
def run(self, image, mask):
image = image.permute(0,3,1,2)
_, _, h, w = image.shape
mask = F.resize(mask, (h, w), InterpolationMode.BICUBIC)
image = image.permute(0,2,3,1)[:,:,:,:3]
image *= mask[...,None]
return (image, )
class DStack:
RETURN_TYPES = ('IMAGE',)
FUNCTION = 'run'
CATEGORY = 'davcha'
@classmethod
def INPUT_TYPES(cls):
return {
'required': {
'image1': ('IMAGE',),
'image2': ('IMAGE',),
}
}
def run(self, image1, image2):
stack = torch.dstack((image1, image2))
return (stack, )
ratio_merge = ("FLOAT", {"default": 1.0, "min": -5.0, "max": 6.0, "step": 0.001})
class DavchaConditioningConcat:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"conditioning_to": ("CONDITIONING",),
"conditioning_from": ("CONDITIONING",),
}}
RETURN_TYPES = ("CONDITIONING",)
FUNCTION = "concat"
CATEGORY = "conditioning"
def concat(self, conditioning_to, conditioning_from):
out = []
if len(conditioning_from) < 1: return (conditioning_to,)
if len(conditioning_to) < 1: return (conditioning_from,)
cond_from = conditioning_from[0][0].to(conditioning_to[0][0].device)
for i in range(len(conditioning_to)):
t1 = conditioning_to[i][0]
mb = max(t1.shape[0], cond_from.shape[0])
tw = torch.cat((t1.repeat(mb // t1.shape[0],1,1), cond_from.repeat(mb // cond_from.shape[0],1,1)), 1)
n = [tw, conditioning_to[i][1].copy()]
out.append(n)
return (out, )
class DavchaModelMergeSimple:
@classmethod
def INPUT_TYPES(s):
return {"required": { "model1": ("MODEL",),
"model2": ("MODEL",),
"ratio": ratio_merge,
}}
RETURN_TYPES = ("MODEL",)
FUNCTION = "merge"
CATEGORY = "advanced/model_merging"
def merge(self, model1, model2, ratio):
m = model1.clone()
kp = model2.get_key_patches("diffusion_model.")
for k in kp:
m.add_patches({k: kp[k]}, 1.0 - ratio, ratio)
return (m, )
class DavchaCLIPMergeSimple:
@classmethod
def INPUT_TYPES(s):
return {"required": { "clip1": ("CLIP",),
"clip2": ("CLIP",),
"ratio": ratio_merge,
}}
RETURN_TYPES = ("CLIP",)
FUNCTION = "merge"
CATEGORY = "advanced/model_merging"
def merge(self, clip1, clip2, ratio):
m = clip1.clone()
kp = clip2.get_key_patches()
for k in kp:
if k.endswith(".position_ids") or k.endswith(".logit_scale"):
continue
m.add_patches({k: kp[k]}, 1.0 - ratio, ratio)
return (m, )
import comfy_extras.nodes_model_merging
class DavchaModelMergeSD1(comfy_extras.nodes_model_merging.ModelMergeBlocks):
CATEGORY = "advanced/model_merging/model_specific"
@classmethod
def INPUT_TYPES(s):
arg_dict = { "model1": ("MODEL",),
"model2": ("MODEL",)}
argument = ratio_merge
arg_dict["time_embed."] = argument
arg_dict["label_emb."] = argument
for i in range(12):
arg_dict["input_blocks.{}.".format(i)] = argument
for i in range(3):
arg_dict["middle_block.{}.".format(i)] = argument
for i in range(12):
arg_dict["output_blocks.{}.".format(i)] = argument
arg_dict["out."] = argument
return {"required": arg_dict}
class DavchaModelMergeSDXL(comfy_extras.nodes_model_merging.ModelMergeBlocks):
CATEGORY = "advanced/model_merging/model_specific"
@classmethod
def INPUT_TYPES(s):
arg_dict = { "model1": ("MODEL",),
"model2": ("MODEL",)}
argument = ratio_merge
arg_dict["time_embed."] = argument
arg_dict["label_emb."] = argument
for i in range(9):
arg_dict["input_blocks.{}".format(i)] = argument
for i in range(3):
arg_dict["middle_block.{}".format(i)] = argument
for i in range(9):
arg_dict["output_blocks.{}".format(i)] = argument
arg_dict["out."] = argument
return {"required": arg_dict}
class ConditioningCompress:
CATEGORY = "davcha"
@classmethod
def INPUT_TYPES(s):
return {"required": {
"conditioning": ("CONDITIONING",),
"max_tokens": ("INT", {"default": 75, "min": 1, "step": 1})
}}
RETURN_TYPES = ("CONDITIONING",)
FUNCTION = "run"
def run(self, conditioning, max_tokens):
def reduc(o):
u,s,v = torch.svd(o)
return (u[0,:max_tokens,:max_tokens]@s[0,:max_tokens].diag()@v[0,:,:max_tokens].T)[None]
out = [[c[0].clone(), c[1]] for c in conditioning]
for o in out:
o[0] = reduc(o[0])
return (out, )
################ DavchaCLIPTextEncode
import regex
from itertools import product
def parse(text):
def _inner(text, start=0, end=1):
matches = regex.findall(r'(\[((?:(?:embedding:)?[^\[\]])*?):((?:(?:embedding:)?[^\[\]])*?):(\d*\.?\d+)\])', text, regex.DOTALL)
if len(matches) == 0:
return [(text, (start, end))]
x = [m[1:3] for m in matches]
prod = list(product(*x))
results = {}
for p in prod:
if p not in results: results[p] = []
for f, b, a, t in matches:
t = float(t)
if b in p:
results[p].append((f, b, (start, t)))
if a in p:
results[p].append((f, a, (t, end)))
final = []
for items in results.values():
txt = text
st, en = start, end
for f, r, (s, e) in items:
txt = txt.replace(f, r)
st = max(st, s)
en = min(en, e)
if st <= en:
for prompt, (a, b) in _inner(txt, st, en):
final.append((prompt, (a, b)))
return final
candidates = _inner(text)
dic = {}
for prompt, (s, e) in candidates:
if prompt not in dic:
dic[prompt] = (s, e)
else:
dic[prompt] = (min(s, dic[prompt][0]), max(e, dic[prompt][1]))
return [(prompt, (s,e)) for prompt, (s, e) in dic.items()]
class DavchaCLIPTextEncode:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"text": ("STRING", {"multiline": True, "dynamicPrompts": True, "tooltip": "The text to be encoded."}),
"clip": ("CLIP", {"tooltip": "The CLIP model used for encoding the text."})
}
}
RETURN_TYPES = ("CONDITIONING",)
OUTPUT_IS_LIST = (True,)
FUNCTION = "run"
CATEGORY = "davcha"
def run(self, clip, text):
text = text.split('|')
result = []
for txt in text:
texts = re.split(r"\b(AREA\(\s*(?:\d*\.?\d+\s*){4,5}\))", txt)
areas = []
current_area = None
for item in texts:
if item.startswith('AREA'):
current_area = item
else:
prompt = parse(item)
if current_area is None:
areas.append((prompt, None))
else:
values = re.findall(r"(\d*\.?\d+)", current_area)
x, y, w, h, s = [float(i) for i in values + [1.0] * (5 - len(values))]
areas.append((prompt, (x, y, w, h, s)))
current_area = None
for schedule, area in areas:
for prompt, (start, end) in schedule:
print(f'{area} {start}-{end}: {prompt}')
cs = []
for prompts, area in areas:
for txt, (lb, ub) in prompts:
if len(txt) > 0:
tokens = clip.tokenize(txt)
output = clip.encode_from_tokens(tokens, return_pooled=True, return_dict=True)
cond = output.pop("cond")
c = [[cond, output]]
c = node_helpers.conditioning_set_values(c, {"start_percent": lb,
"end_percent": ub})
if area is not None:
x, y, w, h, s = area
c = node_helpers.conditioning_set_values(c, {"area": ("percentage", h, w, y, x),
"strength": s,
"set_area_to_bounds": False})
cs += c
result.append(cs)
return (result, )
from pathlib import Path
from webp import WebPData, WebPAnimDecoderOptions, WebPAnimDecoder, mimread
def video_reader(file, start=0, count=0, target_fps=0):
if Path(file).suffix.lower() == '.webp':
return _webp_reader(file, start, count, target_fps)
else:
return _ffmpeg_reader(file, start, count, target_fps)
def _webp_reader(file, start, count, target_fps):
with open(file, 'rb') as f:
webp_data = WebPData.from_buffer(f.read())
dec_opts = WebPAnimDecoderOptions.new(use_threads=True)
dec = WebPAnimDecoder.new(webp_data, dec_opts)
eps = 1e-7
frames_data = list(dec.frames())
frames = [arr[:,:,:3] for arr, _ in frames_data]
fps = 1000 * len(frames_data) / frames_data[-1][1]
if target_fps > 0:
if fps > target_fps:
frames = [frame[:,:,:3] for frame in mimread(file, fps=target_fps)]
else:
target_fps = fps
if count > 0:
frames = frames[int(start):int(start+count)]
else:
frames = frames[int(start):]
frames = torch.from_numpy(np.array(frames)).float() / 255.0
return frames, target_fps
import cv2
import numpy as np
def _ffmpeg_reader(file, start, count, target_fps):
cap = cv2.VideoCapture(file)
# Get video properties
fps = int(cap.get(cv2.CAP_PROP_FPS))
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
if target_fps == 0:
target_fps = fps
start_frame = int(min(max(0, start), frame_count - 1) * fps / target_fps)
if count == 0:
count = int(frame_count * target_fps / fps)
count = min(int(frame_count * target_fps / fps - start), count)
frames = np.empty((count, frame_height, frame_width, 3), dtype=np.uint8)
for i in range(count):
cap.set(cv2.CAP_PROP_POS_FRAMES, int(start_frame + i * fps / target_fps))
ret, frame = cap.read()
if not ret:
break
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frames[i] = frame_rgb
cap.release()
frames = torch.from_numpy(frames).float() / 255.0
return frames, target_fps
class DavchaLoadVideo:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"path": ("STRING", {"multiline": False}),
"start": ("INT", {"default": 0, "min": 0, "step": 1}),
"count": ("INT", {"default": 0, "min": 0, "step": 1}),
"target_fps": ("FLOAT", {"default": 0, "min": 0, "max": 60.0, "step": 0.01}),
}
}
RETURN_TYPES = ("IMAGE", "INT", "FLOAT")
RETURN_NAMES = ("frames", "frame_count", "fps")
FUNCTION = "run"
CATEGORY = "davcha"
def run(self, path, start, count, target_fps):
fullpath = os.path.join(folder_paths.get_input_directory(), path)
frames, fps = video_reader(fullpath, start, count, target_fps)
return (frames, len(frames), fps)
MAX_RESOLUTION = 16384
class AnyType(str):
def __eq__(self, _) -> bool:
return True
def __ne__(self, __value: object) -> bool:
return False
class DavchaEmptyLatentImage:
def __init__(self):
self.device = comfy.model_management.intermediate_device()
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"width": ("INT", {"default": 512, "min": 16, "max": MAX_RESOLUTION, "step": 8, "tooltip": "The width of the latent images in pixels."}),
"height": ("INT", {"default": 512, "min": 16, "max": MAX_RESOLUTION, "step": 8, "tooltip": "The height of the latent images in pixels."}),
"option": ([AnyType("*")], ),
"batch_size": ("INT", {"default": 1, "min": 1, "max": 4096, "tooltip": "The number of latent images in the batch."})
}
}
RETURN_TYPES = ("LATENT", "FLOAT", "INT")
RETURN_NAMES = ("latent", "upscale_factor", "batch_size")
OUTPUT_TOOLTIPS = ("The empty latent image batch.",)
FUNCTION = "generate"
CATEGORY = "davcha"
DESCRIPTION = "Create a new batch of empty latent images to be denoised via sampling."
def generate(self, width, height, option, batch_size=1):
width, height, upscale_factor = re.findall(r"(\d+)x(\d+): (\d+\.?\d*)", option)[0]
width, height, upscale_factor = int(width), int(height), float(upscale_factor)
latent = torch.zeros([batch_size, 4, height // 8, width // 8], device=self.device)
return ({"samples":latent}, upscale_factor, batch_size)
class DavchaMaskImage:
@classmethod
def INPUT_TYPES(s):
return {"required": { "image": ("IMAGE",),
"mask": ("MASK",),
}}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "run"
CATEGORY = "davcha"
def run(self, image, mask):
b, w, h, c = image.shape
mask = F.resize(mask, (w, h), InterpolationMode.BICUBIC)
image = image.permute(0, 3, 1, 2)
if c != 4:
image = torch.cat((image, torch.ones((b, 1, w, h), dtype=torch.float32)), 1)# mask.unsqueeze(1)), 1)
image = image * mask.unsqueeze(1)
image = image.permute(0, 2, 3, 1)
return (image, )
class DavchaPop:
@classmethod
def INPUT_TYPES(s):
return {"required": { "items": (AnyType("*"),),
}}
INPUT_IS_LIST = (True, )
OUTPUT_IS_LIST = (False, True)
RETURN_TYPES = (AnyType("*"), AnyType("*"))
FUNCTION = "run"
CATEGORY = "davcha"
def run(self, items):
return (items[0], items[1:])
gguf_folder = os.path.join(folder_paths.models_dir, "llm_gguf")
if os.path.isdir(gguf_folder):
gguf_files = [file for file in os.listdir(gguf_folder) if file.endswith('.gguf')]
else:
gguf_files = []
class DavchaLoadLLM:
@classmethod
def INPUT_TYPES(s):
return {'required':{
'modelname': (gguf_files,),
}}
RETURN_NAMES = ('model',)
RETURN_TYPES = ('DavchaLLModel',)
FUNCTION = "run"
CATEGORY = "davcha"
def run(self, modelname):
model_path = os.path.join(gguf_folder, modelname)
model = Llama(
model_path=model_path,
n_gpu_layers=-1,
verbose=False,
n_ctx=2048,
)
return (model,)
class DavchaLLMAdvanced:
@classmethod
def INPUT_TYPES(s):
return {'required':{
'model': ('DavchaLLModel',),
'seed': ('INT', {'default': 0, 'min': 0, 'max': 0xffffffffffffffff}),
'system': ('STRING', {'multiline': True, 'dynamicPrompts': True}),
'text': ('STRING', {'multiline': True, 'dynamicPrompts': True}),
'max_tokens': ('INT', {'default': 512, 'min': 1, 'max': 8192}),
'temperature': ('FLOAT', {'default': 1.0, 'min': 0, 'max': 1.0, 'step': 0.1}),
'top_p': ('FLOAT', {'default': 0.9, 'min': 0, 'max': 1.0, 'step': 0.1}),
'top_k': ('INT', {'default': 50, 'min': 0, 'max': 100}),
'repeat_penalty': ('FLOAT', {'default': 1.2, 'min': 0, 'max': 5.0, 'step': 0.1}),
}}
RETURN_NAMES = ('text',)
RETURN_TYPES = ('STRING',)
FUNCTION = "run"
CATEGORY = "davcha"
def run(self, model, seed, system, text, max_tokens, temperature, top_p, top_k, repeat_penalty):
generate_kwargs = {
'max_tokens': max_tokens,
'temperature': temperature,
'top_p': top_p,
'top_k': top_k,
'repeat_penalty': repeat_penalty
}
msgs = []
if len(system.strip()) > 0:
msgs += [{'role': 'system', 'content': system}]
msgs += [{"role": "user", "content": text}]
model.set_seed(seed)
llm_result = model.create_chat_completion(msgs, **generate_kwargs)
return (llm_result['choices'][0]['message']['content'].strip(),)
class DavchaLLM:
@classmethod
def INPUT_TYPES(s):
return {'required':{
'model': ('DavchaLLModel',),
'seed': ('INT', {'default': 0, 'min': 0, 'max': 0xffffffffffffffff}),
'text': ('STRING', {'multiline': True, 'dynamicPrompts': True}),
}}
RETURN_NAMES = ('text',)
RETURN_TYPES = ('STRING',)
FUNCTION = "run"
CATEGORY = "davcha"
def run(self, model, seed, text):
generate_kwargs = {
'max_tokens': 512,
'temperature': 1.0,
'top_p': 0.9,
'top_k': 50,
'repeat_penalty': 1.2
}
msgs = []
msgs += [{"role": "user", "content": text}]
model.set_seed(seed)
llm_result = model.create_chat_completion(msgs, **generate_kwargs)
return (llm_result['choices'][0]['message']['content'].strip(),)
NODE_CLASS_MAPPINGS = {
'DavchaLoadLLM': DavchaLoadLLM,
'DavchaLLM': DavchaLLM,
'DavchaLLMAdvanced': DavchaLLMAdvanced,
'PadAndResize': PadAndResize,
'SmartMask': SmartMask,
'ResizeCropFit': ResizeCropFit,
'PercentPadding': PercentPadding,
'SoftErosion': SoftErosion,
'ApplyMask': ApplyMask,
'StringScheduleHelper': StringScheduleHelper,
'DStack': DStack,
'DavchaConditioningConcat': DavchaConditioningConcat,
'DavchaModelMergeSimple' : DavchaModelMergeSimple,
'DavchaCLIPMergeSimple' : DavchaCLIPMergeSimple,
'DavchaModelMergeSD1' : DavchaModelMergeSD1,
'DavchaModelMergeSDXL' : DavchaModelMergeSDXL,
'ConditioningCompress': ConditioningCompress,
'DavchaCLIPTextEncode': DavchaCLIPTextEncode,
'DavchaLoadVideo': DavchaLoadVideo,
'DavchaEmptyLatentImage': DavchaEmptyLatentImage,
'DavchaMaskImage': DavchaMaskImage,
'DavchaPop': DavchaPop,
}
NODE_DISPLAY_NAME_MAPPINGS = {
'DavchaLoadLLM': 'DavchaLoadLLM',
'DavchaLLM': 'DavchaLLM',
'DavchaLLMAdvanced': 'DavchaLLMAdvanced',
'PadAndResize': 'PadAndResize',
'SmartMask': 'SmartMask',
'ResizeCropFit': 'Resize, Crop or Fit',
'PercentPadding': 'Percent Padding',
'SoftErosion': 'SoftErosion',
'ApplyMask': 'ApplyMask',
'StringScheduleHelper': 'StringScheduleHelper',
'DStack': 'DStack',
'DavchaConditioningConcat': 'DavchaConditioningConcat',
'DavchaModelMergeSimple': 'DavchaModelMergeSimple',
'DavchaCLIPMergeSimple': 'DavchaCLIPMergeSimple',
'DavchaModelMergeSD1': 'DavchaModelMergeSD1',
'DavchaModelMergeSDXL': 'DavchaModelMergeSDXL',
'ConditioningCompress': 'ConditioningCompress',
'DavchaCLIPTextEncode': 'CLIP Text Encode (Davcha)',
'DavchaLoadVideo': 'Load Video (Davcha)',
'DavchaEmptyLatentImage': 'Empty Latent Image (Davcha)',
'DavchaMaskImage': 'Mask Image (Davcha)',
'DavchaPop': 'Pop',
}