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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import node_helpers
|
| 2 |
+
import comfy.utils
|
| 3 |
+
import math
|
| 4 |
+
from typing_extensions import override
|
| 5 |
+
from comfy_api.latest import ComfyExtension, io
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class TextEncodeQwenImageEdit(io.ComfyNode):
|
| 9 |
+
@classmethod
|
| 10 |
+
def define_schema(cls):
|
| 11 |
+
return io.Schema(
|
| 12 |
+
node_id="TextEncodeQwenImageEdit",
|
| 13 |
+
category="advanced/conditioning",
|
| 14 |
+
inputs=[
|
| 15 |
+
io.Clip.Input("clip"),
|
| 16 |
+
io.String.Input("prompt", multiline=True, dynamic_prompts=True),
|
| 17 |
+
io.Vae.Input("vae", optional=True),
|
| 18 |
+
io.Image.Input("image", optional=True),
|
| 19 |
+
],
|
| 20 |
+
outputs=[
|
| 21 |
+
io.Conditioning.Output(),
|
| 22 |
+
],
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
@classmethod
|
| 26 |
+
def execute(cls, clip, prompt, vae=None, image=None) -> io.NodeOutput:
|
| 27 |
+
ref_latent = None
|
| 28 |
+
if image is None:
|
| 29 |
+
images = []
|
| 30 |
+
else:
|
| 31 |
+
# Konsistente Bildverarbeitung - nur einmal skalieren
|
| 32 |
+
samples = image.movedim(-1, 1)
|
| 33 |
+
|
| 34 |
+
# Für Qwen-Image-Edit: Zielauflösung 1024x1024 (wie in der offiziellen Pipeline)
|
| 35 |
+
target_pixels = int(1024 * 1024)
|
| 36 |
+
current_pixels = samples.shape[3] * samples.shape[2]
|
| 37 |
+
|
| 38 |
+
if current_pixels != target_pixels:
|
| 39 |
+
scale_by = math.sqrt(target_pixels / current_pixels)
|
| 40 |
+
width = round(samples.shape[3] * scale_by)
|
| 41 |
+
height = round(samples.shape[2] * scale_by)
|
| 42 |
+
|
| 43 |
+
# Skalierung mit area interpolation für bessere Qualität
|
| 44 |
+
s = comfy.utils.common_upscale(samples, width, height, "area", "disabled")
|
| 45 |
+
image = s.movedim(1, -1)
|
| 46 |
+
else:
|
| 47 |
+
image = samples.movedim(1, -1)
|
| 48 |
+
|
| 49 |
+
images = [image[:, :, :, :3]]
|
| 50 |
+
|
| 51 |
+
# VAE-Encoding für reference latents
|
| 52 |
+
if vae is not None:
|
| 53 |
+
ref_latent = vae.encode(image[:, :, :, :3])
|
| 54 |
+
|
| 55 |
+
# Tokenisierung mit korrektem Template für Qwen-Image-Edit
|
| 56 |
+
tokens = clip.tokenize(prompt, images=images)
|
| 57 |
+
conditioning = clip.encode_from_tokens_scheduled(tokens)
|
| 58 |
+
|
| 59 |
+
# Reference latents hinzufügen (wie in der offiziellen Pipeline)
|
| 60 |
+
if ref_latent is not None:
|
| 61 |
+
conditioning = node_helpers.conditioning_set_values(conditioning, {"reference_latents": [ref_latent]}, append=True)
|
| 62 |
+
|
| 63 |
+
return io.NodeOutput(conditioning)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class TextEncodeQwenImageEditPlus(io.ComfyNode):
|
| 67 |
+
@classmethod
|
| 68 |
+
def define_schema(cls):
|
| 69 |
+
return io.Schema(
|
| 70 |
+
node_id="TextEncodeQwenImageEditPlus",
|
| 71 |
+
category="advanced/conditioning",
|
| 72 |
+
inputs=[
|
| 73 |
+
io.Clip.Input("clip"),
|
| 74 |
+
io.String.Input("prompt", multiline=True, dynamic_prompts=True),
|
| 75 |
+
io.Vae.Input("vae", optional=True),
|
| 76 |
+
io.Image.Input("image1", optional=True),
|
| 77 |
+
io.Image.Input("image2", optional=True),
|
| 78 |
+
io.Image.Input("image3", optional=True),
|
| 79 |
+
],
|
| 80 |
+
outputs=[
|
| 81 |
+
io.Conditioning.Output(),
|
| 82 |
+
],
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
@classmethod
|
| 86 |
+
def execute(cls, clip, prompt, vae=None, image1=None, image2=None, image3=None) -> io.NodeOutput:
|
| 87 |
+
ref_latents = []
|
| 88 |
+
images = [image1, image2, image3]
|
| 89 |
+
images_vl = []
|
| 90 |
+
|
| 91 |
+
# Korrigierter Template für Qwen-Image-Edit (basierend auf offizieller Pipeline)
|
| 92 |
+
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"
|
| 93 |
+
image_prompt = ""
|
| 94 |
+
|
| 95 |
+
for i, image in enumerate(images):
|
| 96 |
+
if image is not None:
|
| 97 |
+
# KONSISTENTE Bildverarbeitung - nur einmal skalieren
|
| 98 |
+
samples = image.movedim(-1, 1)
|
| 99 |
+
|
| 100 |
+
# Einheitliche Zielauflösung für alle Verarbeitungsschritte
|
| 101 |
+
target_pixels = int(1024 * 1024)
|
| 102 |
+
current_pixels = samples.shape[3] * samples.shape[2]
|
| 103 |
+
|
| 104 |
+
if current_pixels != target_pixels:
|
| 105 |
+
scale_by = math.sqrt(target_pixels / current_pixels)
|
| 106 |
+
width = round(samples.shape[3] * scale_by)
|
| 107 |
+
height = round(samples.shape[2] * scale_by)
|
| 108 |
+
|
| 109 |
+
# Skalierung mit area interpolation
|
| 110 |
+
s = comfy.utils.common_upscale(samples, width, height, "area", "disabled")
|
| 111 |
+
processed_image = s.movedim(1, -1)
|
| 112 |
+
else:
|
| 113 |
+
processed_image = samples.movedim(1, -1)
|
| 114 |
+
|
| 115 |
+
# Gleiche Bildverarbeitung für Vision und VAE
|
| 116 |
+
images_vl.append(processed_image)
|
| 117 |
+
|
| 118 |
+
# VAE-Encoding für reference latents (gleiche Auflösung!)
|
| 119 |
+
if vae is not None:
|
| 120 |
+
ref_latents.append(vae.encode(processed_image[:, :, :, :3]))
|
| 121 |
+
|
| 122 |
+
image_prompt += "Picture {}: <|vision_start|><|image_pad|><|vision_end|>".format(i + 1)
|
| 123 |
+
|
| 124 |
+
# Tokenisierung mit korrigiertem Template
|
| 125 |
+
tokens = clip.tokenize(image_prompt + prompt, images=images_vl, llama_template=llama_template)
|
| 126 |
+
conditioning = clip.encode_from_tokens_scheduled(tokens)
|
| 127 |
+
|
| 128 |
+
# Reference latents hinzufügen
|
| 129 |
+
if len(ref_latents) > 0:
|
| 130 |
+
conditioning = node_helpers.conditioning_set_values(conditioning, {"reference_latents": ref_latents}, append=True)
|
| 131 |
+
|
| 132 |
+
return io.NodeOutput(conditioning)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
class TextEncodeQwenNegative(io.ComfyNode):
|
| 136 |
+
@classmethod
|
| 137 |
+
def define_schema(cls):
|
| 138 |
+
return io.Schema(
|
| 139 |
+
node_id="TextEncodeQwenNegative",
|
| 140 |
+
category="advanced/conditioning",
|
| 141 |
+
description="Creates proper negative conditioning for Qwen-Image-Edit models (uses empty string instead of zero conditioning)",
|
| 142 |
+
inputs=[
|
| 143 |
+
io.Clip.Input("clip"),
|
| 144 |
+
io.String.Input("negative_prompt", multiline=True, default=" ", tooltip="Negative prompt (use empty string ' ' for best results)"),
|
| 145 |
+
],
|
| 146 |
+
outputs=[
|
| 147 |
+
io.Conditioning.Output(),
|
| 148 |
+
],
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
@classmethod
|
| 152 |
+
def execute(cls, clip, negative_prompt=" ") -> io.NodeOutput:
|
| 153 |
+
# Für Qwen-Image-Edit: Leerstring als negative prompt (wie in der offiziellen Pipeline)
|
| 154 |
+
if not negative_prompt or negative_prompt.strip() == "":
|
| 155 |
+
negative_prompt = " "
|
| 156 |
+
|
| 157 |
+
tokens = clip.tokenize(negative_prompt)
|
| 158 |
+
conditioning = clip.encode_from_tokens_scheduled(tokens)
|
| 159 |
+
return io.NodeOutput(conditioning)
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
class QwenExtension(ComfyExtension):
|
| 163 |
+
@override
|
| 164 |
+
async def get_node_list(self) -> list[type[io.ComfyNode]]:
|
| 165 |
+
return [
|
| 166 |
+
TextEncodeQwenImageEdit,
|
| 167 |
+
TextEncodeQwenImageEditPlus,
|
| 168 |
+
TextEncodeQwenNegative,
|
| 169 |
+
]
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
async def comfy_entrypoint() -> QwenExtension:
|
| 173 |
+
return QwenExtension()
|