File size: 8,096 Bytes
b5ac208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db60c62
b5ac208
 
 
 
f5f0556
b5ac208
 
 
 
 
 
 
 
 
 
 
 
 
 
5306acf
b5ac208
5306acf
 
 
 
db60c62
5306acf
b5ac208
5306acf
b5ac208
5306acf
 
 
db60c62
5306acf
 
 
 
 
 
 
db60c62
5306acf
 
 
06d0fb4
 
db60c62
06d0fb4
 
 
5306acf
9331538
06d0fb4
 
db60c62
06d0fb4
 
 
db60c62
 
 
 
 
 
06d0fb4
9331538
a01c15b
 
db60c62
a01c15b
 
 
 
4e82511
 
 
db60c62
4e82511
 
 
 
db60c62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5ac208
 
849a9be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5ac208
 
 
 
 
5306acf
 
 
b5ac208
5306acf
 
b5ac208
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import os
import sys
import shutil

from core.settings import *

def move_and_overwrite(src, dst):
    if os.path.isdir(src):
        if os.path.exists(dst):
            shutil.rmtree(dst)
        shutil.move(src, dst)
    elif os.path.isfile(src):
        if os.path.exists(dst):
            os.remove(dst)
        shutil.move(src, dst)

def initialize_comfyui():
    APP_DIR = sys.path[0]
    COMFYUI_TEMP_DIR = "ComfyUI_temp"

    print("--- Cloning ComfyUI Repository ---")
    if not os.path.exists(COMFYUI_TEMP_DIR):
        os.system(f"git clone --depth 1 https://github.com/comfy-Org/ComfyUI {COMFYUI_TEMP_DIR}")
        print("βœ… ComfyUI repository cloned.")
    else:
        print("βœ… ComfyUI repository already exists.")


    print(f"--- Merging ComfyUI from '{COMFYUI_TEMP_DIR}' to '{APP_DIR}' ---")
    for item in os.listdir(COMFYUI_TEMP_DIR):
        src_path = os.path.join(COMFYUI_TEMP_DIR, item)
        dst_path = os.path.join(APP_DIR, item)
        if item == '.git':
            continue
        move_and_overwrite(src_path, dst_path)
    
    try:
        shutil.rmtree(COMFYUI_TEMP_DIR)
        print("βœ… ComfyUI merged and temporary directory removed.")
    except OSError as e:
        print(f"⚠️ Could not remove temporary directory '{COMFYUI_TEMP_DIR}': {e}")


    print("--- Cloning third-party extensions for ComfyUI ---")

    # 1. ComfyUI_IPAdapter_plus
    ipadapter_plus_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI_IPAdapter_plus")
    if not os.path.exists(ipadapter_plus_path):
        os.system(f"git clone --depth 1 https://github.com/cubiq/ComfyUI_IPAdapter_plus.git {ipadapter_plus_path}")
        print("βœ… ComfyUI_IPAdapter_plus extension cloned.")
    else:
        print("βœ… ComfyUI_IPAdapter_plus extension already exists.")

    # 2. ComfyUI-InstantX-IPAdapter-SD3
    ipadapter_plus_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-InstantX-IPAdapter-SD3")
    if not os.path.exists(ipadapter_plus_path):
        os.system(f"git clone --depth 1 https://github.com/Slickytail/ComfyUI-InstantX-IPAdapter-SD3.git {ipadapter_plus_path}")
        print("βœ… ComfyUI-InstantX-IPAdapter-SD3 extension cloned.")
    else:
        print("βœ… ComfyUI-InstantX-IPAdapter-SD3 extension already exists.")

    # 3. ComfyUI-IPAdapter-Flux
    ipadapter_flux_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-IPAdapter-Flux")
    if not os.path.exists(ipadapter_flux_path):
        os.system(f"git clone --depth 1 https://github.com/Shakker-Labs/ComfyUI-IPAdapter-Flux.git {ipadapter_flux_path}")
        print("βœ… ComfyUI-IPAdapter-Flux extension cloned.")
    else:
        print("βœ… ComfyUI-IPAdapter-Flux extension already exists.")
    try:
        print("--- [Setup] Applying PR #108 compatibility patch for ComfyUI-IPAdapter-Flux ---")
        os.system(f"git -C {ipadapter_flux_path} fetch --depth 1 origin pull/108/head && git -C {ipadapter_flux_path} checkout -f FETCH_HEAD")
        print("βœ… Successfully applied PR #108 compatibility patch.")
    except Exception as e:
        print(f"⚠️ Warning: Could not apply PR #108 compatibility patch for ComfyUI-IPAdapter-Flux: {e}")

    # 4. comfyui-krea2-controlnet
    krea2_controlnet_nodes_path = os.path.join(APP_DIR, "custom_nodes", "comfyui-krea2-controlnet")
    if not os.path.exists(krea2_controlnet_nodes_path):
        os.system(f"git clone --depth 1 https://github.com/facok/comfyui-krea2-controlnet.git {krea2_controlnet_nodes_path}")
        print("βœ… comfyui-krea2-controlnet extension cloned.")
    else:
        print("βœ… comfyui-krea2-controlnet extension already exists.")
    try:
        print("--- [Setup] Applying PR #12 compatibility patch for comfyui-krea2-controlnet ---")
        os.system(f"git -C {krea2_controlnet_nodes_path} fetch --depth 1 origin pull/12/head && git -C {krea2_controlnet_nodes_path} checkout -f FETCH_HEAD")
        print("βœ… Successfully applied PR #12 compatibility patch.")
    except Exception as e:
        print(f"⚠️ Warning: Could not apply PR #12 compatibility patch for comfyui-krea2-controlnet: {e}")

    # 5. comfyui-krea2edit
    krea2edit_nodes_path = os.path.join(APP_DIR, "custom_nodes", "comfyui-krea2edit")
    if not os.path.exists(krea2edit_nodes_path):
        os.system(f"git clone --depth 1 https://github.com/lbouaraba/comfyui-krea2edit.git {krea2edit_nodes_path}")
        print("βœ… comfyui-krea2edit extension cloned.")
    else:
        print("βœ… comfyui-krea2edit extension already exists.")

    # 6. comfyui-anima-3-8B
    anima_38b_nodes_path = os.path.join(APP_DIR, "custom_nodes", "comfyui-anima-3-8B")
    if not os.path.exists(anima_38b_nodes_path):
        os.system(f"git clone --depth 1 https://github.com/GumGum10/comfyui-anima-3-8B.git {anima_38b_nodes_path}")
        print("βœ… comfyui-anima-3-8B extension cloned.")
    else:
        print("βœ… comfyui-anima-3-8B extension already exists.")

    # 7. ComfyUI-Anima-2.9B-loraPatch
    anima_29b_lora_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-Anima-2.9B-loraPatch")
    if not os.path.exists(anima_29b_lora_path):
        os.system(f"git clone --depth 1 https://github.com/sparklingcoffee777/ComfyUI-Anima-2.9B-loraPatch.git {anima_29b_lora_path}")
        print("βœ… ComfyUI-Anima-2.9B-loraPatch extension cloned.")
    else:
        print("βœ… ComfyUI-Anima-2.9B-loraPatch extension already exists.")

    # 8. ComfyUI-Anima-3.8B-loraPatch
    anima_38b_lora_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-Anima-3.8B-loraPatch")
    if not os.path.exists(anima_38b_lora_path):
        os.system(f"git clone --depth 1 https://github.com/KitagawaCoding/ComfyUI-Anima-3.8B-loraPatch.git {anima_38b_lora_path}")
        print("βœ… ComfyUI-Anima-3.8B-loraPatch extension cloned.")
    else:
        print("βœ… ComfyUI-Anima-3.8B-loraPatch extension already exists.")

    print(f"βœ… Current working directory is: {os.getcwd()}")
    
    # [ZeroGPU / Upstream ComfyUI Compatibility]
    # Guard against ComfyUI PR #16746 is_integrated attribute check crash under ZeroGPU SimpleNamespace
    try:
        import torch
        if hasattr(torch, "cuda") and hasattr(torch.cuda, "get_device_properties"):
            _orig_gdp = torch.cuda.get_device_properties
            def _safe_gdp(*args, **kwargs):
                p = _orig_gdp(*args, **kwargs)
                if not hasattr(p, "is_integrated"):
                    try:
                        p.is_integrated = False
                    except Exception:
                        pass
                return p
            torch.cuda.get_device_properties = _safe_gdp
    except Exception:
        pass

    mm_path = os.path.join(APP_DIR, "comfy", "model_management.py")
    if os.path.exists(mm_path):
        try:
            with open(mm_path, "r", encoding="utf-8") as f:
                mm_code = f.read()
            unsafe_check = "bool(torch.cuda.get_device_properties(device).is_integrated)"
            safe_check = "bool(getattr(torch.cuda.get_device_properties(device), 'is_integrated', False))"
            if unsafe_check in mm_code:
                with open(mm_path, "w", encoding="utf-8") as f:
                    f.write(mm_code.replace(unsafe_check, safe_check))
                print("πŸ›‘οΈ [Setup] Patched comfy/model_management.py for safe is_integrated attribute access.")
        except Exception as e:
            print(f"⚠️ [Setup] Failed to patch comfy/model_management.py: {e}")

    import comfy.model_management
    print("--- Environment Ready ---")

    print("βœ… ComfyUI initialized with default attention mechanism.")

    for dir_path in CATEGORY_TO_DIR_MAP.values():
        os.makedirs(os.path.join(APP_DIR, dir_path), exist_ok=True)
    
    os.makedirs(os.path.join(APP_DIR, INPUT_DIR), exist_ok=True)
    os.makedirs(os.path.join(APP_DIR, OUTPUT_DIR), exist_ok=True)
    
    print("βœ… All required model directories are present.")