Dervlex commited on
Commit
ab6cf66
·
verified ·
1 Parent(s): 1d59b3e

Upload sdxl_embedding_bigasp.py

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