From c93073e6f6087df9f8738ccbd3adba16daaeddd5 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 11 Dec 2025 18:07:49 +0000 Subject: [PATCH] qwen i2l style --- __init__.py | 3 ++ nodes.py | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ rh_config.json | 1 + 3 files changed, 101 insertions(+) create mode 100644 __init__.py create mode 100644 nodes.py create mode 100644 rh_config.json diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..76028e6 --- /dev/null +++ b/__init__.py @@ -0,0 +1,3 @@ +from .nodes import NODE_CLASS_MAPPINGS +NODE_DISPLAY_NAME_MAPPINGS = {k:k for k,v in NODE_CLASS_MAPPINGS.items()} +__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] diff --git a/nodes.py b/nodes.py new file mode 100644 index 0000000..086f00e --- /dev/null +++ b/nodes.py @@ -0,0 +1,97 @@ +import uuid +import os +import folder_paths +from diffsynth.pipelines.qwen_image import ( + QwenImagePipeline, ModelConfig, + QwenImageUnit_Image2LoRAEncode, QwenImageUnit_Image2LoRADecode +) +from modelscope import snapshot_download +from safetensors.torch import save_file +import torch +from PIL import Image +import numpy as np + +class RunningHub_ImageQwenI2L_Loader_Style: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + + } + } + + RETURN_TYPES = ('RH_QwenImageI2LPipeline', ) + RETURN_NAMES = ('QwenImageI2LPipeline', ) + FUNCTION = "load" + CATEGORY = "RunningHub/ImageQwenI2L" + + def __init__(self): + self.vram_config_disk_offload = { + "offload_dtype": "disk", + "offload_device": "disk", + "onload_dtype": "disk", + "onload_device": "disk", + "preparing_dtype": torch.bfloat16, + "preparing_device": "cuda", + "computation_dtype": torch.bfloat16, + "computation_device": "cuda", + } + # self.encoder_path = os.path.join(folder_paths.models_dir, 'DiffSynth-Studio', 'General-Image-Encoders') + # self.i2l_path = os.path.join(folder_paths.models_dir, 'DiffSynth-Studio', 'Qwen-Image-i2L') + # self.processor_path = os.path.join(folder_paths.models_dir, 'DiffSynth-Studio', 'Qwen-Image-Edit') + + def load(self): + pipe = QwenImagePipeline.from_pretrained( + torch_dtype=torch.bfloat16, + device="cuda", + model_configs=[ + ModelConfig(model_id='DiffSynth-Studio/General-Image-Encoders', origin_file_pattern="SigLIP2-G384/model.safetensors", **self.vram_config_disk_offload), + ModelConfig(model_id='DiffSynth-Studio/General-Image-Encoders', origin_file_pattern="DINOv3-7B/model.safetensors", **self.vram_config_disk_offload), + ModelConfig(model_id='DiffSynth-Studio/Qwen-Image-i2L', origin_file_pattern="Qwen-Image-i2L-Style.safetensors", **self.vram_config_disk_offload), + ], + processor_config=ModelConfig(model_id='Qwen/Qwen-Image-Edit', origin_file_pattern="processor/"), + vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 2, + ) + return (pipe, ) + +class RunningHub_ImageQwenI2L_LoraGenerator: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "pipeline": ("RH_QwenImageI2LPipeline", ), + "training_images": ("IMAGE", ), + "seed": ("INT", {"default": 42, "min": 0, "max": 0xffffffffffffffff}), + } + } + + RETURN_TYPES = ('STRING', 'RH_Lora') + RETURN_NAMES = ('lora_name', 'lora') + FUNCTION = "generate" + CATEGORY = "RunningHub/ImageQwenI2L" + + OUTPUT_NODE = True + + def tensor_2_pil(self, img_tensor): + i = 255. * img_tensor.squeeze().cpu().numpy() + img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8)) + return img + + def __init__(self): + self.lora_name = f"i2l_style_lora_{str(uuid.uuid4())}.safetensors" + + def generate(self, pipeline, training_images, **kwargs): + training_images = [self.tensor_2_pil(image) for image in training_images] + training_images = [image.convert("RGB") for image in training_images] + lora_path = os.path.join(folder_paths.models_dir, 'loras', self.lora_name) + with torch.no_grad(): + embs = QwenImageUnit_Image2LoRAEncode().process(pipeline, image2lora_images=training_images) + lora = QwenImageUnit_Image2LoRADecode().process(pipeline, **embs)["lora"] + save_file(lora, lora_path) + return (self.lora_name, lora_path) + +NODE_CLASS_MAPPINGS = { + "RunningHub_ImageQwenI2L_Loader(Style)": RunningHub_ImageQwenI2L_Loader_Style, + "RunningHub_ImageQwenI2L_LoraGenerator": RunningHub_ImageQwenI2L_LoraGenerator, +} + diff --git a/rh_config.json b/rh_config.json new file mode 100644 index 0000000..6e0b528 --- /dev/null +++ b/rh_config.json @@ -0,0 +1 @@ +{"enable": true, "untracked_paths": []}