Download sd3_embedding_node_new.py from Dervlex/Models: direct link, hf CLI and curl.
- Browser
- Download file 2.06 kB
-
https://huggingface.co/Dervlex/Models/resolve/main/sd3_embedding_node_new.py
- Command line
-
hf download hf://Dervlex/Models/sd3_embedding_node_new.py
-
curl -L -o sd3_embedding_node_new.py https://huggingface.co/Dervlex/Models/resolve/main/sd3_embedding_node_new.py
2.06 kB
| import torch | |
| from diffusers import StableDiffusion3Pipeline | |
| from sd_embed.embedding_funcs import get_weighted_text_embeddings_sd3 | |
| class WeightedSD35Embeddings: | |
| def INPUT_TYPES(cls): | |
| return { | |
| "required": { | |
| "model_path": ("STRING", {"default":"stabilityai/stable-diffusion-3.5-large"}), | |
| "prompt": ("STRING", {"multiline":True}), | |
| "neg_prompt": ("STRING", {"multiline":True}), | |
| } | |
| } | |
| RETURN_TYPES = ("CONDITIONING","CONDITIONING") | |
| FUNCTION = "run" | |
| CATEGORY = "embedding/SD35" | |
| _PIPELINE_CACHE = {} | |
| def run(self, model_path, prompt, neg_prompt): | |
| # load or re-use the SD3.5 pipeline | |
| pipe = self._PIPELINE_CACHE.get(model_path) | |
| if pipe is None: | |
| pipe = StableDiffusion3Pipeline.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) | |
| prompt_embeds, prompt_neg_embeds, pooled_prompt_embeds, negative_pooled_prompt_embeds = get_weighted_text_embeddings_sd3( | |
| pipe, prompt=prompt, neg_prompt=neg_prompt | |
| ) | |
| # Keep as tensors, just move to CPU if needed | |
| prompt_embeds_cpu = prompt_embeds.detach().cpu() | |
| pooled_prompt_embeds_cpu = pooled_prompt_embeds.detach().cpu() | |
| prompt_neg_embeds_cpu = prompt_neg_embeds.detach().cpu() | |
| negative_pooled_prompt_embeds_cpu = negative_pooled_prompt_embeds.detach().cpu() | |
| # Format for ComfyUI | |
| positive = [[prompt_embeds_cpu, {"pooled_output": pooled_prompt_embeds_cpu}]] | |
| negative = [[prompt_neg_embeds_cpu, {"pooled_output": negative_pooled_prompt_embeds_cpu}]] | |
| return (positive, negative) | |
| NODE_CLASS_MAPPINGS = { | |
| "WeightedSD35Embeddings": WeightedSD35Embeddings | |
| } | |
| NODE_DISPLAY_NAME_MAPPINGS = { | |
| "WeightedSD35Embeddings": "Weighted SD3.5 Embeddings" | |
| } | |