Models / juggernaut_embedding_node.py
Dervlex's picture
Upload juggernaut_embedding_node.py
91d25a6 verified
Raw History Blame Contribute Delete
2.7 kB
# custom_nodes/sdxl_embed_node.py
import torch
import numpy as np
from diffusers import StableDiffusionXLPipeline
from sd_embed.embedding_funcs import get_weighted_text_embeddings_sdxl
class WeightedSDXLEmbeddings:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model_path": ("STRING", {"default":"RunDiffusion/Juggernaut-XI-v11"}),
"prompt": ("STRING", {"multiline":True}),
"neg_prompt": ("STRING", {"multiline":True}),
}
}
RETURN_TYPES = ("CONDITIONING","CONDITIONING")
FUNCTION = "run"
CATEGORY = "embedding/SDXL"
# cache so we don’t reload every batch
_PIPELINE_CACHE = {}
import torch
import numpy as np
from diffusers import StableDiffusionXLPipeline
from sd_embed.embedding_funcs import get_weighted_text_embeddings_sdxl
class WeightedSDXLEmbeddings:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model_path": ("STRING", {"default":"RunDiffusion/Juggernaut-XI-v11"}),
"prompt": ("STRING", {"multiline":True}),
"neg_prompt": ("STRING", {"multiline":True}),
}
}
RETURN_TYPES = ("CONDITIONING","CONDITIONING")
FUNCTION = "run"
CATEGORY = "embedding/SDXL"
# cache so we don't reload every batch
_PIPELINE_CACHE = {}
def run(self, model_path, prompt, neg_prompt):
# load or re-use the SDXL pipeline
pipe = self._PIPELINE_CACHE.get(model_path)
if pipe is None:
pipe = StableDiffusionXLPipeline.from_pretrained(
model_path,
torch_dtype=torch.float16
).to("cuda")
self._PIPELINE_CACHE[model_path] = pipe
# get the 4 embeddings (all torch.Tensor on cuda)
pe, ne, pooled_pe, pooled_ne = get_weighted_text_embeddings_sdxl(
pipe, prompt=prompt, neg_prompt=neg_prompt
)
# Keep as tensors instead of converting to NumPy
# Just move to CPU if needed for ComfyUI
pe_tensor = pe.detach().cpu()
pooled_pe_tensor = pooled_pe.detach().cpu()
ne_tensor = ne.detach().cpu()
pooled_ne_tensor = pooled_ne.detach().cpu()
# Format correctly for ComfyUI
positive = [[pe_tensor, {"pooled_output": pooled_pe_tensor}]]
negative = [[ne_tensor, {"pooled_output": pooled_ne_tensor}]]
return (positive, negative)
NODE_CLASS_MAPPINGS = {
"WeightedSDXLEmbeddings": WeightedSDXLEmbeddings
}
NODE_DISPLAY_NAME_MAPPINGS = {
"WeightedSDXLEmbeddings": "Weighted SDXL Embeddings"
}