From d0612dac1cfc6f04da0b0d2cc51ca5ccd23f5ec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=8D=8E=E4=B8=9C?= Date: Wed, 30 Apr 2025 14:58:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BB=8E=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=A8=A1=E5=9E=8B=E6=96=87=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __init__.py | 4 +- nodes/comfy_nodes.py | 39 +++++ workflow/InstantCharacterFromLocal.json | 204 ++++++++++++++++++++++++ 3 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 workflow/InstantCharacterFromLocal.json diff --git a/__init__.py b/__init__.py index 9db9a81..d2c810d 100644 --- a/__init__.py +++ b/__init__.py @@ -2,15 +2,17 @@ # 注册节点 -from .nodes.comfy_nodes import InstantCharacterLoadModel, InstantCharacterGenerate +from .nodes.comfy_nodes import InstantCharacterLoadModel, InstantCharacterGenerate, InstantCharacterLoadModelFromLocal NODE_CLASS_MAPPINGS = { + "InstantCharacterLoadModelFromLocal": InstantCharacterLoadModelFromLocal, "InstantCharacterLoadModel": InstantCharacterLoadModel, "InstantCharacterGenerate": InstantCharacterGenerate, } NODE_DISPLAY_NAME_MAPPINGS = { + "InstantCharacterLoadModelFromLocal": "InstantCharacter Load Model From Local Checkpoints", "InstantCharacterLoadModel": "InstantCharacter Load Model", "InstantCharacterGenerate": "InstantCharacter Generate", } diff --git a/nodes/comfy_nodes.py b/nodes/comfy_nodes.py index 7ef3dc8..4a32af3 100644 --- a/nodes/comfy_nodes.py +++ b/nodes/comfy_nodes.py @@ -20,6 +20,45 @@ else: folder_paths.folder_names_and_paths["ipadapter"] = (current_paths, folder_paths.supported_pt_extensions) +class InstantCharacterLoadModelFromLocal: + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + # 模型路径输入替换为 STRING 类型,用户可手动输入路径 + "base_model_path": ("STRING", {"default": "models/FLUX.1-dev", "tooltip": ""}), + "image_encoder_path": ("STRING", {"default": "models/google/siglip-so400m-patch14-384", "tooltip": ""}), + "image_encoder_2_path": ("STRING", {"default": "models/facebook/dinov2-giant", "tooltip": ""}), + "ip_adapter_path": ("STRING", {"default": "models/InstantCharacter/instantcharacter_ip-adapter.bin", "tooltip": ""}), + "cpu_offload": ("BOOLEAN", {"default": False, "tooltip": "是否启用CPU卸载以节省显存"}), + } + } + + RETURN_TYPES = ("INSTANTCHAR_PIPE",) + FUNCTION = "load_model" + CATEGORY = "InstantCharacter" + DESCRIPTION = "加载InstantCharacter模型并支持自定义模型路径" + + def load_model(self, base_model_path, image_encoder_path, image_encoder_2_path, ip_adapter_path, cpu_offload): + + device = "cuda" if torch.cuda.is_available() else "cpu" + + pipe = InstantCharacterFluxPipeline.from_pretrained(base_model_path, torch_dtype=torch.bfloat16) + + if cpu_offload: + pipe.enable_sequential_cpu_offload() + else: + pipe.to(device) + + pipe.init_adapter( + image_encoder_path=image_encoder_path, + image_encoder_2_path=image_encoder_2_path, + subject_ipadapter_cfg=dict(subject_ip_adapter_path=ip_adapter_path, nb_token=1024), + ) + + return (pipe,) + + class InstantCharacterLoadModel: @classmethod def INPUT_TYPES(cls): diff --git a/workflow/InstantCharacterFromLocal.json b/workflow/InstantCharacterFromLocal.json new file mode 100644 index 0000000..4e8e44a --- /dev/null +++ b/workflow/InstantCharacterFromLocal.json @@ -0,0 +1,204 @@ +{ + "id": "895d842a-8b76-4258-a17b-9047cbbc6de3", + "revision": 0, + "last_node_id": 13, + "last_link_id": 4, + "nodes": [ + { + "id": 6, + "type": "SaveImage", + "pos": [ + 926.482666015625, + 333.0054016113281 + ], + "size": [ + 315, + 270 + ], + "flags": {}, + "order": 3, + "mode": 0, + "inputs": [ + { + "name": "images", + "type": "IMAGE", + "link": 3 + } + ], + "outputs": [], + "properties": { + "cnr_id": "comfy-core", + "ver": "0.3.27" + }, + "widgets_values": [ + "ComfyUI", + "" + ] + }, + { + "id": 5, + "type": "LoadImage", + "pos": [ + -148.6251678466797, + 517.7960205078125 + ], + "size": [ + 315, + 314 + ], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "links": [ + 2 + ] + }, + { + "name": "MASK", + "type": "MASK", + "links": null + } + ], + "properties": { + "Node name for S&R": "LoadImage", + "cnr_id": "comfy-core", + "ver": "0.3.27" + }, + "widgets_values": [ + "spider.png", + "image", + "" + ] + }, + { + "id": 2, + "type": "InstantCharacterGenerate", + "pos": [ + 442.3385314941406, + 341.07720947265625 + ], + "size": [ + 400, + 276 + ], + "flags": {}, + "order": 2, + "mode": 0, + "inputs": [ + { + "name": "pipe", + "type": "INSTANTCHAR_PIPE", + "link": 4 + }, + { + "name": "subject_image", + "shape": 7, + "type": "IMAGE", + "link": 2 + } + ], + "outputs": [ + { + "name": "IMAGE", + "type": "IMAGE", + "links": [ + 3 + ] + } + ], + "properties": { + "Node name for S&R": "InstantCharacterGenerate", + "aux_id": "jax-explorer/ComfyUI-InstantCharacter", + "ver": "0a0b09f7ff699237a081aa80a553292ab2106325" + }, + "widgets_values": [ + "The American animated IP, Spider-Man 2099, wearing a futuristic suit, leaps between cybernetic cityscapes with neon lights crisscrossing the background, exuding a strong sense of technology.", + 1024, + 1024, + 3.5, + 28, + 1049281553832103, + "randomize", + 0.9 + ] + }, + { + "id": 7, + "type": "InstantCharacterLoadModelFromLocal", + "pos": [ + -133.9701690673828, + 192.62173461914062 + ], + "size": [ + 340.20001220703125, + 154 + ], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "INSTANTCHAR_PIPE", + "type": "INSTANTCHAR_PIPE", + "links": [ + 4 + ] + } + ], + "properties": { + "Node name for S&R": "InstantCharacterLoadModel" + }, + "widgets_values": [ + "models/FLUX.1-dev", + "models/google/siglip-so400m-patch14-384", + "models/facebook/dinov2-giant", + "models/InstantCharacter/instantcharacter_ip-adapter.bin", + false + ] + } + ], + "links": [ + [ + 2, + 5, + 0, + 2, + 1, + "IMAGE" + ], + [ + 3, + 2, + 0, + 6, + 0, + "IMAGE" + ], + [ + 4, + 7, + 0, + 2, + 0, + "INSTANTCHAR_PIPE" + ] + ], + "groups": [], + "config": {}, + "extra": { + "ds": { + "scale": 1.1, + "offset": [ + 486.6873837530304, + 34.905245727143566 + ] + } + }, + "version": 0.4 +} \ No newline at end of file