File size: 7,290 Bytes
cd1fb94 | 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | import node_helpers
import comfy.utils
import math
from typing_extensions import override
from comfy_api.latest import ComfyExtension, io
class TextEncodeQwenImageEdit(io.ComfyNode):
@classmethod
def define_schema(cls):
return io.Schema(
node_id="TextEncodeQwenImageEdit",
category="advanced/conditioning",
inputs=[
io.Clip.Input("clip"),
io.String.Input("prompt", multiline=True, dynamic_prompts=True),
io.Vae.Input("vae", optional=True),
io.Image.Input("image", optional=True),
],
outputs=[
io.Conditioning.Output(),
],
)
@classmethod
def execute(cls, clip, prompt, vae=None, image=None) -> io.NodeOutput:
ref_latent = None
if image is None:
images = []
else:
# Konsistente Bildverarbeitung - nur einmal skalieren
samples = image.movedim(-1, 1)
# Für Qwen-Image-Edit: Zielauflösung 1024x1024 (wie in der offiziellen Pipeline)
target_pixels = int(1024 * 1024)
current_pixels = samples.shape[3] * samples.shape[2]
if current_pixels != target_pixels:
scale_by = math.sqrt(target_pixels / current_pixels)
width = round(samples.shape[3] * scale_by)
height = round(samples.shape[2] * scale_by)
# Skalierung mit area interpolation für bessere Qualität
s = comfy.utils.common_upscale(samples, width, height, "area", "disabled")
image = s.movedim(1, -1)
else:
image = samples.movedim(1, -1)
images = [image[:, :, :, :3]]
# VAE-Encoding für reference latents
if vae is not None:
ref_latent = vae.encode(image[:, :, :, :3])
# Tokenisierung mit korrektem Template für Qwen-Image-Edit
tokens = clip.tokenize(prompt, images=images)
conditioning = clip.encode_from_tokens_scheduled(tokens)
# Reference latents hinzufügen (wie in der offiziellen Pipeline)
if ref_latent is not None:
conditioning = node_helpers.conditioning_set_values(conditioning, {"reference_latents": [ref_latent]}, append=True)
return io.NodeOutput(conditioning)
class TextEncodeQwenImageEditPlus(io.ComfyNode):
@classmethod
def define_schema(cls):
return io.Schema(
node_id="TextEncodeQwenImageEditPlus",
category="advanced/conditioning",
inputs=[
io.Clip.Input("clip"),
io.String.Input("prompt", multiline=True, dynamic_prompts=True),
io.Vae.Input("vae", optional=True),
io.Image.Input("image1", optional=True),
io.Image.Input("image2", optional=True),
io.Image.Input("image3", optional=True),
],
outputs=[
io.Conditioning.Output(),
],
)
@classmethod
def execute(cls, clip, prompt, vae=None, image1=None, image2=None, image3=None) -> io.NodeOutput:
ref_latents = []
images = [image1, image2, image3]
images_vl = []
# Korrigierter Template für Qwen-Image-Edit (basierend auf offizieller Pipeline)
llama_template = "<|im_start|>system\nYou are a helpful assistant that can edit images based on user instructions. Analyze the input image and provide detailed guidance on how to modify it according to the user's request.<|im_end|>\n<|im_start|>user\n{}<|im_end|>\n<|im_start|>assistant\n"
image_prompt = ""
for i, image in enumerate(images):
if image is not None:
# KONSISTENTE Bildverarbeitung - nur einmal skalieren
samples = image.movedim(-1, 1)
# Einheitliche Zielauflösung für alle Verarbeitungsschritte
target_pixels = int(1024 * 1024)
current_pixels = samples.shape[3] * samples.shape[2]
if current_pixels != target_pixels:
scale_by = math.sqrt(target_pixels / current_pixels)
width = round(samples.shape[3] * scale_by)
height = round(samples.shape[2] * scale_by)
# Skalierung mit area interpolation
s = comfy.utils.common_upscale(samples, width, height, "area", "disabled")
processed_image = s.movedim(1, -1)
else:
processed_image = samples.movedim(1, -1)
# Gleiche Bildverarbeitung für Vision und VAE
images_vl.append(processed_image)
# VAE-Encoding für reference latents (gleiche Auflösung!)
if vae is not None:
ref_latents.append(vae.encode(processed_image[:, :, :, :3]))
image_prompt += "Picture {}: <|vision_start|><|image_pad|><|vision_end|>".format(i + 1)
# Tokenisierung mit korrigiertem Template
tokens = clip.tokenize(image_prompt + prompt, images=images_vl, llama_template=llama_template)
conditioning = clip.encode_from_tokens_scheduled(tokens)
# Reference latents hinzufügen
if len(ref_latents) > 0:
conditioning = node_helpers.conditioning_set_values(conditioning, {"reference_latents": ref_latents}, append=True)
return io.NodeOutput(conditioning)
class TextEncodeQwenNegative(io.ComfyNode):
@classmethod
def define_schema(cls):
return io.Schema(
node_id="TextEncodeQwenNegative",
category="advanced/conditioning",
description="Creates proper negative conditioning for Qwen-Image-Edit models (uses empty string instead of zero conditioning)",
inputs=[
io.Clip.Input("clip"),
io.String.Input("negative_prompt", multiline=True, default=" ", tooltip="Negative prompt (use empty string ' ' for best results)"),
],
outputs=[
io.Conditioning.Output(),
],
)
@classmethod
def execute(cls, clip, negative_prompt=" ") -> io.NodeOutput:
# Für Qwen-Image-Edit: Leerstring als negative prompt (wie in der offiziellen Pipeline)
if not negative_prompt or negative_prompt.strip() == "":
negative_prompt = " "
tokens = clip.tokenize(negative_prompt)
conditioning = clip.encode_from_tokens_scheduled(tokens)
return io.NodeOutput(conditioning)
class QwenExtension(ComfyExtension):
@override
async def get_node_list(self) -> list[type[io.ComfyNode]]:
return [
TextEncodeQwenImageEdit,
TextEncodeQwenImageEditPlus,
TextEncodeQwenNegative,
]
async def comfy_entrypoint() -> QwenExtension:
return QwenExtension()
|