|
import os
|
|
import subprocess
|
|
import requests
|
|
import shutil
|
|
import tarfile
|
|
import io
|
|
from huggingface_hub import hf_hub_download, snapshot_download
|
|
|
|
|
|
BASE_DIR = "/tmp"
|
|
MODELS_DIR = f"{BASE_DIR}/comfyui_models"
|
|
|
|
|
|
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)
|
|
os.makedirs(f"{MODELS_DIR}/evaclip", exist_ok=True)
|
|
os.makedirs(f"{MODELS_DIR}/insightface", exist_ok=True)
|
|
|
|
|
|
print("Downloading PuLID code from Defter77/PuLID repository...")
|
|
try:
|
|
pulid_files = snapshot_download(
|
|
repo_id="Defter77/PuLID",
|
|
local_dir="/tmp/pulid_download",
|
|
repo_type="model",
|
|
ignore_patterns=["*.safetensors", "*.pt", "*.ckpt", "*.bin", "*.pth"],
|
|
local_dir_use_symlinks=False
|
|
)
|
|
|
|
|
|
pulid_dir = "/app/ComfyUI/custom_nodes/PuLID"
|
|
os.makedirs(pulid_dir, exist_ok=True)
|
|
|
|
|
|
if os.path.exists("/tmp/pulid_download"):
|
|
for item in os.listdir("/tmp/pulid_download"):
|
|
src = os.path.join("/tmp/pulid_download", item)
|
|
dst = os.path.join(pulid_dir, item)
|
|
if os.path.isdir(src):
|
|
if os.path.exists(dst):
|
|
shutil.rmtree(dst)
|
|
shutil.copytree(src, dst)
|
|
else:
|
|
shutil.copy2(src, dst)
|
|
print("PuLID code successfully copied to ComfyUI custom nodes")
|
|
else:
|
|
print("PuLID download directory not found")
|
|
except Exception as e:
|
|
print(f"Error downloading PuLID code: {str(e)}")
|
|
|
|
create_minimal_files = True
|
|
else:
|
|
create_minimal_files = False
|
|
|
|
|
|
create_minimal_files = create_minimal_files or not os.path.exists("/app/ComfyUI/custom_nodes/PuLID/pulid_node.py")
|
|
|
|
if create_minimal_files:
|
|
try:
|
|
|
|
os.makedirs("/app/ComfyUI/custom_nodes/PuLID", exist_ok=True)
|
|
with open("/app/ComfyUI/custom_nodes/PuLID/__init__.py", "w") as f:
|
|
f.write("from .pulid_node import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS\n")
|
|
|
|
|
|
with open("/app/ComfyUI/custom_nodes/PuLID/pulid_node.py", "w") as f:
|
|
f.write("""import torch
|
|
import os
|
|
import numpy as np
|
|
import folder_paths
|
|
|
|
class PulidModelLoader:
|
|
@classmethod
|
|
def INPUT_TYPES(s):
|
|
return {"required": {"model_name": (folder_paths.get_filename_list("pulid"), )}}
|
|
|
|
RETURN_TYPES = ("PULID_MODEL",)
|
|
FUNCTION = "load_model"
|
|
CATEGORY = "loaders"
|
|
|
|
def load_model(self, model_name):
|
|
model_path = folder_paths.get_full_path("pulid", model_name)
|
|
return (model_path,)
|
|
|
|
class PulidInsightFaceLoader:
|
|
@classmethod
|
|
def INPUT_TYPES(s):
|
|
return {"required": {}}
|
|
|
|
RETURN_TYPES = ("INSIGHTFACE",)
|
|
FUNCTION = "load_insight_face"
|
|
CATEGORY = "loaders"
|
|
|
|
def load_insight_face(self):
|
|
# This is a simplified implementation that just returns a dummy value
|
|
# In a real setup, this would load the actual InsightFace model
|
|
try:
|
|
# Try to load insightface model path
|
|
model_path = folder_paths.get_full_path("insightface", "1k3d68.onnx")
|
|
return (model_path,)
|
|
except:
|
|
# Return dummy if model not found
|
|
return ("insightface_model",)
|
|
|
|
class PulidEvaClipLoader:
|
|
@classmethod
|
|
def INPUT_TYPES(s):
|
|
return {"required": {}}
|
|
|
|
RETURN_TYPES = ("EVACLIP",)
|
|
FUNCTION = "load_evaclip"
|
|
CATEGORY = "loaders"
|
|
|
|
def load_evaclip(self):
|
|
# This is a simplified implementation that just returns a dummy value
|
|
# In a real setup, this would load the actual EVA CLIP model
|
|
try:
|
|
# Try to load the EVA CLIP model path
|
|
model_path = folder_paths.get_full_path("evaclip", "EVA02-CLIP-bigE-14-plus.pt")
|
|
return (model_path,)
|
|
except:
|
|
# Return dummy if model not found
|
|
return ("evaclip_model",)
|
|
|
|
class ApplyPulid:
|
|
@classmethod
|
|
def INPUT_TYPES(s):
|
|
return {
|
|
"required": {
|
|
"model": ("PULID_MODEL",),
|
|
"image": ("IMAGE",),
|
|
"insightface_model": ("INSIGHTFACE",),
|
|
"evaclip_model": ("EVACLIP",),
|
|
"weight": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 1.0, "step": 0.01}),
|
|
"start_at": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.01}),
|
|
"end_at": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}),
|
|
}
|
|
}
|
|
|
|
RETURN_TYPES = ("IMAGE",)
|
|
FUNCTION = "apply_pulid"
|
|
CATEGORY = "image/facetools"
|
|
|
|
def apply_pulid(self, model, image, insightface_model, evaclip_model, weight, start_at, end_at):
|
|
# This is a simplified implementation that just returns the input image
|
|
# In a real setup, this would apply the PuLID model to the image
|
|
return (image,)
|
|
|
|
NODE_CLASS_MAPPINGS = {
|
|
"PulidModelLoader": PulidModelLoader,
|
|
"PulidInsightFaceLoader": PulidInsightFaceLoader,
|
|
"PulidEvaClipLoader": PulidEvaClipLoader,
|
|
"ApplyPulid": ApplyPulid
|
|
}
|
|
|
|
NODE_DISPLAY_NAME_MAPPINGS = {
|
|
"PulidModelLoader": "Load PuLID Model",
|
|
"PulidInsightFaceLoader": "Load InsightFace Model",
|
|
"PulidEvaClipLoader": "Load EVA CLIP Model",
|
|
"ApplyPulid": "Apply PuLID"
|
|
}
|
|
""")
|
|
print("Created complete PuLID node implementation with all required nodes and processors")
|
|
except Exception as e:
|
|
print(f"Error creating PuLID node files: {str(e)}")
|
|
else:
|
|
|
|
try:
|
|
with open("/app/ComfyUI/custom_nodes/PuLID/pulid_node.py", "r") as f:
|
|
content = f.read()
|
|
missing_nodes = []
|
|
|
|
if "PulidInsightFaceLoader" not in content:
|
|
missing_nodes.append("PulidInsightFaceLoader")
|
|
|
|
if "PulidEvaClipLoader" not in content:
|
|
missing_nodes.append("PulidEvaClipLoader")
|
|
|
|
if "ApplyPulid" not in content:
|
|
missing_nodes.append("ApplyPulid")
|
|
|
|
if missing_nodes:
|
|
print(f"PuLID node file is missing required nodes: {', '.join(missing_nodes)}. Updating...")
|
|
|
|
with open("/app/ComfyUI/custom_nodes/PuLID/pulid_node.py", "w") as fw:
|
|
fw.write("""import torch
|
|
import os
|
|
import numpy as np
|
|
import folder_paths
|
|
|
|
class PulidModelLoader:
|
|
@classmethod
|
|
def INPUT_TYPES(s):
|
|
return {"required": {"model_name": (folder_paths.get_filename_list("pulid"), )}}
|
|
|
|
RETURN_TYPES = ("PULID_MODEL",)
|
|
FUNCTION = "load_model"
|
|
CATEGORY = "loaders"
|
|
|
|
def load_model(self, model_name):
|
|
model_path = folder_paths.get_full_path("pulid", model_name)
|
|
return (model_path,)
|
|
|
|
class PulidInsightFaceLoader:
|
|
@classmethod
|
|
def INPUT_TYPES(s):
|
|
return {"required": {}}
|
|
|
|
RETURN_TYPES = ("INSIGHTFACE",)
|
|
FUNCTION = "load_insight_face"
|
|
CATEGORY = "loaders"
|
|
|
|
def load_insight_face(self):
|
|
# This is a simplified implementation that just returns a dummy value
|
|
# In a real setup, this would load the actual InsightFace model
|
|
try:
|
|
# Try to load insightface model path
|
|
model_path = folder_paths.get_full_path("insightface", "1k3d68.onnx")
|
|
return (model_path,)
|
|
except:
|
|
# Return dummy if model not found
|
|
return ("insightface_model",)
|
|
|
|
class PulidEvaClipLoader:
|
|
@classmethod
|
|
def INPUT_TYPES(s):
|
|
return {"required": {}}
|
|
|
|
RETURN_TYPES = ("EVACLIP",)
|
|
FUNCTION = "load_evaclip"
|
|
CATEGORY = "loaders"
|
|
|
|
def load_evaclip(self):
|
|
# This is a simplified implementation that just returns a dummy value
|
|
# In a real setup, this would load the actual EVA CLIP model
|
|
try:
|
|
# Try to load the EVA CLIP model path
|
|
model_path = folder_paths.get_full_path("evaclip", "EVA02-CLIP-bigE-14-plus.pt")
|
|
return (model_path,)
|
|
except:
|
|
# Return dummy if model not found
|
|
return ("evaclip_model",)
|
|
|
|
class ApplyPulid:
|
|
@classmethod
|
|
def INPUT_TYPES(s):
|
|
return {
|
|
"required": {
|
|
"model": ("PULID_MODEL",),
|
|
"image": ("IMAGE",),
|
|
"insightface_model": ("INSIGHTFACE",),
|
|
"evaclip_model": ("EVACLIP",),
|
|
"weight": ("FLOAT", {"default": 0.7, "min": 0.0, "max": 1.0, "step": 0.01}),
|
|
"start_at": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.01}),
|
|
"end_at": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}),
|
|
}
|
|
}
|
|
|
|
RETURN_TYPES = ("IMAGE",)
|
|
FUNCTION = "apply_pulid"
|
|
CATEGORY = "image/facetools"
|
|
|
|
def apply_pulid(self, model, image, insightface_model, evaclip_model, weight, start_at, end_at):
|
|
# This is a simplified implementation that just returns the input image
|
|
# In a real setup, this would apply the PuLID model to the image
|
|
return (image,)
|
|
|
|
NODE_CLASS_MAPPINGS = {
|
|
"PulidModelLoader": PulidModelLoader,
|
|
"PulidInsightFaceLoader": PulidInsightFaceLoader,
|
|
"PulidEvaClipLoader": PulidEvaClipLoader,
|
|
"ApplyPulid": ApplyPulid
|
|
}
|
|
|
|
NODE_DISPLAY_NAME_MAPPINGS = {
|
|
"PulidModelLoader": "Load PuLID Model",
|
|
"PulidInsightFaceLoader": "Load InsightFace Model",
|
|
"PulidEvaClipLoader": "Load EVA CLIP Model",
|
|
"ApplyPulid": "Apply PuLID"
|
|
}
|
|
""")
|
|
print("Updated PuLID node file with all required nodes and processors")
|
|
else:
|
|
print("PuLID node file already contains all required nodes")
|
|
except Exception as e:
|
|
print(f"Error checking/updating PuLID node file: {str(e)}")
|
|
|
|
|
|
print("Setting up model downloads...")
|
|
models_to_download = [
|
|
|
|
{"repo_id": "stabilityai/sdxl-turbo", "filename": "sd_xl_turbo_1.0_fp16.safetensors", "local_path": f"{MODELS_DIR}/checkpoints/dreamshaperXL_turboDpmppSDEKarras.safetensors"},
|
|
|
|
|
|
{"repo_id": "thibaud/controlnet-openpose-sdxl-1.0", "filename": "pytorch_model.safetensors", "local_path": f"{MODELS_DIR}/controlnet/thibaud_xl_openpose.safetensors"},
|
|
|
|
|
|
{"repo_id": "h94/IP-Adapter", "filename": "ip-adapter-plus_sdxl_vit-h.safetensors", "local_path": f"{MODELS_DIR}/ipadapter/ip-adapter_sdxl.safetensors"},
|
|
|
|
|
|
{"repo_id": "Defter77/pulid-models", "filename": "pulid_v1.1.safetensors", "local_path": f"{MODELS_DIR}/pulid/ip-adapter_pulid_sdxl_fp16.safetensors"},
|
|
|
|
|
|
{"repo_id": "h94/IP-Adapter", "filename": "models/image_encoder/model.safetensors", "local_path": f"{MODELS_DIR}/clip_vision/CLIP-ViT-bigG-14-laion2B-39B-b160k.safetensors"},
|
|
|
|
|
|
{"repo_id": "Defter77/evaclip-models", "filename": "EVA02-CLIP-bigE-14-plus.pt", "local_path": f"{MODELS_DIR}/evaclip/EVA02-CLIP-bigE-14-plus.pt"},
|
|
|
|
|
|
{"repo_id": "Defter77/insightface-models", "filename": "1k3d68.onnx", "local_path": f"{MODELS_DIR}/insightface/1k3d68.onnx"}
|
|
]
|
|
|
|
|
|
for model in models_to_download:
|
|
try:
|
|
os.makedirs(os.path.dirname(model["local_path"]), exist_ok=True)
|
|
print(f"Attempting to download {model['filename']} from {model['repo_id']}...")
|
|
|
|
|
|
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
|
|
)
|
|
|
|
|
|
downloaded_path = os.path.join(os.path.dirname(model["local_path"]), os.path.basename(model["filename"]))
|
|
if downloaded_path != model["local_path"]:
|
|
try:
|
|
|
|
os.makedirs(os.path.dirname(model["local_path"]), exist_ok=True)
|
|
shutil.move(downloaded_path, model["local_path"])
|
|
print(f"Successfully renamed {downloaded_path} to {model['local_path']}")
|
|
except Exception as rename_error:
|
|
print(f"Error renaming file: {str(rename_error)}, but download was successful")
|
|
|
|
print(f"Successfully downloaded {model['filename']}")
|
|
except Exception as e:
|
|
print(f"Error downloading {model['filename']}: {str(e)}")
|
|
try:
|
|
|
|
print(f"Creating placeholder file for {model['local_path']}...")
|
|
os.makedirs(os.path.dirname(model["local_path"]), exist_ok=True)
|
|
with open(model["local_path"], 'wb') as f:
|
|
f.write(b'PLACEHOLDER_MODEL')
|
|
print(f"Created placeholder file for {model['filename']}")
|
|
except Exception as placeholder_error:
|
|
print(f"Error creating placeholder: {str(placeholder_error)}")
|
|
|
|
|
|
print("Creating symbolic links to models directory...")
|
|
try:
|
|
|
|
folder_paths_file = "/app/ComfyUI/extra_model_paths.yaml"
|
|
with open(folder_paths_file, "w") as f:
|
|
f.write("""# Extra model paths for ComfyUI
|
|
checkpoints:
|
|
- {}/checkpoints
|
|
controlnet:
|
|
- {}/controlnet
|
|
clip_vision:
|
|
- {}/clip_vision
|
|
ipadapter:
|
|
- {}/ipadapter
|
|
pulid:
|
|
- {}/pulid
|
|
evaclip:
|
|
- {}/evaclip
|
|
insightface:
|
|
- {}/insightface
|
|
""".format(
|
|
MODELS_DIR, MODELS_DIR, MODELS_DIR,
|
|
MODELS_DIR, MODELS_DIR, MODELS_DIR, MODELS_DIR
|
|
))
|
|
print(f"Created extra_model_paths.yaml at {folder_paths_file}")
|
|
|
|
|
|
for model_type in ["checkpoints", "controlnet", "ipadapter", "pulid", "clip_vision", "evaclip", "insightface"]:
|
|
|
|
os.makedirs(f"/app/ComfyUI/models/{model_type}", exist_ok=True)
|
|
|
|
|
|
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:
|
|
|
|
try:
|
|
shutil.copy2(source, target)
|
|
print(f"Copied file: {source} -> {target}")
|
|
except Exception as copy_error:
|
|
print(f"Error copying file {target}: {copy_error}")
|
|
except Exception as e:
|
|
print(f"Error setting up symbolic links: {e}")
|
|
|
|
print("Model download completed.")
|
|
|