File size: 2,527 Bytes
7d2ed44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Let ComfyUI-GGUF load MiniMax-H3-Image GGUF files.

ComfyUI-GGUF only loads GGUF files whose architecture is on its list, and
"minimax_h3" isn't on it yet. This adds it: one word in
custom_nodes/ComfyUI-GGUF/loader.py. Nothing else changes.

Usage, from anywhere, with the Python that runs your ComfyUI:

    python patch_comfyui_gguf.py PATH/TO/ComfyUI
    python_embeded\\python.exe patch_comfyui_gguf.py ComfyUI     (Windows portable)

Then restart ComfyUI. Running it twice is harmless. A copy of the original
is saved next to it as loader.py.bak.

If updating ComfyUI-GGUF later complains about local changes to loader.py,
restore the original (git checkout loader.py, or copy loader.py.bak back),
update, and run this again. Once ComfyUI-GGUF includes minimax_h3 itself,
you no longer need it.
"""
import glob
import os
import re
import shutil
import sys

ARCH = b'"minimax_h3"'


def find_loaders(root):
    """ComfyUI-GGUF's loader.py under root/custom_nodes (any folder name)."""
    hits = []
    for path in glob.glob(os.path.join(root, "custom_nodes", "*", "loader.py")):
        with open(path, "rb") as f:
            if b"IMG_ARCH_LIST" in f.read():
                hits.append(path)
    return hits


def patch(path):
    with open(path, "rb") as f:
        src = f.read()
    m = re.search(rb"^IMG_ARCH_LIST\s*=\s*\{([^}\r\n]*)\}", src, re.M)
    if not m:
        print(f"  {path}: no IMG_ARCH_LIST line in the expected form; edit it by hand")
        return False
    if ARCH in m.group(1):
        print(f"  {path}: already allows minimax_h3")
        return True
    body = m.group(1).rstrip()
    new_body = (body + b", " if body else b"") + ARCH
    out = src[:m.start(1)] + new_body + src[m.end(1):]
    shutil.copy2(path, path + ".bak")
    with open(path, "wb") as f:
        f.write(out)
    print(f"  {path}: patched (original saved as loader.py.bak)")
    return True


def main():
    root = sys.argv[1] if len(sys.argv) > 1 else os.getcwd()
    if os.path.basename(os.path.normpath(root)) == "custom_nodes":
        root = os.path.dirname(os.path.normpath(root))
    loaders = find_loaders(root)
    if not loaders:
        sys.exit(f"No ComfyUI-GGUF found in {os.path.join(root, 'custom_nodes')}. "
                 "Pass your ComfyUI folder: python patch_comfyui_gguf.py PATH/TO/ComfyUI")
    ok = all(patch(p) for p in loaders)
    print("Done. Restart ComfyUI." if ok else "Not every copy could be patched; see above.")
    sys.exit(0 if ok else 1)


if __name__ == "__main__":
    main()