Spaces:
Running on Zero
Running on Zero
Add persistent custom LoRA library
Browse filesAdd a URL-driven safetensors library stored in the mounted bucket and expose saved LoRAs through the existing multi-select control.
- app.py +30 -1
- lora_loader.py +134 -15
app.py
CHANGED
|
@@ -670,6 +670,16 @@ def _render_selected_loras(lora_groups):
|
|
| 670 |
return "<span style='color:#888'>Selected LoRAs: <b>" + "</b>, <b>".join(selected) + "</b></span>"
|
| 671 |
|
| 672 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 673 |
GLASS_CSS = """
|
| 674 |
/* ββ Hidden timestamp (preserved) ββ */
|
| 675 |
#hidden-timestamp {
|
|
@@ -950,9 +960,22 @@ with gr.Blocks(delete_cache=(3600, 10800)) as demo:
|
|
| 950 |
step=0.1, value=10.0, label="Duration",
|
| 951 |
)
|
| 952 |
lora_dropdown = gr.Dropdown(
|
| 953 |
-
choices=lora_loader.
|
| 954 |
label="LoRAs", multiselect=True,
|
| 955 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 956 |
|
| 957 |
with gr.Row():
|
| 958 |
add_queue_btn = gr.Button("Add to Queue", variant="primary")
|
|
@@ -1137,6 +1160,12 @@ with gr.Blocks(delete_cache=(3600, 10800)) as demo:
|
|
| 1137 |
inputs=[lora_dropdown],
|
| 1138 |
outputs=[selected_lora_preview],
|
| 1139 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1140 |
|
| 1141 |
grab_frame_btn.click(
|
| 1142 |
fn=None, inputs=None, outputs=[timestamp_box],
|
|
|
|
| 670 |
return "<span style='color:#888'>Selected LoRAs: <b>" + "</b>, <b>".join(selected) + "</b></span>"
|
| 671 |
|
| 672 |
|
| 673 |
+
def save_custom_lora(label, url, stage, selected_loras):
|
| 674 |
+
"""Save a direct safetensors URL to the mounted Bucket and refresh UI."""
|
| 675 |
+
try:
|
| 676 |
+
message = lora_loader.add_custom_lora(label, url, stage)
|
| 677 |
+
selected = [x for x in (selected_loras or []) if x in lora_loader.get_all_lora_choices()]
|
| 678 |
+
return gr.update(choices=lora_loader.get_all_lora_choices(), value=selected), message, ""
|
| 679 |
+
except Exception as exc:
|
| 680 |
+
return gr.update(), f"Could not save LoRA: {exc}", url
|
| 681 |
+
|
| 682 |
+
|
| 683 |
GLASS_CSS = """
|
| 684 |
/* ββ Hidden timestamp (preserved) ββ */
|
| 685 |
#hidden-timestamp {
|
|
|
|
| 960 |
step=0.1, value=10.0, label="Duration",
|
| 961 |
)
|
| 962 |
lora_dropdown = gr.Dropdown(
|
| 963 |
+
choices=lora_loader.get_all_lora_choices(),
|
| 964 |
label="LoRAs", multiselect=True,
|
| 965 |
)
|
| 966 |
+
|
| 967 |
+
with gr.Accordion("Personal LoRA library", open=False):
|
| 968 |
+
gr.Markdown(
|
| 969 |
+
"Add a direct `.safetensors` endpoint once; it is copied to the Space bucket and then appears in the LoRA selector. "
|
| 970 |
+
"Use the same name twice to attach its HIGH and LOW stage files."
|
| 971 |
+
)
|
| 972 |
+
custom_lora_name = gr.Textbox(label="LoRA name", placeholder="My character / motion LoRA")
|
| 973 |
+
custom_lora_url = gr.Textbox(label="Direct .safetensors URL", placeholder="https://.../model.safetensors")
|
| 974 |
+
custom_lora_stage = gr.Dropdown(
|
| 975 |
+
label="Wan stage", choices=[("Auto from filename", "auto"), ("High-noise transformer", "high"), ("Low-noise transformer", "low")], value="auto"
|
| 976 |
+
)
|
| 977 |
+
save_custom_lora_button = gr.Button("Save to LoRA library", variant="secondary")
|
| 978 |
+
custom_lora_status = gr.Markdown()
|
| 979 |
|
| 980 |
with gr.Row():
|
| 981 |
add_queue_btn = gr.Button("Add to Queue", variant="primary")
|
|
|
|
| 1160 |
inputs=[lora_dropdown],
|
| 1161 |
outputs=[selected_lora_preview],
|
| 1162 |
)
|
| 1163 |
+
|
| 1164 |
+
save_custom_lora_button.click(
|
| 1165 |
+
fn=save_custom_lora,
|
| 1166 |
+
inputs=[custom_lora_name, custom_lora_url, custom_lora_stage, lora_dropdown],
|
| 1167 |
+
outputs=[lora_dropdown, custom_lora_status, custom_lora_url],
|
| 1168 |
+
)
|
| 1169 |
|
| 1170 |
grab_frame_btn.click(
|
| 1171 |
fn=None, inputs=None, outputs=[timestamp_box],
|
lora_loader.py
CHANGED
|
@@ -10,15 +10,26 @@ trigger phrase present in the prompt; concept categories dedupe output to at
|
|
| 10 |
most one LoRA per category. See LORA_AUTO_PHRASES / LORA_AUTO_CATEGORIES /
|
| 11 |
auto_activation_details below.
|
| 12 |
"""
|
| 13 |
-
import os
|
| 14 |
-
import urllib.parse
|
| 15 |
-
import re
|
| 16 |
-
import math
|
| 17 |
-
import
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
LORA_REPO = "lkzd7/WAN2.2_LoraSet_NSFW"
|
| 21 |
-
HF_TOKEN = os.environ.get("HF_TOKEN") # authenticated downloads (covers private repos)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Pinned commit hashes (2026-07-19) β protection from breaking upstream changes
|
| 24 |
PINNED_REVISIONS = {
|
|
@@ -154,7 +165,7 @@ for label, (repo, hi, lo) in EXTRA.items():
|
|
| 154 |
LORA_PAIRS[label]["LOW"] = (repo, lo)
|
| 155 |
|
| 156 |
|
| 157 |
-
def get_lora_choices():
|
| 158 |
choices = []
|
| 159 |
for group in sorted(LORA_PAIRS.keys()):
|
| 160 |
p = LORA_PAIRS[group]
|
|
@@ -164,7 +175,105 @@ def get_lora_choices():
|
|
| 164 |
choices.append(f"{group} (HIGH only)")
|
| 165 |
elif p["LOW"]:
|
| 166 |
choices.append(f"{group} (LOW only)")
|
| 167 |
-
return choices
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
|
| 170 |
# ββ Auto-activation: scored inverse-concept-frequency (ICF) βββββββββββββββ
|
|
@@ -1038,12 +1147,22 @@ def get_lora_prompt_additions(lora_groups):
|
|
| 1038 |
return triggers
|
| 1039 |
|
| 1040 |
|
| 1041 |
-
def download_lora(group_name):
|
| 1042 |
-
if not group_name:
|
| 1043 |
-
return None, None
|
| 1044 |
-
clean_name = re.sub(r'\s*\(HIGH only\)|\s*\(LOW only\)', '', group_name)
|
| 1045 |
-
if clean_name
|
| 1046 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1047 |
pair = LORA_PAIRS[clean_name]
|
| 1048 |
high_path, low_path = None, None
|
| 1049 |
if pair["HIGH"]:
|
|
|
|
| 10 |
most one LoRA per category. See LORA_AUTO_PHRASES / LORA_AUTO_CATEGORIES /
|
| 11 |
auto_activation_details below.
|
| 12 |
"""
|
| 13 |
+
import os
|
| 14 |
+
import urllib.parse
|
| 15 |
+
import re
|
| 16 |
+
import math
|
| 17 |
+
import json
|
| 18 |
+
import hashlib
|
| 19 |
+
import tempfile
|
| 20 |
+
import urllib.request
|
| 21 |
+
import safetensors.torch
|
| 22 |
+
from huggingface_hub import hf_hub_download
|
| 23 |
|
| 24 |
LORA_REPO = "lkzd7/WAN2.2_LoraSet_NSFW"
|
| 25 |
+
HF_TOKEN = os.environ.get("HF_TOKEN") # authenticated downloads (covers private repos)
|
| 26 |
+
|
| 27 |
+
# The Space mounts its persistent HF Bucket at /data. User-added LoRAs live
|
| 28 |
+
# here rather than in the ephemeral Hugging Face cache, so a LoRA fetched once
|
| 29 |
+
# remains available after the Space sleeps or restarts.
|
| 30 |
+
LORA_LIBRARY_DIR = os.environ.get("LORA_LIBRARY_DIR", "/data/loras")
|
| 31 |
+
LORA_LIBRARY_MANIFEST = os.path.join(LORA_LIBRARY_DIR, "library.json")
|
| 32 |
+
MAX_CUSTOM_LORA_BYTES = int(os.environ.get("MAX_CUSTOM_LORA_BYTES", 8 * 1024**3))
|
| 33 |
|
| 34 |
# Pinned commit hashes (2026-07-19) β protection from breaking upstream changes
|
| 35 |
PINNED_REVISIONS = {
|
|
|
|
| 165 |
LORA_PAIRS[label]["LOW"] = (repo, lo)
|
| 166 |
|
| 167 |
|
| 168 |
+
def get_lora_choices():
|
| 169 |
choices = []
|
| 170 |
for group in sorted(LORA_PAIRS.keys()):
|
| 171 |
p = LORA_PAIRS[group]
|
|
|
|
| 175 |
choices.append(f"{group} (HIGH only)")
|
| 176 |
elif p["LOW"]:
|
| 177 |
choices.append(f"{group} (LOW only)")
|
| 178 |
+
return choices
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
def _custom_key(label):
|
| 182 |
+
return f"User: {label}"
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def _read_custom_library():
|
| 186 |
+
"""Return the persisted custom LoRA manifest, tolerating a first run."""
|
| 187 |
+
try:
|
| 188 |
+
with open(LORA_LIBRARY_MANIFEST, "r", encoding="utf-8") as handle:
|
| 189 |
+
data = json.load(handle)
|
| 190 |
+
return data if isinstance(data, dict) else {}
|
| 191 |
+
except (OSError, ValueError, TypeError):
|
| 192 |
+
return {}
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def _write_custom_library(entries):
|
| 196 |
+
os.makedirs(LORA_LIBRARY_DIR, exist_ok=True)
|
| 197 |
+
fd, temporary_path = tempfile.mkstemp(prefix="library-", suffix=".json", dir=LORA_LIBRARY_DIR)
|
| 198 |
+
try:
|
| 199 |
+
with os.fdopen(fd, "w", encoding="utf-8") as handle:
|
| 200 |
+
json.dump(entries, handle, indent=2, sort_keys=True)
|
| 201 |
+
os.replace(temporary_path, LORA_LIBRARY_MANIFEST)
|
| 202 |
+
finally:
|
| 203 |
+
if os.path.exists(temporary_path):
|
| 204 |
+
os.unlink(temporary_path)
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
def get_custom_lora_choices():
|
| 208 |
+
return [_custom_key(label) for label in sorted(_read_custom_library(), key=str.casefold)]
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
def get_all_lora_choices():
|
| 212 |
+
return get_lora_choices() + get_custom_lora_choices()
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
def _stage_for_custom_lora(filename, requested_stage):
|
| 216 |
+
if requested_stage in {"high", "low"}:
|
| 217 |
+
return requested_stage.upper()
|
| 218 |
+
return "LOW" if re.search(r"(?:low|[_-]ln\\b|[_-]l\\b)", filename, re.IGNORECASE) else "HIGH"
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
def add_custom_lora(label, url, stage="auto"):
|
| 222 |
+
"""Fetch one public safetensors URL into the persistent LoRA library.
|
| 223 |
+
|
| 224 |
+
Reusing the same label lets the user add its HIGH and LOW stage files as a
|
| 225 |
+
pair. The download lands atomically, so an interrupted request never
|
| 226 |
+
leaves a selectable partial weight file in the Bucket.
|
| 227 |
+
"""
|
| 228 |
+
label = (label or "").strip()
|
| 229 |
+
url = (url or "").strip()
|
| 230 |
+
if not label:
|
| 231 |
+
raise ValueError("Give this LoRA a name.")
|
| 232 |
+
if not re.fullmatch(r"[\\w .()'\\-]{1,96}", label):
|
| 233 |
+
raise ValueError("LoRA names may use letters, numbers, spaces, . ( ) ' and -.")
|
| 234 |
+
parsed = urllib.parse.urlparse(url)
|
| 235 |
+
if parsed.scheme not in {"http", "https"} or not parsed.netloc:
|
| 236 |
+
raise ValueError("Use a direct http(s) URL to a .safetensors file.")
|
| 237 |
+
source_name = os.path.basename(urllib.parse.unquote(parsed.path))
|
| 238 |
+
if not source_name.lower().endswith(".safetensors"):
|
| 239 |
+
raise ValueError("The endpoint path must end in .safetensors.")
|
| 240 |
+
|
| 241 |
+
os.makedirs(LORA_LIBRARY_DIR, exist_ok=True)
|
| 242 |
+
digest = hashlib.sha256(url.encode("utf-8")).hexdigest()[:12]
|
| 243 |
+
safe_label = re.sub(r"[^A-Za-z0-9._-]+", "-", label).strip(".-") or "lora"
|
| 244 |
+
destination = os.path.join(LORA_LIBRARY_DIR, f"{safe_label}-{digest}.safetensors")
|
| 245 |
+
fd, temporary_path = tempfile.mkstemp(prefix="download-", suffix=".part", dir=LORA_LIBRARY_DIR)
|
| 246 |
+
size = 0
|
| 247 |
+
try:
|
| 248 |
+
request = urllib.request.Request(url, headers={"User-Agent": "wan2.2-lora-library/1.0"})
|
| 249 |
+
with urllib.request.urlopen(request, timeout=60) as response, os.fdopen(fd, "wb") as output:
|
| 250 |
+
length = response.headers.get("Content-Length")
|
| 251 |
+
if length and int(length) > MAX_CUSTOM_LORA_BYTES:
|
| 252 |
+
raise ValueError("That LoRA is larger than this Space's 8 GB library limit.")
|
| 253 |
+
while True:
|
| 254 |
+
chunk = response.read(1024 * 1024)
|
| 255 |
+
if not chunk:
|
| 256 |
+
break
|
| 257 |
+
size += len(chunk)
|
| 258 |
+
if size > MAX_CUSTOM_LORA_BYTES:
|
| 259 |
+
raise ValueError("That LoRA is larger than this Space's 8 GB library limit.")
|
| 260 |
+
output.write(chunk)
|
| 261 |
+
if size == 0:
|
| 262 |
+
raise ValueError("The LoRA endpoint returned an empty file.")
|
| 263 |
+
# Validate the safetensors header without materializing the weights.
|
| 264 |
+
with safetensors.safe_open(temporary_path, framework="pt") as weights:
|
| 265 |
+
if not list(weights.keys()):
|
| 266 |
+
raise ValueError("The downloaded safetensors file has no tensors.")
|
| 267 |
+
os.replace(temporary_path, destination)
|
| 268 |
+
entries = _read_custom_library()
|
| 269 |
+
entry = entries.setdefault(label, {"HIGH": None, "LOW": None})
|
| 270 |
+
entry[_stage_for_custom_lora(source_name, stage)] = {"path": destination, "url": url, "bytes": size}
|
| 271 |
+
_write_custom_library(entries)
|
| 272 |
+
except Exception:
|
| 273 |
+
if os.path.exists(temporary_path):
|
| 274 |
+
os.unlink(temporary_path)
|
| 275 |
+
raise
|
| 276 |
+
return f"Saved {label} ({size / 1024**2:.1f} MB) in the persistent LoRA library."
|
| 277 |
|
| 278 |
|
| 279 |
# ββ Auto-activation: scored inverse-concept-frequency (ICF) βββββββββββββββ
|
|
|
|
| 1147 |
return triggers
|
| 1148 |
|
| 1149 |
|
| 1150 |
+
def download_lora(group_name):
|
| 1151 |
+
if not group_name:
|
| 1152 |
+
return None, None
|
| 1153 |
+
clean_name = re.sub(r'\s*\(HIGH only\)|\s*\(LOW only\)', '', group_name)
|
| 1154 |
+
if clean_name.startswith("User: "):
|
| 1155 |
+
entry = _read_custom_library().get(clean_name[6:])
|
| 1156 |
+
if not entry:
|
| 1157 |
+
return None, None
|
| 1158 |
+
high = (entry.get("HIGH") or {}).get("path")
|
| 1159 |
+
low = (entry.get("LOW") or {}).get("path")
|
| 1160 |
+
# A manually removed Bucket file should disappear cleanly rather than
|
| 1161 |
+
# becoming a permanent failed selection.
|
| 1162 |
+
return (high if high and os.path.isfile(high) else None,
|
| 1163 |
+
low if low and os.path.isfile(low) else None)
|
| 1164 |
+
if clean_name not in LORA_PAIRS:
|
| 1165 |
+
return None, None
|
| 1166 |
pair = LORA_PAIRS[clean_name]
|
| 1167 |
high_path, low_path = None, None
|
| 1168 |
if pair["HIGH"]:
|