File size: 10,095 Bytes
ae826ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import os

# Define paths
BASE_PATH = "./ComfyUI"
MODEL_PATH = os.path.join(BASE_PATH, "models")
CACHE_PATH = "/workspace/huggingface_cache"

os.environ["HF_HOME"] = CACHE_PATH
os.makedirs(CACHE_PATH, exist_ok=True)


def run_command(command):
    """Run a shell command using os.system() (simpler than subprocess)."""
    print(f"πŸ”„ Running: {command}")
    exit_code = os.system(command)

    if exit_code != 0:
        print(f"❌ Command failed: {command} (Exit Code: {exit_code})")
        exit(1)  # Exit on failure


def install_dependencies():
    """Install system dependencies and Python packages."""
    print("πŸ“¦ Installing dependencies...")
    # run_command("apt-get update && apt-get install -y git wget curl libgl1-mesa-glx libglib2.0-0 tmux emacs git-lfs")
    run_command("pip install --upgrade pip")
    run_command("pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124")
    run_command("pip install xformers --index-url https://download.pytorch.org/whl/cu124")
    run_command("pip install -r requirements.txt")
    print("βœ… Dependencies installed.")


def manage_git_repo(repo_url, install_path, requirements=False, submodules=False):
    """Clone or update a git repository and handle its dependencies.

    Args:
        repo_url: URL of the git repository
        install_path: Where to install/update the repository
        requirements: Whether to install requirements.txt
        submodules: Whether to update git submodules
    """
    if not os.path.exists(install_path) or not os.path.isdir(install_path) or not os.path.exists(
            os.path.join(install_path, ".git")):
        print(f"πŸ“‚ Cloning {os.path.basename(install_path)}...")
        run_command(f"git clone {repo_url} {install_path}")
    else:
        print(f"πŸ”„ {os.path.basename(install_path)} exists. Checking for updates...")

    # Change to repo directory and update
    os.chdir(install_path)
    run_command("git pull")

    if submodules:
        run_command("git submodule update --init --recursive")

    if requirements:
        run_command("python -m pip install -r requirements.txt")

    print(f"βœ… {os.path.basename(install_path)} installed and updated.")


def install_comfyui():
    """Clone and set up ComfyUI if not already installed."""
    manage_git_repo(
        "https://github.com/comfyanonymous/ComfyUI.git",
        BASE_PATH,
        requirements=True
    )


def download_huggingface_models():
    """Download required models from Hugging Face."""
    from huggingface_hub import hf_hub_download
    hf_models = [
        {"repo_id": "black-forest-labs/FLUX.1-dev", "filename": "flux1-dev.safetensors", "folder": "unet"},
        {"repo_id": "black-forest-labs/FLUX.1-dev", "filename": "ae.safetensors", "folder": "vae"},
        {"repo_id": "Shakker-Labs/FLUX.1-dev-ControlNet-Union-Pro", "filename": "diffusion_pytorch_model.safetensors",
         "folder": "controlnet"},
        {"repo_id": "guozinan/PuLID", "filename": "pulid_flux_v0.9.1.safetensors", "folder": "pulid"},
        {"repo_id": "comfyanonymous/flux_text_encoders", "filename": "t5xxl_fp16.safetensors", "folder": "text_encoders"},
        {"repo_id": "comfyanonymous/flux_text_encoders", "filename": "clip_l.safetensors", "folder": "text_encoders"},
    ]

    # Dictionary mapping repo_ids to specific filenames
    filename_mappings = {
        "Shakker-Labs/FLUX.1-dev-ControlNet-Union-Pro": "Flux_Dev_ControlNet_Union_Pro_ShakkerLabs.safetensors",
    }

    for model in hf_models:
        try:
            model_path = hf_hub_download(
                repo_id=model["repo_id"], 
                filename=model["filename"],
                cache_dir=CACHE_PATH,
                repo_type=model.get("repo_type", "model")
            )
            target_dir = os.path.join(MODEL_PATH, model["folder"])
            os.makedirs(target_dir, exist_ok=True)

            # Use mapping if it exists, otherwise use original filename
            file_name_only = filename_mappings.get(model["repo_id"], os.path.basename(model["filename"]))
            target_path = os.path.join(target_dir, file_name_only)

            if not os.path.exists(target_path):
                os.symlink(model_path, target_path)
                print(f"βœ… Linked: {file_name_only}")
            else:
                print(f"βœ… Already exists: {file_name_only}")
        except Exception as e:
            print(f"❌ Failed to download {model['filename']}: {e}")


def download_and_extract_antelopev2():
    """Download and extract AntelopeV2 model for insightface."""
    import zipfile, requests
    base_path = os.path.join(BASE_PATH, "models/insightface/models")
    download_url = "https://huggingface.co/MonsterMMORPG/tools/resolve/main/antelopev2.zip"
    zip_path = os.path.join(base_path, "antelopev2.zip")
    extract_path = os.path.join(base_path, "antelopev2")

    os.makedirs(base_path, exist_ok=True)

    if not os.path.exists(extract_path):
        print(f"πŸ“₯ Downloading AntelopeV2 model...")
        try:
            response = requests.get(download_url, stream=True)
            response.raise_for_status()
            with open(zip_path, "wb") as file:
                for chunk in response.iter_content(chunk_size=8192):
                    file.write(chunk)
            print("βœ… Download complete.")

            print("πŸ“‚ Extracting AntelopeV2 model...")
            with zipfile.ZipFile(zip_path, "r") as zip_ref:
                zip_ref.extractall(base_path)
            print("βœ… Extraction complete.")

            os.remove(zip_path)
            print("πŸ—‘οΈ Cleaned up ZIP file.")
        except Exception as e:
            print(f"❌ Failed to download/extract AntelopeV2: {e}")
    else:
        print("βœ… AntelopeV2 model already exists")


def install_custom_nodes():
    """Install all custom nodes for ComfyUI."""
    # List of custom nodes to install via comfy node install
    custom_nodes = [
        "comfyui_essentials",
        "comfyui-detail-daemon",
        "comfyui-advancedliveportrait",
        "comfyui-impact-pack",
        "comfyui-custom-scripts",
        "rgthree-comfy",
        "comfyui-easy-use",
        "comfyui-florence2",
        "comfyui-kjnodes",
        "cg-use-everywhere",
        "comfyui-impact-subpack",
        "pulid_comfyui",
        "comfyui_pulid_flux_ll",
        "comfyui_birefnet_ll",
        "comfyui_controlnet_aux"
    ]

    os.chdir(BASE_PATH)
    # First update all existing nodes
    run_command("comfy node update all")

    # Then install any missing nodes
    for node in custom_nodes:
        run_command(f"comfy node install {node}")

    print("βœ… Installed and updated all ComfyUI registry nodes.")

    # List of custom nodes to install from git
    custom_nodes_git = [
        {
            "repo": "https://github.com/WASasquatch/was-node-suite-comfyui.git",
            "name": "was-node-suite-comfyui",
            "requirements": True
        },
        {
            "repo": "https://github.com/ssitu/ComfyUI_UltimateSDUpscale.git",
            "name": "ComfyUI_UltimateSDUpscale",
            "submodules": True
        },
        {
            "repo": "https://github.com/huanngzh/ComfyUI-MVAdapter",
            "name": "ComfyUI-MVAdapter",
            "requirements": True
        },
        {
            "repo": "https://github.com/sipie800/ComfyUI-PuLID-Flux-Enhanced.git",
            "name": "ComfyUI-PuLID-Flux-Enhanced",
            "requirements": True
        },
        {
            "repo": "https://github.com/liusida/ComfyUI-AutoCropFaces.git",
            "name": "ComfyUI-AutoCropFaces",
            "submodules": True
        },
        {
            "repo": "https://github.com/giriss/comfy-image-saver.git",
            "name": "comfy-image-saver",
            "requirements": True
        },
        {
            "repo": "https://github.com/spacepxl/ComfyUI-Image-Filters.git",
            "name": "ComfyUI-Image-Filters",
            "requirements": True
        },
        {
            "repo": "https://github.com/pydn/ComfyUI-to-Python-Extension.git",
            "name": "ComfyUI-to-Python-Extension",
            "requirements": True
        },
        {
            "repo": "https://github.com/Limitex/ComfyUI-Diffusers.git",
            "name": "ComfyUI-Diffusers",
            "requirements": True,
            "post_install": [
                "git clone https://github.com/cumulo-autumn/StreamDiffusion.git",
                "python -m streamdiffusion.tools.install-tensorrt"
            ]
        },
        {
            "repo": "https://github.com/Vaibhavs10/ComfyUI-DDUF.git",
            "name": "ComfyUI-DDUF",
            "requirements": True
        },
        {
            "repo": "https://github.com/Chaoses-Ib/ComfyScript.git",
            "name": "ComfyScript",
            "post_install": [
                "python -m pip install -e \".[default]\""
            ]
        }
    ]

    # Install nodes from git
    os.chdir(os.path.join(BASE_PATH, "custom_nodes"))
    for node in custom_nodes_git:
        repo_name = node["name"]
        repo_path = os.path.join(BASE_PATH, "custom_nodes", repo_name)
        manage_git_repo(
            node["repo"],
            repo_path,
            requirements=node.get("requirements", False),
            submodules=node.get("submodules", False)
        )

        # Handle any post-install commands
        if "post_install" in node:
            os.chdir(repo_path)
            for command in node["post_install"]:
                run_command(command)


def install_comfyui_manager():
    """Install ComfyUI Manager."""
    manager_path = os.path.join(BASE_PATH, "custom_nodes", "ComfyUI-Manager")
    manage_git_repo(
        "https://github.com/ltdrdata/ComfyUI-Manager",
        manager_path,
        requirements=True
    )


if __name__ == "__main__":
    # install_dependencies()
    install_comfyui()
    install_comfyui_manager()
    # download_huggingface_models()
    # install_custom_nodes()
    print("πŸŽ‰ Setup Complete! Run `run.py` to start ComfyUI.")