Avatars / download_models.py
Defter77's picture
Upload download_models.py with huggingface_hub
3a580c7 verified
raw
history blame
3.57 kB
import os
import subprocess
import requests
from huggingface_hub import hf_hub_download
# Use writable directories
BASE_DIR = "/tmp"
MODELS_DIR = f"{BASE_DIR}/comfyui_models"
# Create directories
os.makedirs(f"{MODELS_DIR}/checkpoints", exist_ok=True)
os.makedirs(f"{MODELS_DIR}/controlnet", exist_ok=True)
os.makedirs(f"{MODELS_DIR}/ipadapter", exist_ok=True)
os.makedirs(f"{MODELS_DIR}/pulid", exist_ok=True)
os.makedirs(f"{MODELS_DIR}/clip_vision", exist_ok=True)
# List of models to download
models = [
# SDXL Checkpoint
{"repo_id": "Lykon/DreamShaper-XL-Turbo", "filename": "dreamshaperXL_turboDpmppSDEKarras.safetensors", "local_path": f"{MODELS_DIR}/checkpoints/dreamshaperXL_turboDpmppSDEKarras.safetensors"},
# ControlNet model
{"repo_id": "thibaud/controlnet-openpose-sdxl-1.0", "filename": "diffusion_pytorch_model.safetensors", "local_path": f"{MODELS_DIR}/controlnet/thibaud_xl_openpose.safetensors"},
# IP-Adapter
{"repo_id": "h94/IP-Adapter", "filename": "ip-adapter_sdxl.safetensors", "local_path": f"{MODELS_DIR}/ipadapter/ip-adapter_sdxl.safetensors"},
# PuLID model
{"repo_id": "l7dev/ip-adapter-pulid-sdxl", "filename": "ip-adapter_pulid_sdxl_fp16.safetensors", "local_path": f"{MODELS_DIR}/pulid/ip-adapter_pulid_sdxl_fp16.safetensors"},
# CLIP Vision
{"repo_id": "h94/IP-Adapter", "filename": "CLIP-ViT-bigG-14-laion2B-39B-b160k.safetensors", "local_path": f"{MODELS_DIR}/clip_vision/CLIP-ViT-bigG-14-laion2B-39B-b160k.safetensors"}
]
# Download each model
for model in models:
try:
os.makedirs(os.path.dirname(model["local_path"]), exist_ok=True)
print(f"Downloading {model['filename']} from {model['repo_id']}...")
# Get token from environment variable if available
token = os.environ.get("HF_TOKEN", None)
hf_hub_download(
repo_id=model["repo_id"],
filename=model["filename"],
local_dir=os.path.dirname(model["local_path"]),
local_dir_use_symlinks=False,
token=token
)
# Rename if necessary
downloaded_path = os.path.join(os.path.dirname(model["local_path"]), model["filename"])
if downloaded_path != model["local_path"]:
os.rename(downloaded_path, model["local_path"])
print(f"Successfully downloaded {model['filename']}")
except Exception as e:
print(f"Error downloading {model['filename']}: {str(e)}")
# Create symbolic links for standard ComfyUI paths
print("Creating symbolic links to models directory...")
try:
# Link from ComfyUI's default directories to our writable directory
for model_type in ["checkpoints", "controlnet", "ipadapter", "pulid", "clip_vision"]:
# Make the original directory if it doesn't exist
os.makedirs(f"/app/ComfyUI/models/{model_type}", exist_ok=True)
# Create symbolic links for each file
for filename in os.listdir(f"{MODELS_DIR}/{model_type}"):
source = f"{MODELS_DIR}/{model_type}/{filename}"
target = f"/app/ComfyUI/models/{model_type}/{filename}"
if not os.path.exists(target):
try:
os.symlink(source, target)
print(f"Created symlink: {target} -> {source}")
except Exception as e:
print(f"Error creating symlink {target}: {e}")
except Exception as e:
print(f"Error setting up symbolic links: {e}")
print("Model download completed.")