File size: 6,813 Bytes
c5be659
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""SnapRef (simple node): up to eight pictures of a character, Mode, Strength, Fidelity.
Everything else at the trained defaults; the full set of inputs is on SnapRef (Advanced) in __init__.py."""
from __future__ import annotations

import logging
import math
import os

import torch

import comfy.patcher_extension
import folder_paths

IMAGE_EXTS = (".png", ".jpg", ".jpeg", ".webp", ".bmp", ".avif")
MODES = {"Character & Style": "cs", "Character": "char"}
MAX_REFERENCES = 8


def input_images() -> list[str]:
    d = folder_paths.get_input_directory()
    try:
        return sorted(f for f in os.listdir(d) if os.path.isfile(os.path.join(d, f)) and f.lower().endswith(IMAGE_EXTS))
    except OSError:
        return []


def load_input_image(name: str) -> torch.Tensor:
    """ComfyUI input image -> IMAGE [1, H, W, 3] (EXIF orientation applied, alpha flattened on white)."""
    import numpy as np
    from PIL import Image, ImageOps
    import node_helpers
    img = node_helpers.pillow(Image.open, folder_paths.get_annotated_filepath(name))
    img = node_helpers.pillow(ImageOps.exif_transpose, img)
    if img.mode in ("RGBA", "LA", "P"):
        img = img.convert("RGBA")
        bg = Image.new("RGB", img.size, (255, 255, 255))
        bg.paste(img, mask=img.split()[3])
        img = bg
    return torch.from_numpy(np.asarray(img.convert("RGB")).astype(np.float32) / 255.0)[None]


def snapref_files() -> list[str]:
    """SnapRef weights in models/loras: bf16 before FP8, newer version names first (v2a-pre2, pre1, v1b, v1a)."""
    names = [n for n in folder_paths.get_filename_list("loras") if "snapref" in n.lower()]
    return sorted(names, key=lambda n: ("fp8" in n.lower(), [-ord(c) for c in os.path.basename(n).lower()])) \
        or ["snapref_v1a.safetensors"]


def make_simple_node(core):
    """core = the SnapRef module (adapters, encoder, wrappers). Returns the node class."""

    class SnapRefSimple:
        @classmethod
        def INPUT_TYPES(cls):
            images = input_images()
            inputs = {"required": {
                "model": ("MODEL",),
                "clip": ("CLIP",),
                "vae": ("VAE",),
                "adapter": (snapref_files(), {"tooltip": "SnapRef weights (models/loras)"}),
                "reference": (images, {"image_upload": True, "tooltip": "a picture of the character"}),
                "mode": (list(MODES), {"default": "Character & Style",
                                       "tooltip": "Character & Style: character and art style from the picture. Character: "
                                                  "only the character; the style comes from the model and your prompt"}),
                "strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 2.0, "step": 0.05,
                                       "tooltip": "how much the picture counts: 1 = as trained, 0 = off, 2 = twice"}),
                "fidelity": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.05,
                                       "tooltip": "1 = keep every detail of the picture; lower = follow the prompt more freely"}),
            }, "optional": {
                "reference_2": (["none"] + images, {"default": "none",
                                                   "tooltip": "a second picture of the same character (e.g. full body)"}),
                "strength_2": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 2.0, "step": 0.05,
                                         "tooltip": "how much the second picture counts"}),
            }}
            for i in range(3, MAX_REFERENCES + 1):
                inputs["optional"][f"reference_{i}"] = (["none"] + images, {"default": "none"})
                inputs["optional"][f"strength_{i}"] = ("FLOAT", {"default": 1.0, "min": 0.0, "max": 2.0, "step": 0.05})
            return inputs

        RETURN_TYPES = ("MODEL",)
        FUNCTION = "apply"
        CATEGORY = "SnapRef"
        DESCRIPTION = "Reference a character with up to eight pictures. Write only the scene in the prompt."

        @classmethod
        def IS_CHANGED(cls, reference=None, reference_2="none", **kw):
            out = []
            for n in (reference, reference_2, *(kw.get(f"reference_{i}") for i in range(3, MAX_REFERENCES + 1))):
                if n and n != "none":
                    p = folder_paths.get_annotated_filepath(n)
                    out.append(f"{n}:{os.path.getmtime(p) if os.path.exists(p) else 0}")
            return "|".join(out)

        @classmethod
        def VALIDATE_INPUTS(cls, reference=None, reference_2="none", **kw):
            for n in (reference, reference_2, *(kw.get(f"reference_{i}") for i in range(3, MAX_REFERENCES + 1))):
                if n and n != "none" and not folder_paths.exists_annotated_filepath(n):
                    return f"Invalid image file: {n}"
            return True

        def apply(self, model, clip, vae, adapter, reference, mode, strength, fidelity, reference_2="none", strength_2=1.0, **kw):
            path = folder_paths.get_full_path_or_raise("loras", adapter)
            if path not in core._adapters:
                core._adapters[path] = core._Adapter(path)
            ad = core._adapters[path]
            refs, weights = [], []
            pairs = [(reference, strength), (reference_2, strength_2)]
            pairs.extend((kw.get(f"reference_{i}", "none"), kw.get(f"strength_{i}", 1.0))
                         for i in range(3, MAX_REFERENCES + 1))
            for name, w in pairs:
                if name and name != "none" and w > 0:
                    refs.append(load_input_image(name))
                    weights.append(float(w))
            if not refs:
                return (model,)
            core._adapter_block_map(ad.block_indices, model.get_model_object("diffusion_model"))
            latents = [core._encode_image(model, vae, px, 1.0)[0] for px in refs]
            cond = clip.encode_from_tokens_scheduled(clip.tokenize(""))[0]
            # Fidelity f: the negative branch also reads the picture, weighted (1 - f); f = 1 is the trained default
            uncond_bias = None if fidelity >= 1.0 else math.log(max(1.0 - float(fidelity), 1e-6))
            m = model.clone()
            m.add_wrapper_with_key(comfy.patcher_extension.WrappersMP.DIFFUSION_MODEL, core.WRAPPER_KEY,
                                   core._make_wrapper(ad, latents, cond, MODES[mode], False, 0.0, weights, uncond_bias))
            wr = m.wrappers[comfy.patcher_extension.WrappersMP.DIFFUSION_MODEL]
            m.wrappers[comfy.patcher_extension.WrappersMP.DIFFUSION_MODEL] = {core.WRAPPER_KEY: wr.pop(core.WRAPPER_KEY), **wr}
            logging.info(f"[SnapRef] {adapter}: {len(refs)} picture(s), {mode}, strength {weights}, fidelity {fidelity}")
            return (m,)

    return SnapRefSimple