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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import subprocess
|
| 4 |
+
import zipfile
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
from huggingface_hub.utils import (
|
| 7 |
+
RepositoryNotFoundError,
|
| 8 |
+
EntryNotFoundError,
|
| 9 |
+
HfHubHTTPError,
|
| 10 |
+
LocalEntryNotFoundError
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def run_command(command):
|
| 14 |
+
"""Run a shell command and print its output"""
|
| 15 |
+
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
|
| 16 |
+
for line in process.stdout:
|
| 17 |
+
print(line, end='')
|
| 18 |
+
process.wait()
|
| 19 |
+
return process.returncode
|
| 20 |
+
|
| 21 |
+
def install_comfyui():
|
| 22 |
+
"""First-time installation of ComfyUI and models to Google Drive"""
|
| 23 |
+
# Mount Google Drive
|
| 24 |
+
print("Mounting Google Drive...")
|
| 25 |
+
from google.colab import drive
|
| 26 |
+
drive.mount('/content/drive')
|
| 27 |
+
|
| 28 |
+
# Create ComfyUI directory in Google Drive
|
| 29 |
+
COMFYUI_DRIVE_PATH = "/content/drive/MyDrive/ComfyUI"
|
| 30 |
+
os.makedirs(COMFYUI_DRIVE_PATH, exist_ok=True)
|
| 31 |
+
|
| 32 |
+
# Clone ComfyUI repository
|
| 33 |
+
print("\nCloning ComfyUI repository...")
|
| 34 |
+
run_command("git clone https://github.com/comfyanonymous/ComfyUI /content/ComfyUI")
|
| 35 |
+
|
| 36 |
+
# Create and activate virtual environment
|
| 37 |
+
print("\nSetting up virtual environment...")
|
| 38 |
+
run_command("cd /content/ComfyUI && python -m venv venv")
|
| 39 |
+
run_command("source /content/ComfyUI/venv/bin/activate && pip install --upgrade pip")
|
| 40 |
+
|
| 41 |
+
# Install PyTorch and dependencies
|
| 42 |
+
print("\nInstalling PyTorch and dependencies...")
|
| 43 |
+
run_command("source /content/ComfyUI/venv/bin/activate && pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121")
|
| 44 |
+
|
| 45 |
+
# Install additional packages
|
| 46 |
+
print("\nInstalling additional packages...")
|
| 47 |
+
packages = [
|
| 48 |
+
"insightface", "onnxruntime-gpu", "wheel", "setuptools", "packaging", "ninja",
|
| 49 |
+
"accelerate", "diffusers", "transformers", "mediapipe>=0.10.8", "huggingface-hub",
|
| 50 |
+
"omegaconf", "einops", "opencv-python", "face-alignment", "decord",
|
| 51 |
+
"ffmpeg-python>=0.2.0", "safetensors", "soundfile", "pytorch-lightning",
|
| 52 |
+
"deepspeed", "flash-attn", "triton==3.2.0"
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
for package in packages:
|
| 56 |
+
run_command(f"source /content/ComfyUI/venv/bin/activate && pip install {package}")
|
| 57 |
+
|
| 58 |
+
# Install requirements.txt
|
| 59 |
+
run_command("source /content/ComfyUI/venv/bin/activate && pip install -r /content/ComfyUI/requirements.txt")
|
| 60 |
+
|
| 61 |
+
# Install SageAttention
|
| 62 |
+
print("\nInstalling SageAttention...")
|
| 63 |
+
run_command("cd /content && git clone https://github.com/thu-ml/SageAttention")
|
| 64 |
+
run_command("cd /content/SageAttention && export MAX_JOBS=4 && python setup.py install")
|
| 65 |
+
|
| 66 |
+
# Create model directories
|
| 67 |
+
print("\nCreating model directories...")
|
| 68 |
+
model_dirs = ["checkpoints", "controlnet", "clip", "vae", "loras"]
|
| 69 |
+
for dir_name in model_dirs:
|
| 70 |
+
os.makedirs(os.path.join(COMFYUI_DRIVE_PATH, "models", dir_name), exist_ok=True)
|
| 71 |
+
|
| 72 |
+
# Download models
|
| 73 |
+
print("\nDownloading models...")
|
| 74 |
+
BASE_DOWNLOAD_DIR = os.path.join(COMFYUI_DRIVE_PATH, "models")
|
| 75 |
+
|
| 76 |
+
def download_and_process_item(repo_id, local_dir, filename, extract_and_delete=False, repo_type=None):
|
| 77 |
+
try:
|
| 78 |
+
print(f"Downloading {filename}...")
|
| 79 |
+
file_path = hf_hub_download(
|
| 80 |
+
repo_id=repo_id,
|
| 81 |
+
filename=filename,
|
| 82 |
+
repo_type=repo_type,
|
| 83 |
+
local_dir=local_dir
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
if extract_and_delete and filename.endswith('.zip'):
|
| 87 |
+
print(f"Extracting {filename}...")
|
| 88 |
+
with zipfile.ZipFile(file_path, 'r') as zip_ref:
|
| 89 |
+
zip_ref.extractall(local_dir)
|
| 90 |
+
os.remove(file_path)
|
| 91 |
+
print(f"Extracted and removed {filename}")
|
| 92 |
+
|
| 93 |
+
return True
|
| 94 |
+
except Exception as e:
|
| 95 |
+
print(f"Error downloading {filename}: {str(e)}")
|
| 96 |
+
return False
|
| 97 |
+
|
| 98 |
+
# Download tasks
|
| 99 |
+
flux_dev_tasks = [
|
| 100 |
+
# ControlNet models
|
| 101 |
+
{"repo_id": "simwalo/FluxDevFP8", "repo_type": "dataset", "filename": "Flux-Union-Pro2.safetensors", "local_dir": os.path.join(BASE_DOWNLOAD_DIR, "controlnet")},
|
| 102 |
+
{"repo_id": "simwalo/FluxDevFP8", "repo_type": "dataset", "filename": "Flux1-controlnet-upscaler-Jasperai-fp8.safetensors", "local_dir": os.path.join(BASE_DOWNLOAD_DIR, "controlnet")},
|
| 103 |
+
# CLIP models
|
| 104 |
+
{"repo_id": "simwalo/FluxDevFP8", "repo_type": "dataset", "filename": "clip_l.safetensors", "local_dir": os.path.join(BASE_DOWNLOAD_DIR, "clip")},
|
| 105 |
+
{"repo_id": "simwalo/FluxDevFP8", "repo_type": "dataset", "filename": "ViT-L-14-TEXT-detail-improved-hiT-GmP-TE-only-HF.safetensors", "local_dir": os.path.join(BASE_DOWNLOAD_DIR, "clip")},
|
| 106 |
+
# VAE models
|
| 107 |
+
{"repo_id": "simwalo/FluxDevFP8", "repo_type": "dataset", "filename": "ae.safetensors", "local_dir": os.path.join(BASE_DOWNLOAD_DIR, "vae")},
|
| 108 |
+
# LoRA models
|
| 109 |
+
{"repo_id": "simwalo/FluxDevFP8", "repo_type": "dataset", "filename": "comfyui_portrait_lora64.safetensors", "local_dir": os.path.join(BASE_DOWNLOAD_DIR, "loras")},
|
| 110 |
+
{"repo_id": "simwalo/FluxDevFP8", "repo_type": "dataset", "filename": "Maya_Lora_v1_000002500.safetensors", "local_dir": os.path.join(BASE_DOWNLOAD_DIR, "loras")},
|
| 111 |
+
{"repo_id": "simwalo/FluxDevFP8", "repo_type": "dataset", "filename": "more_details.safetensors", "local_dir": os.path.join(BASE_DOWNLOAD_DIR, "loras")},
|
| 112 |
+
{"repo_id": "simwalo/FluxDevFP8", "repo_type": "dataset", "filename": "FameGrid_Bold_SDXL_V1.safetensors", "local_dir": os.path.join(BASE_DOWNLOAD_DIR, "loras")},
|
| 113 |
+
# Checkpoints
|
| 114 |
+
{"repo_id": "simwalo/FluxDevFP8", "repo_type": "dataset", "filename": "epicrealism_naturalSinRC1VAE.safetensors", "local_dir": os.path.join(BASE_DOWNLOAD_DIR, "checkpoints")},
|
| 115 |
+
{"repo_id": "simwalo/FluxDevFP8", "repo_type": "dataset", "filename": "epicrealism_pureEvolutionV3.safetensors", "local_dir": os.path.join(BASE_DOWNLOAD_DIR, "checkpoints")},
|
| 116 |
+
{"repo_id": "simwalo/FluxDevFP8", "repo_type": "dataset", "filename": "epicrealismXL_vxviLastfameRealism.safetensors", "local_dir": os.path.join(BASE_DOWNLOAD_DIR, "checkpoints")}
|
| 117 |
+
]
|
| 118 |
+
|
| 119 |
+
wanskyreels_tasks = [
|
| 120 |
+
{"repo_id": "simwalo/WanSkyReelsFP8", "repo_type": "dataset", "filename": "wan2.1-fp8.safetensors", "local_dir": os.path.join(BASE_DOWNLOAD_DIR, "checkpoints")}
|
| 121 |
+
]
|
| 122 |
+
|
| 123 |
+
custom_nodes_tasks = [
|
| 124 |
+
{"repo_id": "simwalo/custom_nodes", "repo_type": "dataset", "filename": "PersonMaskUltraV2.zip", "local_dir": os.path.join(COMFYUI_DRIVE_PATH, "custom_nodes"), "extract_and_delete": True},
|
| 125 |
+
{"repo_id": "simwalo/custom_nodes", "repo_type": "dataset", "filename": "insightface.zip", "local_dir": os.path.join(COMFYUI_DRIVE_PATH, "custom_nodes"), "extract_and_delete": True},
|
| 126 |
+
{"repo_id": "simwalo/custom_nodes", "repo_type": "dataset", "filename": "liveportrait.zip", "local_dir": os.path.join(COMFYUI_DRIVE_PATH, "custom_nodes"), "extract_and_delete": True},
|
| 127 |
+
{"repo_id": "simwalo/custom_nodes", "repo_type": "dataset", "filename": "rembg.zip", "local_dir": os.path.join(COMFYUI_DRIVE_PATH, "custom_nodes"), "extract_and_delete": True},
|
| 128 |
+
{"repo_id": "simwalo/custom_nodes", "repo_type": "dataset", "filename": "LLM.zip", "local_dir": os.path.join(COMFYUI_DRIVE_PATH, "custom_nodes"), "extract_and_delete": True},
|
| 129 |
+
{"repo_id": "simwalo/custom_nodes", "repo_type": "dataset", "filename": "sams.zip", "local_dir": os.path.join(COMFYUI_DRIVE_PATH, "custom_nodes"), "extract_and_delete": True},
|
| 130 |
+
{"repo_id": "simwalo/custom_nodes", "repo_type": "dataset", "filename": "ComfyUI-LatentSyncWrapper.zip", "local_dir": os.path.join(COMFYUI_DRIVE_PATH, "custom_nodes"), "extract_and_delete": True}
|
| 131 |
+
]
|
| 132 |
+
|
| 133 |
+
# Download all models
|
| 134 |
+
for task in flux_dev_tasks + wanskyreels_tasks + custom_nodes_tasks:
|
| 135 |
+
download_and_process_item(**task)
|
| 136 |
+
|
| 137 |
+
# Copy ComfyUI to Google Drive
|
| 138 |
+
print("\nCopying ComfyUI to Google Drive...")
|
| 139 |
+
run_command(f"cp -r /content/ComfyUI/* {COMFYUI_DRIVE_PATH}/")
|
| 140 |
+
|
| 141 |
+
print("\nInstallation complete! You can now use the startup function to run ComfyUI.")
|
| 142 |
+
|
| 143 |
+
def start_comfyui():
|
| 144 |
+
"""Start ComfyUI using the installation from Google Drive"""
|
| 145 |
+
# Mount Google Drive
|
| 146 |
+
print("Mounting Google Drive...")
|
| 147 |
+
from google.colab import drive
|
| 148 |
+
drive.mount('/content/drive')
|
| 149 |
+
|
| 150 |
+
# Set paths
|
| 151 |
+
COMFYUI_DRIVE_PATH = "/content/drive/MyDrive/ComfyUI"
|
| 152 |
+
COMFYUI_LOCAL_PATH = "/content/ComfyUI"
|
| 153 |
+
|
| 154 |
+
# Create local directory
|
| 155 |
+
os.makedirs(COMFYUI_LOCAL_PATH, exist_ok=True)
|
| 156 |
+
|
| 157 |
+
# Copy ComfyUI from Drive to local
|
| 158 |
+
print("\nCopying ComfyUI from Google Drive...")
|
| 159 |
+
run_command(f"cp -r {COMFYUI_DRIVE_PATH}/* {COMFYUI_LOCAL_PATH}/")
|
| 160 |
+
|
| 161 |
+
# Create and activate virtual environment
|
| 162 |
+
print("\nSetting up virtual environment...")
|
| 163 |
+
run_command(f"cd {COMFYUI_LOCAL_PATH} && python -m venv venv")
|
| 164 |
+
run_command(f"source {COMFYUI_LOCAL_PATH}/venv/bin/activate && pip install --upgrade pip")
|
| 165 |
+
|
| 166 |
+
# Install PyTorch and dependencies
|
| 167 |
+
print("\nInstalling PyTorch and dependencies...")
|
| 168 |
+
run_command(f"source {COMFYUI_LOCAL_PATH}/venv/bin/activate && pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121")
|
| 169 |
+
|
| 170 |
+
# Install additional packages
|
| 171 |
+
print("\nInstalling additional packages...")
|
| 172 |
+
packages = [
|
| 173 |
+
"insightface", "onnxruntime-gpu", "wheel", "setuptools", "packaging", "ninja",
|
| 174 |
+
"accelerate", "diffusers", "transformers", "mediapipe>=0.10.8", "huggingface-hub",
|
| 175 |
+
"omegaconf", "einops", "opencv-python", "face-alignment", "decord",
|
| 176 |
+
"ffmpeg-python>=0.2.0", "safetensors", "soundfile", "pytorch-lightning",
|
| 177 |
+
"deepspeed", "flash-attn", "triton==3.2.0"
|
| 178 |
+
]
|
| 179 |
+
|
| 180 |
+
for package in packages:
|
| 181 |
+
run_command(f"source {COMFYUI_LOCAL_PATH}/venv/bin/activate && pip install {package}")
|
| 182 |
+
|
| 183 |
+
# Install requirements.txt
|
| 184 |
+
run_command(f"source {COMFYUI_LOCAL_PATH}/venv/bin/activate && pip install -r {COMFYUI_LOCAL_PATH}/requirements.txt")
|
| 185 |
+
|
| 186 |
+
# Install SageAttention
|
| 187 |
+
print("\nInstalling SageAttention...")
|
| 188 |
+
run_command("cd /content && git clone https://github.com/thu-ml/SageAttention")
|
| 189 |
+
run_command("cd /content/SageAttention && export MAX_JOBS=4 && python setup.py install")
|
| 190 |
+
|
| 191 |
+
# Start ComfyUI
|
| 192 |
+
print("\nStarting ComfyUI...")
|
| 193 |
+
run_command(f"cd {COMFYUI_LOCAL_PATH} && source venv/bin/activate && python main.py --fast --use-pytorch-cross-attention --listen")
|
| 194 |
+
|
| 195 |
+
if __name__ == "__main__":
|
| 196 |
+
print("ComfyUI Google Colab Helper")
|
| 197 |
+
print("1. First-time installation (saves to Google Drive)")
|
| 198 |
+
print("2. Quick startup (uses existing installation from Google Drive)")
|
| 199 |
+
|
| 200 |
+
choice = input("Enter your choice (1 or 2): ")
|
| 201 |
+
|
| 202 |
+
if choice == "1":
|
| 203 |
+
install_comfyui()
|
| 204 |
+
elif choice == "2":
|
| 205 |
+
start_comfyui()
|
| 206 |
+
else:
|
| 207 |
+
print("Invalid choice. Please enter 1 or 2.")
|