File size: 2,260 Bytes
7da4861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# custom_nodes/bigasp_embed_node.py

import torch
from diffusers import StableDiffusionPipeline
from transformers import CLIPTokenizer, CLIPTextModel

class BigASP_Embeddings:
    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "model_path": ("STRING", {"default": "silveroxides/big-asp-v2"}),
                "prompt":     ("STRING", {"multiline": True}),
                "neg_prompt": ("STRING", {"multiline": True}),
            }
        }

    RETURN_TYPES = ("CONDITIONING", "CONDITIONING")
    FUNCTION     = "run"
    CATEGORY     = "embedding/BigASP"

    _PIPELINE_CACHE = {}

    def run(self, model_path, prompt, neg_prompt):
        # Load or reuse pipeline
        pipe = self._PIPELINE_CACHE.get(model_path)
        if pipe is None:
            print(f"Loading Big-ASP model from: {model_path}")
            pipe = StableDiffusionPipeline.from_pretrained(
                model_path,
                torch_dtype=torch.float16,
                use_safetensors=True
            ).to("cuda")
            pipe.safety_checker = None  # Optional
            self._PIPELINE_CACHE[model_path] = pipe

        device = pipe.device  # usually 'cuda'

        # Tokenize and move inputs to GPU
        text_input = pipe.tokenizer(
            prompt, padding="max_length", max_length=pipe.tokenizer.model_max_length, return_tensors="pt"
        )
        neg_text_input = pipe.tokenizer(
            neg_prompt, padding="max_length", max_length=pipe.tokenizer.model_max_length, return_tensors="pt"
        )

        text_input = {k: v.to(device) for k, v in text_input.items()}
        neg_text_input = {k: v.to(device) for k, v in neg_text_input.items()}

        with torch.no_grad():
            prompt_embeds = pipe.text_encoder(**text_input).last_hidden_state
            neg_prompt_embeds = pipe.text_encoder(**neg_text_input).last_hidden_state

        # Move embeddings to CPU if needed by ComfyUI
        positive = [[prompt_embeds.to("cpu"), {}]]
        negative = [[neg_prompt_embeds.to("cpu"), {}]]

        return (positive, negative)


NODE_CLASS_MAPPINGS = {
    "BigASP_Embeddings": BigASP_Embeddings
}
NODE_DISPLAY_NAME_MAPPINGS = {
    "BigASP_Embeddings": "BigASP Prompt Embeddings"
}