Update salia_get_lora_x.py
Browse files- salia_get_lora_x.py +527 -319
salia_get_lora_x.py
CHANGED
|
@@ -1,320 +1,528 @@
|
|
| 1 |
-
import logging
|
| 2 |
-
import os
|
| 3 |
-
import
|
| 4 |
-
import urllib.
|
| 5 |
-
import urllib.
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
import
|
| 9 |
-
|
| 10 |
-
import
|
| 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 |
-
if
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
os.
|
| 51 |
-
except
|
| 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 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
}
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import os
|
| 3 |
+
import time
|
| 4 |
+
import urllib.error
|
| 5 |
+
import urllib.parse
|
| 6 |
+
import urllib.request
|
| 7 |
+
|
| 8 |
+
from safetensors import safe_open
|
| 9 |
+
|
| 10 |
+
import comfy.controlnet
|
| 11 |
+
import comfy.sd
|
| 12 |
+
import comfy.utils
|
| 13 |
+
import folder_paths
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
HF_REPO_BASE = "https://huggingface.co/saliacoel/x/resolve/main"
|
| 17 |
+
HF_LORA_REPO_BASE = HF_REPO_BASE
|
| 18 |
+
HF_CHECKPOINT_REPO_BASE = HF_REPO_BASE
|
| 19 |
+
HF_CONTROLNET_REPO_BASE = HF_REPO_BASE
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class _SaliaRepoFileMixin:
|
| 23 |
+
DOWNLOAD_CHUNK_SIZE = 8 * 1024 * 1024
|
| 24 |
+
DOWNLOAD_TIMEOUT_SECONDS = 300
|
| 25 |
+
MAX_DOWNLOAD_ATTEMPTS = 3
|
| 26 |
+
MIN_SAFETENSORS_SIZE = 16
|
| 27 |
+
|
| 28 |
+
@staticmethod
|
| 29 |
+
def _normalize_safetensors_name(name: str) -> str:
|
| 30 |
+
raw = str(name or "").strip().strip('"').strip("'").replace("\\", "/")
|
| 31 |
+
base = os.path.basename(raw).strip()
|
| 32 |
+
|
| 33 |
+
if not base:
|
| 34 |
+
raise ValueError("filename cannot be empty.")
|
| 35 |
+
|
| 36 |
+
if base.lower().endswith(".safetensors"):
|
| 37 |
+
base = base[: -len(".safetensors")].strip()
|
| 38 |
+
|
| 39 |
+
if not base:
|
| 40 |
+
raise ValueError("filename resolves to an empty name.")
|
| 41 |
+
|
| 42 |
+
return f"{base}.safetensors"
|
| 43 |
+
|
| 44 |
+
@classmethod
|
| 45 |
+
def _validate_safetensors_file(cls, path: str):
|
| 46 |
+
if not os.path.isfile(path):
|
| 47 |
+
return False, "file does not exist"
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
size = os.path.getsize(path)
|
| 51 |
+
except OSError as e:
|
| 52 |
+
return False, f"cannot stat file: {e}"
|
| 53 |
+
|
| 54 |
+
if size < cls.MIN_SAFETENSORS_SIZE:
|
| 55 |
+
return False, f"file is too small: {size} bytes"
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
with safe_open(path, framework="pt", device="cpu") as f:
|
| 59 |
+
# Force header parsing. This does not load tensor payloads.
|
| 60 |
+
_ = list(f.keys())
|
| 61 |
+
return True, ""
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return False, f"{type(e).__name__}: {e}"
|
| 64 |
+
|
| 65 |
+
@staticmethod
|
| 66 |
+
def _unique_invalid_path(path: str) -> str:
|
| 67 |
+
stamp = time.strftime("%Y%m%d-%H%M%S")
|
| 68 |
+
candidate = f"{path}.invalid-{stamp}"
|
| 69 |
+
counter = 1
|
| 70 |
+
|
| 71 |
+
while os.path.exists(candidate):
|
| 72 |
+
candidate = f"{path}.invalid-{stamp}.{counter}"
|
| 73 |
+
counter += 1
|
| 74 |
+
|
| 75 |
+
return candidate
|
| 76 |
+
|
| 77 |
+
@classmethod
|
| 78 |
+
def _quarantine_invalid_file(cls, path: str, reason: str, log_prefix: str) -> None:
|
| 79 |
+
if not os.path.isfile(path):
|
| 80 |
+
return
|
| 81 |
+
|
| 82 |
+
bad_path = cls._unique_invalid_path(path)
|
| 83 |
+
|
| 84 |
+
try:
|
| 85 |
+
os.replace(path, bad_path)
|
| 86 |
+
logging.warning(
|
| 87 |
+
"[%s] Existing safetensors file is invalid and was moved to: %s. Reason: %s",
|
| 88 |
+
log_prefix,
|
| 89 |
+
bad_path,
|
| 90 |
+
reason,
|
| 91 |
+
)
|
| 92 |
+
except Exception:
|
| 93 |
+
logging.warning(
|
| 94 |
+
"[%s] Existing safetensors file is invalid but could not be moved: %s. "
|
| 95 |
+
"Reason: %s",
|
| 96 |
+
log_prefix,
|
| 97 |
+
path,
|
| 98 |
+
reason,
|
| 99 |
+
exc_info=True,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
@classmethod
|
| 103 |
+
def _download_file(cls, url: str, target_path: str) -> int:
|
| 104 |
+
os.makedirs(os.path.dirname(target_path), exist_ok=True)
|
| 105 |
+
|
| 106 |
+
tmp_path = f"{target_path}.download.{os.getpid()}"
|
| 107 |
+
if os.path.exists(tmp_path):
|
| 108 |
+
os.remove(tmp_path)
|
| 109 |
+
|
| 110 |
+
bytes_written = 0
|
| 111 |
+
|
| 112 |
+
try:
|
| 113 |
+
request = urllib.request.Request(
|
| 114 |
+
url,
|
| 115 |
+
headers={
|
| 116 |
+
"User-Agent": "ComfyUI-SaliacoelRepoModelLoader/2.0",
|
| 117 |
+
"Accept": "application/octet-stream,*/*",
|
| 118 |
+
},
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
with urllib.request.urlopen(
|
| 122 |
+
request,
|
| 123 |
+
timeout=cls.DOWNLOAD_TIMEOUT_SECONDS,
|
| 124 |
+
) as response:
|
| 125 |
+
status = getattr(response, "status", None) or response.getcode()
|
| 126 |
+
if status < 200 or status >= 300:
|
| 127 |
+
raise RuntimeError(f"HTTP status {status}")
|
| 128 |
+
|
| 129 |
+
expected_raw = response.headers.get("Content-Length")
|
| 130 |
+
expected_size = None
|
| 131 |
+
if expected_raw:
|
| 132 |
+
try:
|
| 133 |
+
expected_size = int(expected_raw)
|
| 134 |
+
except ValueError:
|
| 135 |
+
expected_size = None
|
| 136 |
+
|
| 137 |
+
with open(tmp_path, "wb") as out_file:
|
| 138 |
+
while True:
|
| 139 |
+
chunk = response.read(cls.DOWNLOAD_CHUNK_SIZE)
|
| 140 |
+
if not chunk:
|
| 141 |
+
break
|
| 142 |
+
|
| 143 |
+
bytes_written += len(chunk)
|
| 144 |
+
out_file.write(chunk)
|
| 145 |
+
|
| 146 |
+
out_file.flush()
|
| 147 |
+
os.fsync(out_file.fileno())
|
| 148 |
+
|
| 149 |
+
if expected_size is not None and bytes_written != expected_size:
|
| 150 |
+
raise RuntimeError(
|
| 151 |
+
f"incomplete HTTP download: got {bytes_written} bytes, "
|
| 152 |
+
f"expected {expected_size} bytes"
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
if bytes_written < cls.MIN_SAFETENSORS_SIZE:
|
| 156 |
+
raise RuntimeError(f"downloaded file is too small: {bytes_written} bytes")
|
| 157 |
+
|
| 158 |
+
os.replace(tmp_path, target_path)
|
| 159 |
+
return bytes_written
|
| 160 |
+
|
| 161 |
+
except Exception:
|
| 162 |
+
if os.path.exists(tmp_path):
|
| 163 |
+
try:
|
| 164 |
+
os.remove(tmp_path)
|
| 165 |
+
except OSError:
|
| 166 |
+
pass
|
| 167 |
+
raise
|
| 168 |
+
|
| 169 |
+
@staticmethod
|
| 170 |
+
def _preferred_model_dir(folder_name: str, default_subdir: str) -> str:
|
| 171 |
+
models_dir = getattr(folder_paths, "models_dir", None)
|
| 172 |
+
if models_dir:
|
| 173 |
+
return os.path.join(models_dir, default_subdir)
|
| 174 |
+
|
| 175 |
+
folder_dirs = folder_paths.get_folder_paths(folder_name)
|
| 176 |
+
if not folder_dirs:
|
| 177 |
+
raise RuntimeError(f"No ComfyUI '{folder_name}' folder is configured.")
|
| 178 |
+
|
| 179 |
+
return folder_dirs[0]
|
| 180 |
+
|
| 181 |
+
@staticmethod
|
| 182 |
+
def _candidate_paths(folder_name: str, preferred_path: str, file_name: str):
|
| 183 |
+
paths = []
|
| 184 |
+
|
| 185 |
+
def add(path):
|
| 186 |
+
if path and path not in paths:
|
| 187 |
+
paths.append(path)
|
| 188 |
+
|
| 189 |
+
add(preferred_path)
|
| 190 |
+
|
| 191 |
+
existing_path = folder_paths.get_full_path(folder_name, file_name)
|
| 192 |
+
add(existing_path)
|
| 193 |
+
|
| 194 |
+
return paths
|
| 195 |
+
|
| 196 |
+
@classmethod
|
| 197 |
+
def _download_and_validate(
|
| 198 |
+
cls,
|
| 199 |
+
repo_base: str,
|
| 200 |
+
file_name: str,
|
| 201 |
+
target_path: str,
|
| 202 |
+
log_prefix: str,
|
| 203 |
+
) -> str:
|
| 204 |
+
url = f"{repo_base.rstrip('/')}/{urllib.parse.quote(file_name, safe='')}"
|
| 205 |
+
last_error = None
|
| 206 |
+
|
| 207 |
+
for attempt in range(1, cls.MAX_DOWNLOAD_ATTEMPTS + 1):
|
| 208 |
+
try:
|
| 209 |
+
logging.info(
|
| 210 |
+
"[%s] Downloading %s to %s (attempt %s/%s)",
|
| 211 |
+
log_prefix,
|
| 212 |
+
url,
|
| 213 |
+
target_path,
|
| 214 |
+
attempt,
|
| 215 |
+
cls.MAX_DOWNLOAD_ATTEMPTS,
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
bytes_written = cls._download_file(url, target_path)
|
| 219 |
+
valid, reason = cls._validate_safetensors_file(target_path)
|
| 220 |
+
|
| 221 |
+
if valid:
|
| 222 |
+
logging.info(
|
| 223 |
+
"[%s] Downloaded and validated %s (%s bytes)",
|
| 224 |
+
log_prefix,
|
| 225 |
+
target_path,
|
| 226 |
+
bytes_written,
|
| 227 |
+
)
|
| 228 |
+
return target_path
|
| 229 |
+
|
| 230 |
+
last_error = RuntimeError(reason)
|
| 231 |
+
cls._quarantine_invalid_file(target_path, reason, log_prefix)
|
| 232 |
+
|
| 233 |
+
except urllib.error.HTTPError as e:
|
| 234 |
+
last_error = e
|
| 235 |
+
logging.warning(
|
| 236 |
+
"[%s] HTTP error while downloading %s: %s",
|
| 237 |
+
log_prefix,
|
| 238 |
+
url,
|
| 239 |
+
e,
|
| 240 |
+
)
|
| 241 |
+
except Exception as e:
|
| 242 |
+
last_error = e
|
| 243 |
+
logging.warning(
|
| 244 |
+
"[%s] Error while downloading/validating %s: %s",
|
| 245 |
+
log_prefix,
|
| 246 |
+
url,
|
| 247 |
+
e,
|
| 248 |
+
)
|
| 249 |
+
|
| 250 |
+
if attempt < cls.MAX_DOWNLOAD_ATTEMPTS:
|
| 251 |
+
time.sleep(1.0)
|
| 252 |
+
|
| 253 |
+
raise RuntimeError(
|
| 254 |
+
f"[{log_prefix}] Could not download a valid safetensors file named "
|
| 255 |
+
f"'{file_name}' from {repo_base}. Last error: {last_error}"
|
| 256 |
+
) from last_error
|
| 257 |
+
|
| 258 |
+
@classmethod
|
| 259 |
+
def _ensure_file_available(
|
| 260 |
+
cls,
|
| 261 |
+
folder_name: str,
|
| 262 |
+
default_subdir: str,
|
| 263 |
+
repo_base: str,
|
| 264 |
+
file_name: str,
|
| 265 |
+
log_prefix: str,
|
| 266 |
+
) -> str:
|
| 267 |
+
preferred_dir = cls._preferred_model_dir(folder_name, default_subdir)
|
| 268 |
+
preferred_path = os.path.join(preferred_dir, file_name)
|
| 269 |
+
|
| 270 |
+
for path in cls._candidate_paths(folder_name, preferred_path, file_name):
|
| 271 |
+
if not os.path.isfile(path):
|
| 272 |
+
continue
|
| 273 |
+
|
| 274 |
+
valid, reason = cls._validate_safetensors_file(path)
|
| 275 |
+
if valid:
|
| 276 |
+
logging.info("[%s] Using existing valid file: %s", log_prefix, path)
|
| 277 |
+
return path
|
| 278 |
+
|
| 279 |
+
cls._quarantine_invalid_file(path, reason, log_prefix)
|
| 280 |
+
|
| 281 |
+
return cls._download_and_validate(
|
| 282 |
+
repo_base=repo_base,
|
| 283 |
+
file_name=file_name,
|
| 284 |
+
target_path=preferred_path,
|
| 285 |
+
log_prefix=log_prefix,
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
class Salia_Get_Lora_X(_SaliaRepoFileMixin):
|
| 290 |
+
"""
|
| 291 |
+
Loads a single LoRA from the public Hugging Face repo `saliacoel/x`.
|
| 292 |
+
|
| 293 |
+
Behavior:
|
| 294 |
+
- Takes a single base name, e.g. `Fade_to_Black`
|
| 295 |
+
- Normalizes it to `Fade_to_Black.safetensors`
|
| 296 |
+
- Validates any existing local file
|
| 297 |
+
- If missing or invalid, downloads it atomically
|
| 298 |
+
- Loads it and applies it model-only to one MODEL input
|
| 299 |
+
"""
|
| 300 |
+
|
| 301 |
+
CATEGORY = "loaders/saliacoel"
|
| 302 |
+
FUNCTION = "load_lora"
|
| 303 |
+
RETURN_TYPES = ("MODEL",)
|
| 304 |
+
RETURN_NAMES = ("loaded_model",)
|
| 305 |
+
DESCRIPTION = (
|
| 306 |
+
"Loads a single LoRA from saliacoel/x. If missing or invalid locally, "
|
| 307 |
+
"it is downloaded first, then applied model-only."
|
| 308 |
+
)
|
| 309 |
+
OUTPUT_TOOLTIPS = ("model_in with the LoRA applied.",)
|
| 310 |
+
|
| 311 |
+
def __init__(self):
|
| 312 |
+
self.loaded_lora = None
|
| 313 |
+
|
| 314 |
+
@classmethod
|
| 315 |
+
def INPUT_TYPES(cls):
|
| 316 |
+
return {
|
| 317 |
+
"required": {
|
| 318 |
+
"filename": (
|
| 319 |
+
"STRING",
|
| 320 |
+
{
|
| 321 |
+
"default": "",
|
| 322 |
+
"multiline": False,
|
| 323 |
+
"placeholder": "Fade_to_Black",
|
| 324 |
+
"tooltip": "LoRA name without .safetensors",
|
| 325 |
+
},
|
| 326 |
+
),
|
| 327 |
+
"model_in": (
|
| 328 |
+
"MODEL",
|
| 329 |
+
{"tooltip": "The MODEL input that will receive the LoRA."},
|
| 330 |
+
),
|
| 331 |
+
"strength": (
|
| 332 |
+
"FLOAT",
|
| 333 |
+
{
|
| 334 |
+
"default": 1.0,
|
| 335 |
+
"min": -100.0,
|
| 336 |
+
"max": 100.0,
|
| 337 |
+
"step": 0.01,
|
| 338 |
+
"tooltip": "Strength used when applying the LoRA.",
|
| 339 |
+
},
|
| 340 |
+
),
|
| 341 |
+
}
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
@classmethod
|
| 345 |
+
def _ensure_lora_available(cls, lora_name: str) -> str:
|
| 346 |
+
return cls._ensure_file_available(
|
| 347 |
+
folder_name="loras",
|
| 348 |
+
default_subdir="loras",
|
| 349 |
+
repo_base=HF_LORA_REPO_BASE,
|
| 350 |
+
file_name=lora_name,
|
| 351 |
+
log_prefix="SaliacoelSingleRepoLora",
|
| 352 |
+
)
|
| 353 |
+
|
| 354 |
+
def _get_or_load_lora(self, lora_path: str):
|
| 355 |
+
if self.loaded_lora is not None and self.loaded_lora[0] == lora_path:
|
| 356 |
+
return self.loaded_lora[1]
|
| 357 |
+
|
| 358 |
+
lora = comfy.utils.load_torch_file(lora_path, safe_load=True)
|
| 359 |
+
self.loaded_lora = (lora_path, lora)
|
| 360 |
+
return lora
|
| 361 |
+
|
| 362 |
+
def _apply_lora_model_only(self, model, lora_path: str, strength: float):
|
| 363 |
+
if strength == 0:
|
| 364 |
+
return model
|
| 365 |
+
|
| 366 |
+
lora = self._get_or_load_lora(lora_path)
|
| 367 |
+
model_lora, _ = comfy.sd.load_lora_for_models(model, None, lora, strength, 0)
|
| 368 |
+
return model_lora
|
| 369 |
+
|
| 370 |
+
def load_lora(self, filename, model_in, strength):
|
| 371 |
+
lora_name = self._normalize_safetensors_name(filename)
|
| 372 |
+
lora_path = self._ensure_lora_available(lora_name)
|
| 373 |
+
loaded_model = self._apply_lora_model_only(model_in, lora_path, strength)
|
| 374 |
+
return (loaded_model,)
|
| 375 |
+
|
| 376 |
+
|
| 377 |
+
class Salia_Get_Checkpoint_X(_SaliaRepoFileMixin):
|
| 378 |
+
"""
|
| 379 |
+
Loads a single checkpoint from the public Hugging Face repo `saliacoel/x`.
|
| 380 |
+
|
| 381 |
+
Behavior:
|
| 382 |
+
- Takes a single base name, e.g. `my_model`
|
| 383 |
+
- Normalizes it to `my_model.safetensors`
|
| 384 |
+
- Validates any existing local file
|
| 385 |
+
- If missing or invalid, downloads it to /ComfyUI/models/checkpoints
|
| 386 |
+
- Loads it with the same core loader path used by CheckpointLoaderSimple
|
| 387 |
+
"""
|
| 388 |
+
|
| 389 |
+
CATEGORY = "loaders/saliacoel"
|
| 390 |
+
FUNCTION = "load_checkpoint"
|
| 391 |
+
RETURN_TYPES = ("MODEL", "CLIP", "VAE")
|
| 392 |
+
RETURN_NAMES = ("model", "clip", "vae")
|
| 393 |
+
DESCRIPTION = (
|
| 394 |
+
"Loads a single checkpoint from saliacoel/x. If missing or invalid locally, "
|
| 395 |
+
"it is downloaded first, then loaded like ComfyUI's CheckpointLoaderSimple."
|
| 396 |
+
)
|
| 397 |
+
OUTPUT_TOOLTIPS = (
|
| 398 |
+
"The model used for denoising latents.",
|
| 399 |
+
"The CLIP model used for encoding text prompts.",
|
| 400 |
+
"The VAE model used for encoding and decoding images to and from latent space.",
|
| 401 |
+
)
|
| 402 |
+
SEARCH_ALIASES = [
|
| 403 |
+
"load model",
|
| 404 |
+
"checkpoint",
|
| 405 |
+
"model loader",
|
| 406 |
+
"load checkpoint",
|
| 407 |
+
"ckpt",
|
| 408 |
+
"model",
|
| 409 |
+
]
|
| 410 |
+
|
| 411 |
+
@classmethod
|
| 412 |
+
def INPUT_TYPES(cls):
|
| 413 |
+
return {
|
| 414 |
+
"required": {
|
| 415 |
+
"filename": (
|
| 416 |
+
"STRING",
|
| 417 |
+
{
|
| 418 |
+
"default": "",
|
| 419 |
+
"multiline": False,
|
| 420 |
+
"placeholder": "your_checkpoint",
|
| 421 |
+
"tooltip": "Checkpoint name without .safetensors",
|
| 422 |
+
},
|
| 423 |
+
),
|
| 424 |
+
}
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
@classmethod
|
| 428 |
+
def _ensure_checkpoint_available(cls, ckpt_name: str) -> str:
|
| 429 |
+
return cls._ensure_file_available(
|
| 430 |
+
folder_name="checkpoints",
|
| 431 |
+
default_subdir="checkpoints",
|
| 432 |
+
repo_base=HF_CHECKPOINT_REPO_BASE,
|
| 433 |
+
file_name=ckpt_name,
|
| 434 |
+
log_prefix="SaliacoelSingleRepoCheckpoint",
|
| 435 |
+
)
|
| 436 |
+
|
| 437 |
+
def load_checkpoint(self, filename):
|
| 438 |
+
ckpt_name = self._normalize_safetensors_name(filename)
|
| 439 |
+
ckpt_path = self._ensure_checkpoint_available(ckpt_name)
|
| 440 |
+
|
| 441 |
+
out = comfy.sd.load_checkpoint_guess_config(
|
| 442 |
+
ckpt_path,
|
| 443 |
+
output_vae=True,
|
| 444 |
+
output_clip=True,
|
| 445 |
+
embedding_directory=folder_paths.get_folder_paths("embeddings"),
|
| 446 |
+
)
|
| 447 |
+
|
| 448 |
+
return out[:3]
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
class Salia_Get_ControlNet_Model_X(_SaliaRepoFileMixin):
|
| 452 |
+
"""
|
| 453 |
+
Loads a single ControlNet model from the public Hugging Face repo `saliacoel/x`.
|
| 454 |
+
|
| 455 |
+
Behavior:
|
| 456 |
+
- Takes a single base name, e.g. `depth_controlnet`
|
| 457 |
+
- Normalizes it to `depth_controlnet.safetensors`
|
| 458 |
+
- Validates any existing local file
|
| 459 |
+
- If missing or invalid, downloads it to /ComfyUI/models/controlnet
|
| 460 |
+
- Loads it with the same core loader path used by ControlNetLoader
|
| 461 |
+
"""
|
| 462 |
+
|
| 463 |
+
CATEGORY = "loaders/saliacoel"
|
| 464 |
+
FUNCTION = "load_controlnet"
|
| 465 |
+
RETURN_TYPES = ("CONTROL_NET",)
|
| 466 |
+
RETURN_NAMES = ("control_net",)
|
| 467 |
+
DESCRIPTION = (
|
| 468 |
+
"Loads a single ControlNet model from saliacoel/x. If missing or invalid locally, "
|
| 469 |
+
"it is downloaded first, then loaded like ComfyUI's ControlNetLoader."
|
| 470 |
+
)
|
| 471 |
+
SEARCH_ALIASES = [
|
| 472 |
+
"controlnet",
|
| 473 |
+
"control net",
|
| 474 |
+
"cn",
|
| 475 |
+
"load controlnet",
|
| 476 |
+
"controlnet loader",
|
| 477 |
+
]
|
| 478 |
+
|
| 479 |
+
@classmethod
|
| 480 |
+
def INPUT_TYPES(cls):
|
| 481 |
+
return {
|
| 482 |
+
"required": {
|
| 483 |
+
"filename": (
|
| 484 |
+
"STRING",
|
| 485 |
+
{
|
| 486 |
+
"default": "",
|
| 487 |
+
"multiline": False,
|
| 488 |
+
"placeholder": "your_controlnet",
|
| 489 |
+
"tooltip": "ControlNet model name without .safetensors",
|
| 490 |
+
},
|
| 491 |
+
),
|
| 492 |
+
}
|
| 493 |
+
}
|
| 494 |
+
|
| 495 |
+
@classmethod
|
| 496 |
+
def _ensure_controlnet_available(cls, controlnet_name: str) -> str:
|
| 497 |
+
return cls._ensure_file_available(
|
| 498 |
+
folder_name="controlnet",
|
| 499 |
+
default_subdir="controlnet",
|
| 500 |
+
repo_base=HF_CONTROLNET_REPO_BASE,
|
| 501 |
+
file_name=controlnet_name,
|
| 502 |
+
log_prefix="SaliacoelSingleRepoControlNet",
|
| 503 |
+
)
|
| 504 |
+
|
| 505 |
+
def load_controlnet(self, filename):
|
| 506 |
+
controlnet_name = self._normalize_safetensors_name(filename)
|
| 507 |
+
controlnet_path = self._ensure_controlnet_available(controlnet_name)
|
| 508 |
+
controlnet = comfy.controlnet.load_controlnet(controlnet_path)
|
| 509 |
+
|
| 510 |
+
if controlnet is None:
|
| 511 |
+
raise RuntimeError(
|
| 512 |
+
"ERROR: controlnet file is invalid and does not contain a valid controlnet model."
|
| 513 |
+
)
|
| 514 |
+
|
| 515 |
+
return (controlnet,)
|
| 516 |
+
|
| 517 |
+
|
| 518 |
+
NODE_CLASS_MAPPINGS = {
|
| 519 |
+
"Salia_Get_Lora_X": Salia_Get_Lora_X,
|
| 520 |
+
"Salia_Get_Checkpoint_X": Salia_Get_Checkpoint_X,
|
| 521 |
+
"Salia_Get_ControlNet_Model_X": Salia_Get_ControlNet_Model_X,
|
| 522 |
+
}
|
| 523 |
+
|
| 524 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 525 |
+
"Salia_Get_Lora_X": "Salia Get LoRa and Load LoRa X (1)",
|
| 526 |
+
"Salia_Get_Checkpoint_X": "Salia Get Checkpoint and Load Checkpoint X",
|
| 527 |
+
"Salia_Get_ControlNet_Model_X": "Salia Get ControlNet and Load ControlNet Model X",
|
| 528 |
}
|