wan-studio-pro / lora_loader.py
vimalprasad37
Issue fix2
25bca48
Raw History Blame Contribute Delete
20.6 kB
"""
LoRA Loader for WAN 2.2 I2V - single unified repo.
All LoRAs are served from iamgroot1212/wan2.2-loras.
Also resolves plain download links (CivitAI and anything else serving a
.safetensors), so a slot can take a URL instead of a repo file.
"""
import hashlib
import json
import os
import struct
import tempfile
import urllib.parse
import re
from huggingface_hub import hf_hub_download, list_repo_files
LORA_REPO = "vml12/wan2.2-loras"
HF_TOKEN = os.environ.get("HF_TOKEN")
PINNED_REVISIONS = {
"vml12/wan2.2-loras": "db0939c70da0",
}
# ---------------------------------------------------------------------------
# Build the catalog dynamically from the repo
# ---------------------------------------------------------------------------
# The repo files follow the pattern:
# wan2.2-i2v-high-<name>.safetensors
# wan2.2-i2v-low-<name>.safetensors
def _fetch_repo_safetensors():
"""Return a sorted list of .safetensors filenames from the unified repo."""
try:
files = list_repo_files(LORA_REPO, token=HF_TOKEN,
revision=PINNED_REVISIONS.get(LORA_REPO))
return sorted([f for f in files if f.lower().endswith(".safetensors")
and "wan2.1" not in f])
except Exception:
return []
def _load_local_catalog():
cat_path = os.path.join(os.path.dirname(__file__), "lora_catalog.json")
if os.path.exists(cat_path):
try:
with open(cat_path, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
pass
return None
def _make_display_name(key):
"""Convert kebab-case key to a nice display name."""
special = {
"aishaai": "AishaAI",
"bbc": "BBC",
"pov": "POV",
"ai": "AI",
"crm": "CRM",
"bj": "BJ",
"v1": "V1",
"v2": "V2",
"v3": "V3",
"v4": "V4",
"hd": "HD",
"fps": "FPS",
"fov": "FOV",
"i2v": "I2V",
"t2v": "T2V",
"hn": "HN",
"ln": "LN",
"nsfw": "NSFW",
"igoon": "IGoon",
"dr34ml4y": "Dr34ml4y",
"makeem": "MakeEm",
"aisha": "Aisha",
"mql": "MQL",
"k3nk": "K3nk",
"sid3l3g": "Sid3l3g",
}
parts = key.replace("_", "-").split("-")
result = []
for p in parts:
pl = p.lower()
if pl in special:
result.append(special[pl])
elif len(p) <= 3 and p.isalpha():
result.append(p.upper())
else:
result.append(p.capitalize())
return " ".join(result)
def _build_catalogs():
"""Build separated High and Low dictionaries.
Rule: If any lora text contains 'low', 'Low', etc. it should be shown
ONLY on the LoRA (low) field, not on the high field.
"""
safes = _fetch_repo_safetensors()
high_map = {}
low_map = {}
if safes:
for f in safes:
name_no_ext = f.replace(".safetensors", "")
m_high = re.match(r"wan2\.2-i2v-high-(.+)$", name_no_ext, re.IGNORECASE)
m_low = re.match(r"wan2\.2-i2v-low-(.+)$", name_no_ext, re.IGNORECASE)
if m_high:
raw_key = m_high.group(1)
disp = _make_display_name(raw_key)
# Any text containing 'low' (case-insensitive) goes strictly to Low only
if "low" in disp.lower() or "low" in raw_key.lower() or "low" in f.lower():
lbl = disp if disp not in low_map else f"{disp} (High file)"
low_map[lbl] = (LORA_REPO, f)
else:
high_map[disp] = (LORA_REPO, f)
elif m_low:
raw_key = m_low.group(1)
disp = _make_display_name(raw_key)
lbl = disp if disp not in low_map else f"{disp} (Low file)"
low_map[lbl] = (LORA_REPO, f)
else:
cat = _load_local_catalog()
if cat:
repo = cat.get("repo", LORA_REPO)
for disp, f in cat.get("high_loras", {}).items():
if "low" not in disp.lower() and "low" not in f.lower():
high_map[disp] = (repo, f)
else:
low_map[disp] = (repo, f)
for disp, f in cat.get("low_loras", {}).items():
low_map[disp] = (repo, f)
return high_map, low_map
HIGH_LORA_MAP, LOW_LORA_MAP = _build_catalogs()
def _build_high_to_low_map(high_map, low_map):
"""Map each High LoRA display name to its corresponding Low LoRA display name, if any."""
high_to_low = {}
low_by_suffix = {}
for l_disp, (repo, l_file) in low_map.items():
m = re.match(r"wan2\.2-i2v-low-(.+)\.safetensors$", l_file, re.IGNORECASE)
if m:
low_by_suffix[m.group(1).lower()] = l_disp
for h_disp, (repo, h_file) in high_map.items():
m = re.match(r"wan2\.2-i2v-high-(.+)\.safetensors$", h_file, re.IGNORECASE)
if m:
suf = m.group(1).lower()
if suf in low_by_suffix:
high_to_low[h_disp] = low_by_suffix[suf]
elif h_disp in low_map:
high_to_low[h_disp] = h_disp
elif h_disp in low_map:
high_to_low[h_disp] = h_disp
return high_to_low
HIGH_TO_LOW_MAP = _build_high_to_low_map(HIGH_LORA_MAP, LOW_LORA_MAP)
def get_low_counterpart(high_name):
"""Return the corresponding Low LoRA display name for a given High LoRA, or None."""
if not high_name or high_name == "(None)":
return None
clean = re.sub(r'\s*\(HIGH only\)|\s*\(LOW only\)|\s*\(High file\)|\s*\(Low file\)', '', str(high_name)).strip()
return HIGH_TO_LOW_MAP.get(high_name) or HIGH_TO_LOW_MAP.get(clean)
# Backward compatibility dictionaries
EXTRA = {}
for k, (repo, f) in HIGH_LORA_MAP.items():
EXTRA[k] = (repo, f, None)
for k, (repo, f) in LOW_LORA_MAP.items():
if k in EXTRA:
EXTRA[k] = (repo, EXTRA[k][1], f)
else:
EXTRA[k] = (repo, None, f)
LORA_PAIRS = {}
for label, (repo, hi, lo) in EXTRA.items():
LORA_PAIRS.setdefault(label, {"HIGH": None, "LOW": None})
if hi:
LORA_PAIRS[label]["HIGH"] = (repo, hi)
if lo:
LORA_PAIRS[label]["LOW"] = (repo, lo)
def get_high_lora_choices():
"""Choices for LoRA (High) dropdown: strictly excluding any entry containing 'low'."""
return ["(None)"] + [k for k in sorted(HIGH_LORA_MAP.keys()) if "low" not in k.lower()]
def get_low_lora_choices():
"""Choices for LoRA (Low) dropdown: all low entries and entries containing 'low'."""
return ["(None)"] + sorted(LOW_LORA_MAP.keys())
def get_lora_choices():
choices = ["(None)"]
for group in sorted(LORA_PAIRS.keys()):
p = LORA_PAIRS[group]
if p["HIGH"] and p["LOW"]:
choices.append(group)
elif p["HIGH"]:
choices.append(f"{group} (HIGH only)")
elif p["LOW"]:
choices.append(f"{group} (LOW only)")
return choices
def download_high_lora(name):
if not name or name == "(None)":
return None
clean = re.sub(r'\s*\(HIGH only\)|\s*\(LOW only\)|\s*\(High file\)|\s*\(Low file\)', '', name).strip()
entry = HIGH_LORA_MAP.get(name) or HIGH_LORA_MAP.get(clean)
if entry:
repo, fn = entry
return hf_hub_download(repo, fn, token=HF_TOKEN, revision=PINNED_REVISIONS.get(repo))
return None
def download_low_lora(name):
if not name or name == "(None)":
return None
clean = re.sub(r'\s*\(HIGH only\)|\s*\(LOW only\)|\s*\(High file\)|\s*\(Low file\)', '', name).strip()
entry = LOW_LORA_MAP.get(name) or LOW_LORA_MAP.get(clean)
if entry:
repo, fn = entry
return hf_hub_download(repo, fn, token=HF_TOKEN, revision=PINNED_REVISIONS.get(repo))
return None
def download_lora(group_name):
if not group_name or group_name == "(None)":
return None, None
clean_name = re.sub(r'\s*\(HIGH only\)|\s*\(LOW only\)|\s*\(High file\)|\s*\(Low file\)', '', group_name).strip()
high_path = download_high_lora(group_name) or download_high_lora(clean_name)
low_path = download_low_lora(group_name) or download_low_lora(clean_name)
return high_path, low_path
def parse_repo_link(link: str):
if not link:
return None
link = link.strip().strip('"').strip("'")
m = re.match(r"https?://huggingface\.co/(?:models/)?([^/]+)/([^/]+)", link)
if m:
return f"{m.group(1)}/{m.group(2)}"
if not link.startswith("http") and link.count("/") == 1:
return link
return None
def parse_hf_file_link(link: str):
if not link:
return None, None
link = link.strip().strip('"').strip("'")
m = re.match(
r"https?://huggingface\.co/([^/]+)/([^/]+)/(?:resolve|blob)/[^/]+/(.+)",
link,
)
if m:
return f"{m.group(1)}/{m.group(2)}", urllib.parse.unquote(m.group(3).split("?")[0])
if not link.startswith("http") and link.count("/") >= 2:
parts = link.split("/")
return f"{parts[0]}/{parts[1]}", "/".join(parts[2:])
return None, None
def list_loras_from_repo(repo_link: str):
"""Files in a Hugging Face repo. A direct download link is handed back as the
only choice, so pasting a CivitAI URL into the repo box fills the dropdowns."""
link = (repo_link or "").strip()
if is_direct_link(link):
return ["(None)", link]
repo_id = parse_repo_link(link)
if not repo_id:
return ["(None)"]
try:
files = list_repo_files(repo_id, token=HF_TOKEN,
revision=PINNED_REVISIONS.get(repo_id))
safes = sorted([f for f in files if f.lower().endswith(".safetensors")])
return ["(None)"] + safes if safes else ["(None)"]
except Exception as e:
print(f"list_loras_from_repo error: {e}")
return ["(None)"]
# ----------------------------------------------------------------------------
# Direct links (CivitAI and any other plain .safetensors URL)
# ----------------------------------------------------------------------------
# The download runs on the Space's machine, not in the visitor's browser, so a
# CivitAI session in a browser tab does not authenticate it: a gated model
# answers a server with an HTML login page instead of weights. CIVITAI_TOKEN
# (Settings -> Variables and secrets) is appended automatically when it is set,
# and the file header is checked, so a login page fails with a sentence that
# says what to do rather than a parse error deep inside safetensors.
CIVITAI_HOSTS = ("civitai.com", "civitai.red", "civitai.green", "civitai.work")
CIVITAI_TOKEN = os.environ.get("CIVITAI_TOKEN", "").strip()
def is_direct_link(text: str) -> bool:
"""A URL that is not a huggingface.co link, so a plain file download."""
text = (text or "").strip().strip('"').strip("'")
if not text.startswith(("http://", "https://")):
return False
host = (urllib.parse.urlparse(text).hostname or "").lower()
return not host.endswith("huggingface.co")
def _civitai_alternatives(url: str):
"""The same download through each CivitAI domain in turn. They mirror one another, and one of
them will sometimes answer a datacentre IP with a Cloudflare challenge while another does not."""
yield url
host = (urllib.parse.urlparse(url).hostname or "").lower()
if any(host.endswith(known) for known in CIVITAI_HOSTS):
for other in CIVITAI_HOSTS:
if not host.endswith(other):
yield url.replace(host, other, 1)
def download_direct_link(url: str) -> str:
import requests
url = url.strip().strip('"').strip("'")
cache_dir = os.path.join(tempfile.gettempdir(), "url-loras")
os.makedirs(cache_dir, exist_ok=True)
cached = os.path.join(cache_dir, hashlib.sha256(url.encode()).hexdigest()[:16] + ".safetensors")
if os.path.exists(cached) and os.path.getsize(cached) > 1_000_000:
return cached
response, host, last = None, "", None
for candidate in _civitai_alternatives(url):
host = (urllib.parse.urlparse(candidate).hostname or "").lower()
request_url = candidate
if CIVITAI_TOKEN and any(host.endswith(known) for known in CIVITAI_HOSTS) and "token=" not in candidate:
request_url = candidate + ("&" if "?" in candidate else "?") + f"token={CIVITAI_TOKEN}"
try:
attempt = requests.get(request_url, stream=True, timeout=180,
headers={"User-Agent": "Mozilla/5.0"})
attempt.raise_for_status()
except Exception as error:
last = f"{type(error).__name__} from {host}"
print(f"[lora] {last}; trying another CivitAI domain")
continue
if "text/html" in (attempt.headers.get("content-type") or "").lower():
last = f"a web page instead of a file from {host}"
print(f"[lora] {last}; trying another CivitAI domain")
continue
response = attempt
break
if response is None:
raise ValueError(
f"Could not fetch that link ({last}). CivitAI sometimes puts a Cloudflare challenge in "
"front of a datacentre address, and a gated model needs CIVITAI_TOKEN under Settings -> "
"Variables and secrets. Downloading the file yourself and uploading it always works."
)
written = 0
with open(cached, "wb") as handle:
for chunk in response.iter_content(chunk_size=1 << 20):
if chunk:
handle.write(chunk)
written += len(chunk)
if written < 1_000_000:
os.remove(cached)
raise ValueError(
"That link returned only a few kilobytes - almost always a login or error page rather "
"than weights. Check the link, or add CIVITAI_TOKEN to the Space."
)
# safetensors starts with an 8-byte little-endian header length, then that much JSON.
with open(cached, "rb") as handle:
length = struct.unpack("<Q", handle.read(8))[0]
if not (0 < length < 100_000_000):
os.remove(cached)
raise ValueError("The downloaded file is not a .safetensors (bad header).")
try:
json.loads(handle.read(length).decode("utf-8"))
except Exception:
os.remove(cached)
raise ValueError("The downloaded file is not a .safetensors (unreadable header).")
print(f"Downloaded {written / 1e6:.0f} MB from {host}")
return cached
def download_repo_file(repo_link: str, filename: str):
if not filename or filename == "(None)":
return None
# A local file (already downloaded, or uploaded into the Space) is used as it is.
if os.path.exists(filename):
return filename
# A plain download link, in either box.
if is_direct_link(filename):
return download_direct_link(filename)
if is_direct_link(repo_link) and filename == repo_link:
return download_direct_link(repo_link)
repo_from_file, weight = parse_hf_file_link(filename)
if repo_from_file and weight:
return hf_hub_download(
repo_from_file, weight, token=HF_TOKEN,
revision=PINNED_REVISIONS.get(repo_from_file),
)
repo_id = parse_repo_link(repo_link)
if not repo_id:
raise ValueError(f"Invalid repo/file: repo={repo_link}, file={filename}")
return hf_hub_download(
repo_id, filename, token=HF_TOKEN,
revision=PINNED_REVISIONS.get(repo_id),
)
def _strip_lora_bias(state_dict):
"""Drop the adapter's bias terms.
This transformer is quantized to fp8 with torchao, and `TorchaoLoraLinear` has no path for a
LoRA that carries bias - PEFT refuses the whole adapter with "does not support lora_bias".
The bias is a vector per layer against full weight matrices, so dropping it costs very little
and is the difference between the adapter working and not loading at all.
"""
dropped = {key: value for key, value in state_dict.items()
if not (key.endswith(".bias") or key.endswith(".diff_b"))}
removed = len(state_dict) - len(dropped)
if removed:
print(f"Dropped {removed} bias tensors: a torchao-quantized layer cannot take them")
return dropped
def _load_path(pipe, path, adapter_name, into_t2=False):
kwargs = {"adapter_name": adapter_name}
if into_t2:
kwargs["load_into_transformer_2"] = True
try:
pipe.load_lora_weights(path, **kwargs)
return adapter_name
except Exception as error:
if "lora_bias" not in str(error):
raise
print(f"Retrying {os.path.basename(str(path))} without its bias tensors ({error})")
# PEFT may already have registered the name before it refused, and a second load under the same
# name then fails for an unrelated reason. Clear it, and fall back to a fresh name if it will
# not clear.
try:
pipe.delete_adapters(adapter_name)
except Exception:
pass
from safetensors.torch import load_file
state_dict = _strip_lora_bias(load_file(path))
try:
pipe.load_lora_weights(state_dict, **kwargs)
return adapter_name
except Exception as error:
if "already" not in str(error).lower():
raise
kwargs["adapter_name"] = adapter_name = f"{adapter_name}_nb"
pipe.load_lora_weights(state_dict, **kwargs)
return adapter_name
def load_lora_to_pipe(pipe, group_name, adapter_name="lora", high_scale=1.0, low_scale=1.0):
if not group_name or group_name == "(None)":
return [], []
high_path, low_path = download_lora(group_name)
names, weights = [], []
if high_path:
names.append(_load_path(pipe, high_path, f"{adapter_name}_high", into_t2=False))
weights.append(float(high_scale))
if low_path:
names.append(_load_path(pipe, low_path, f"{adapter_name}_low", into_t2=True))
weights.append(float(low_scale))
if names:
print(f"Loaded preset LoRA: {group_name} scales={weights}")
return names, weights
def load_lora_slots(pipe, slots):
all_names, all_weights = [], []
for idx, slot in enumerate(slots or []):
if not slot:
continue
hs = float(slot.get("high_scale", 1.0) if slot.get("high_scale") is not None else 1.0)
ls = float(slot.get("low_scale", 1.0) if slot.get("low_scale") is not None else 1.0)
# Check for separated High / Low presets
ph = slot.get("preset_high") or slot.get("high_preset")
pl = slot.get("preset_low") or slot.get("low_preset")
preset = slot.get("preset")
if ph or pl:
if ph and ph != "(None)":
path_h = download_high_lora(ph)
if path_h:
all_names.append(_load_path(pipe, path_h, f"lora_{idx}_high", into_t2=False))
all_weights.append(hs)
print(f"Loaded preset HIGH: {ph} scale={hs}")
if pl and pl != "(None)":
path_l = download_low_lora(pl)
if path_l:
all_names.append(_load_path(pipe, path_l, f"lora_{idx}_low", into_t2=True))
all_weights.append(ls)
print(f"Loaded preset LOW: {pl} scale={ls}")
continue
if preset and preset != "(None)":
names, weights = load_lora_to_pipe(
pipe, preset, adapter_name=f"lora_{idx}",
high_scale=hs,
low_scale=ls,
)
all_names.extend(names)
all_weights.extend(weights)
continue
repo = slot.get("repo")
fh = slot.get("file_high")
fl = slot.get("file_low")
if fh and fh != "(None)":
path = download_repo_file(repo, fh)
all_names.append(_load_path(pipe, path, f"link_{idx}_high", into_t2=False))
all_weights.append(hs)
print(f"Loaded link HIGH: {fh} scale={hs}")
if fl and fl != "(None)":
path = download_repo_file(repo, fl)
all_names.append(_load_path(pipe, path, f"link_{idx}_low", into_t2=True))
all_weights.append(ls)
print(f"Loaded link LOW: {fl} scale={ls}")
if all_names:
pipe.set_adapters(all_names, adapter_weights=all_weights)
print(f"Active adapters: {list(zip(all_names, all_weights))}")
return True
return False
def unload_lora(pipe):
try:
pipe.unload_lora_weights()
except Exception:
pass