File size: 2,692 Bytes
7f2508e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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":"fluently/Fluently-XL-Final"}),
                "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":"fluently/Fluently-XL-Final"}),
                "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"
}