Dervlex commited on
Commit
7da4861
·
verified ·
1 Parent(s): bc6ae75

Upload sdxl_embedding_bigasp.py

Browse files
Files changed (1) hide show
  1. sdxl_embedding_bigasp.py +66 -0
sdxl_embedding_bigasp.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # custom_nodes/bigasp_embed_node.py
2
+
3
+ import torch
4
+ from diffusers import StableDiffusionPipeline
5
+ from transformers import CLIPTokenizer, CLIPTextModel
6
+
7
+ class BigASP_Embeddings:
8
+ @classmethod
9
+ def INPUT_TYPES(cls):
10
+ return {
11
+ "required": {
12
+ "model_path": ("STRING", {"default": "silveroxides/big-asp-v2"}),
13
+ "prompt": ("STRING", {"multiline": True}),
14
+ "neg_prompt": ("STRING", {"multiline": True}),
15
+ }
16
+ }
17
+
18
+ RETURN_TYPES = ("CONDITIONING", "CONDITIONING")
19
+ FUNCTION = "run"
20
+ CATEGORY = "embedding/BigASP"
21
+
22
+ _PIPELINE_CACHE = {}
23
+
24
+ def run(self, model_path, prompt, neg_prompt):
25
+ # Load or reuse pipeline
26
+ pipe = self._PIPELINE_CACHE.get(model_path)
27
+ if pipe is None:
28
+ print(f"Loading Big-ASP model from: {model_path}")
29
+ pipe = StableDiffusionPipeline.from_pretrained(
30
+ model_path,
31
+ torch_dtype=torch.float16,
32
+ use_safetensors=True
33
+ ).to("cuda")
34
+ pipe.safety_checker = None # Optional
35
+ self._PIPELINE_CACHE[model_path] = pipe
36
+
37
+ device = pipe.device # usually 'cuda'
38
+
39
+ # Tokenize and move inputs to GPU
40
+ text_input = pipe.tokenizer(
41
+ prompt, padding="max_length", max_length=pipe.tokenizer.model_max_length, return_tensors="pt"
42
+ )
43
+ neg_text_input = pipe.tokenizer(
44
+ neg_prompt, padding="max_length", max_length=pipe.tokenizer.model_max_length, return_tensors="pt"
45
+ )
46
+
47
+ text_input = {k: v.to(device) for k, v in text_input.items()}
48
+ neg_text_input = {k: v.to(device) for k, v in neg_text_input.items()}
49
+
50
+ with torch.no_grad():
51
+ prompt_embeds = pipe.text_encoder(**text_input).last_hidden_state
52
+ neg_prompt_embeds = pipe.text_encoder(**neg_text_input).last_hidden_state
53
+
54
+ # Move embeddings to CPU if needed by ComfyUI
55
+ positive = [[prompt_embeds.to("cpu"), {}]]
56
+ negative = [[neg_prompt_embeds.to("cpu"), {}]]
57
+
58
+ return (positive, negative)
59
+
60
+
61
+ NODE_CLASS_MAPPINGS = {
62
+ "BigASP_Embeddings": BigASP_Embeddings
63
+ }
64
+ NODE_DISPLAY_NAME_MAPPINGS = {
65
+ "BigASP_Embeddings": "BigASP Prompt Embeddings"
66
+ }