-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmodel_management.py
More file actions
76 lines (63 loc) · 3.05 KB
/
Copy pathmodel_management.py
File metadata and controls
76 lines (63 loc) · 3.05 KB
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
import os
import torch
import comfy.utils
from loguru import logger
import folder_paths
from huggingface_hub import hf_hub_download
# --- Constants ---
FOLEY_MODEL_NAMES = ["hunyuanvideo_foley.pth", "vae_128d_48k.pth", "synchformer_state_dict.pth"]
SIGLIP_MODEL_REPO = "google/siglip-base-patch16-512"
CLAP_MODEL_REPO = "laion/clap-htsat-unfused"
# --- Path Management ---
def get_model_dir(subfolder=""):
"""Returns the primary Foley models directory."""
return os.path.join(folder_paths.get_folder_paths("foley")[0], subfolder)
def get_full_model_path(model_name, subfolder=""):
"""Returns the full path for a given model name."""
return os.path.join(get_model_dir(subfolder), model_name)
# --- Core Functionality ---
def find_or_download(model_name, repo_id, subfolder="", subfolder_in_repo=""):
"""
Finds a model file, downloading it if it's not found in standard locations.
- Checks the main ComfyUI foley models directory first.
- Falls back to downloading from Hugging Face.
"""
local_path = get_full_model_path(model_name, subfolder)
if os.path.exists(local_path):
logger.info(f"Found local model: {local_path}")
return local_path
logger.warning(f"Could not find {model_name} locally. Attempting to download from {repo_id}...")
try:
downloaded_path = hf_hub_download(
repo_id=repo_id,
filename=model_name,
subfolder=subfolder_in_repo,
local_dir=get_model_dir(subfolder),
local_dir_use_symlinks=False
)
logger.info(f"Successfully downloaded model to: {downloaded_path}")
return downloaded_path
except Exception as e:
logger.error(f"Failed to download {model_name} from {repo_id}: {e}")
raise FileNotFoundError(f"Could not find or download {model_name}. Please check your connection or download it manually.")
def get_siglip_path():
"""Special handling for the SigLIP model which is a directory."""
return find_or_download_directory(repo_id=SIGLIP_MODEL_REPO, local_dir_name="siglip-base-patch16-512")
def get_clap_path():
"""Special handling for the CLAP model which is a directory."""
return find_or_download_directory(repo_id=CLAP_MODEL_REPO, local_dir_name="clap-htsat-unfused")
def find_or_download_directory(repo_id, local_dir_name):
"""
Finds a model directory, downloading it if it's not found.
This is for models like SigLIP that are not single files.
"""
local_path = get_model_dir(local_dir_name)
if os.path.exists(local_path) and os.listdir(local_path):
logger.info(f"Found local model directory: {local_path}")
return local_path
logger.warning(f"Could not find {local_dir_name} directory locally. Attempting to download from {repo_id}...")
# We can't use hf_hub_download for a whole directory in the same way,
# but the transformers library will handle this caching for us automatically
# when `from_pretrained` is called. We just need to return the repo_id.
# The actual "download" is implicit.
return repo_id