Dervlex commited on
Commit
9498c29
·
verified ·
1 Parent(s): b351c19

Upload sdxl_embedding_bigasp.py

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