Dervlex commited on
Commit
91d25a6
·
verified ·
1 Parent(s): c084b21

Upload juggernaut_embedding_node.py

Browse files
Files changed (1) hide show
  1. juggernaut_embedding_node.py +80 -0
juggernaut_embedding_node.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # custom_nodes/sdxl_embed_node.py
2
+
3
+ import torch
4
+ import numpy as np
5
+ from diffusers import StableDiffusionXLPipeline
6
+ from sd_embed.embedding_funcs import get_weighted_text_embeddings_sdxl
7
+
8
+ class WeightedSDXLEmbeddings:
9
+ @classmethod
10
+ def INPUT_TYPES(cls):
11
+ return {
12
+ "required": {
13
+ "model_path": ("STRING", {"default":"RunDiffusion/Juggernaut-XI-v11"}),
14
+ "prompt": ("STRING", {"multiline":True}),
15
+ "neg_prompt": ("STRING", {"multiline":True}),
16
+ }
17
+ }
18
+ RETURN_TYPES = ("CONDITIONING","CONDITIONING")
19
+ FUNCTION = "run"
20
+ CATEGORY = "embedding/SDXL"
21
+
22
+ # cache so we don’t reload every batch
23
+ _PIPELINE_CACHE = {}
24
+
25
+ import torch
26
+ import numpy as np
27
+ from diffusers import StableDiffusionXLPipeline
28
+ from sd_embed.embedding_funcs import get_weighted_text_embeddings_sdxl
29
+
30
+ class WeightedSDXLEmbeddings:
31
+ @classmethod
32
+ def INPUT_TYPES(cls):
33
+ return {
34
+ "required": {
35
+ "model_path": ("STRING", {"default":"RunDiffusion/Juggernaut-XI-v11"}),
36
+ "prompt": ("STRING", {"multiline":True}),
37
+ "neg_prompt": ("STRING", {"multiline":True}),
38
+ }
39
+ }
40
+ RETURN_TYPES = ("CONDITIONING","CONDITIONING")
41
+ FUNCTION = "run"
42
+ CATEGORY = "embedding/SDXL"
43
+ # cache so we don't reload every batch
44
+ _PIPELINE_CACHE = {}
45
+
46
+ def run(self, model_path, prompt, neg_prompt):
47
+ # load or re-use the SDXL pipeline
48
+ pipe = self._PIPELINE_CACHE.get(model_path)
49
+ if pipe is None:
50
+ pipe = StableDiffusionXLPipeline.from_pretrained(
51
+ model_path,
52
+ torch_dtype=torch.float16
53
+ ).to("cuda")
54
+ self._PIPELINE_CACHE[model_path] = pipe
55
+
56
+ # get the 4 embeddings (all torch.Tensor on cuda)
57
+ pe, ne, pooled_pe, pooled_ne = get_weighted_text_embeddings_sdxl(
58
+ pipe, prompt=prompt, neg_prompt=neg_prompt
59
+ )
60
+
61
+ # Keep as tensors instead of converting to NumPy
62
+ # Just move to CPU if needed for ComfyUI
63
+ pe_tensor = pe.detach().cpu()
64
+ pooled_pe_tensor = pooled_pe.detach().cpu()
65
+ ne_tensor = ne.detach().cpu()
66
+ pooled_ne_tensor = pooled_ne.detach().cpu()
67
+
68
+ # Format correctly for ComfyUI
69
+ positive = [[pe_tensor, {"pooled_output": pooled_pe_tensor}]]
70
+ negative = [[ne_tensor, {"pooled_output": pooled_ne_tensor}]]
71
+
72
+ return (positive, negative)
73
+
74
+
75
+ NODE_CLASS_MAPPINGS = {
76
+ "WeightedSDXLEmbeddings": WeightedSDXLEmbeddings
77
+ }
78
+ NODE_DISPLAY_NAME_MAPPINGS = {
79
+ "WeightedSDXLEmbeddings": "Weighted SDXL Embeddings"
80
+ }