Spaces:
Sleeping
Sleeping
Upload lora_library.py
Browse files- lora_library.py +48 -22
lora_library.py
CHANGED
|
@@ -236,22 +236,15 @@ def _volume_mount_path() -> str:
|
|
| 236 |
return str(_mount_cache["path"])
|
| 237 |
_mount_cache["path"] = ""
|
| 238 |
for path in _runtime_volume_paths() + _mount_table_paths():
|
| 239 |
-
if path and os.path.isdir(path)
|
| 240 |
_mount_cache["path"] = path
|
| 241 |
print(f"[lora-library] using the volume mounted at {path}", flush=True)
|
| 242 |
break
|
| 243 |
return str(_mount_cache["path"])
|
| 244 |
|
| 245 |
|
| 246 |
-
def
|
| 247 |
-
"""
|
| 248 |
-
|
| 249 |
-
A Space can have ordinary persistent storage at /data *and* a bucket attached
|
| 250 |
-
somewhere else, and both are writable. Picking whichever came first in the list
|
| 251 |
-
means a Space with persistent storage quietly reads and writes its own private
|
| 252 |
-
/data/library.json and never sees the shared one - the list looks empty however
|
| 253 |
-
often it is refreshed. So: a directory that already holds the file wins, and a
|
| 254 |
-
plain /data is tried only when nothing else is there at all."""
|
| 255 |
bucket_name = BUCKET.strip("/").split("/")[-1]
|
| 256 |
candidates = [LOCAL_DIR] if LOCAL_DIR else []
|
| 257 |
candidates.append(_volume_mount_path())
|
|
@@ -264,16 +257,31 @@ def _mounted_file() -> str:
|
|
| 264 |
f"/storage/{bucket_name}", "/storage",
|
| 265 |
"/data",
|
| 266 |
]
|
| 267 |
-
|
| 268 |
for directory in candidates:
|
| 269 |
-
if directory and
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
path = os.path.join(directory, LIBRARY_FILE)
|
| 274 |
-
if os.path.exists(path):
|
| 275 |
return path
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
| 277 |
|
| 278 |
|
| 279 |
def _hub_has_buckets() -> bool:
|
|
@@ -361,7 +369,7 @@ def _read_mounted():
|
|
| 361 |
|
| 362 |
|
| 363 |
def _write_mounted(payload: str) -> None:
|
| 364 |
-
mounted = _mounted_file()
|
| 365 |
if not mounted:
|
| 366 |
raise RuntimeError("no mounted bucket")
|
| 367 |
with open(mounted, "w", encoding="utf-8") as handle:
|
|
@@ -545,6 +553,7 @@ def load_library() -> dict:
|
|
| 545 |
ordered = ([b for b in _BACKENDS if b[0] == _active["backend"]]
|
| 546 |
+ [b for b in _BACKENDS if b[0] != _active["backend"]])
|
| 547 |
first_error = ""
|
|
|
|
| 548 |
for name, reader, _writer in ordered:
|
| 549 |
try:
|
| 550 |
data = reader()
|
|
@@ -554,9 +563,17 @@ def load_library() -> dict:
|
|
| 554 |
continue
|
| 555 |
if data is None:
|
| 556 |
continue
|
| 557 |
-
if data["normal"] or data["nsfw"]
|
| 558 |
_active["backend"], _active["detail"] = name, ""
|
| 559 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
_active["detail"] = first_error
|
| 561 |
return _empty()
|
| 562 |
|
|
@@ -597,9 +614,18 @@ def save_library(library: dict) -> str:
|
|
| 597 |
def storage_note() -> str:
|
| 598 |
if _active["backend"] and not _active["detail"]:
|
| 599 |
name = _mounted_name() if _active["backend"] == "mounted bucket" else _active["backend"]
|
| 600 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 601 |
if _active["detail"]:
|
| 602 |
-
return f"storage: not working yet
|
| 603 |
return "storage: not contacted yet"
|
| 604 |
|
| 605 |
|
|
|
|
| 236 |
return str(_mount_cache["path"])
|
| 237 |
_mount_cache["path"] = ""
|
| 238 |
for path in _runtime_volume_paths() + _mount_table_paths():
|
| 239 |
+
if path and os.path.isdir(path):
|
| 240 |
_mount_cache["path"] = path
|
| 241 |
print(f"[lora-library] using the volume mounted at {path}", flush=True)
|
| 242 |
break
|
| 243 |
return str(_mount_cache["path"])
|
| 244 |
|
| 245 |
|
| 246 |
+
def _mount_candidates() -> list:
|
| 247 |
+
"""Every folder an attached bucket could be sitting in, best guess first."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
bucket_name = BUCKET.strip("/").split("/")[-1]
|
| 249 |
candidates = [LOCAL_DIR] if LOCAL_DIR else []
|
| 250 |
candidates.append(_volume_mount_path())
|
|
|
|
| 257 |
f"/storage/{bucket_name}", "/storage",
|
| 258 |
"/data",
|
| 259 |
]
|
| 260 |
+
out = []
|
| 261 |
for directory in candidates:
|
| 262 |
+
if directory and directory not in out and os.path.isdir(directory):
|
| 263 |
+
out.append(directory)
|
| 264 |
+
return out
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
def _mounted_file(require_write: bool = False) -> str:
|
| 268 |
+
"""The library file on a locally mounted bucket, or "" when there is none.
|
| 269 |
+
|
| 270 |
+
Two rules, both learned the hard way. A folder that already holds the file wins, because
|
| 271 |
+
a Space can have several writable folders - persistent storage, a scratch mount, the bucket -
|
| 272 |
+
and only one of them is the shared list; taking whichever came first meant reading an empty
|
| 273 |
+
file forever and no amount of refreshing helped. And reading only needs the folder to be
|
| 274 |
+
readable: demanding write access made a read-only or oddly-permissioned mount look absent,
|
| 275 |
+
which dropped the whole thing through to the empty dataset fallback."""
|
| 276 |
+
folders = _mount_candidates()
|
| 277 |
+
for directory in folders:
|
| 278 |
path = os.path.join(directory, LIBRARY_FILE)
|
| 279 |
+
if os.path.exists(path) and (not require_write or os.access(directory, os.W_OK)):
|
| 280 |
return path
|
| 281 |
+
for directory in folders:
|
| 282 |
+
if os.access(directory, os.W_OK):
|
| 283 |
+
return os.path.join(directory, LIBRARY_FILE)
|
| 284 |
+
return ""
|
| 285 |
|
| 286 |
|
| 287 |
def _hub_has_buckets() -> bool:
|
|
|
|
| 369 |
|
| 370 |
|
| 371 |
def _write_mounted(payload: str) -> None:
|
| 372 |
+
mounted = _mounted_file(require_write=True)
|
| 373 |
if not mounted:
|
| 374 |
raise RuntimeError("no mounted bucket")
|
| 375 |
with open(mounted, "w", encoding="utf-8") as handle:
|
|
|
|
| 553 |
ordered = ([b for b in _BACKENDS if b[0] == _active["backend"]]
|
| 554 |
+ [b for b in _BACKENDS if b[0] != _active["backend"]])
|
| 555 |
first_error = ""
|
| 556 |
+
fallback = None
|
| 557 |
for name, reader, _writer in ordered:
|
| 558 |
try:
|
| 559 |
data = reader()
|
|
|
|
| 563 |
continue
|
| 564 |
if data is None:
|
| 565 |
continue
|
| 566 |
+
if data["normal"] or data["nsfw"]:
|
| 567 |
_active["backend"], _active["detail"] = name, ""
|
| 568 |
+
return data
|
| 569 |
+
# An empty answer is not an answer. The remembered backend used to be asked first and
|
| 570 |
+
# its empty result returned straight away, so a single bad first read stuck forever and
|
| 571 |
+
# the real bucket was never tried again. Keep the empty one aside and carry on looking.
|
| 572 |
+
if fallback is None:
|
| 573 |
+
fallback = (name, data)
|
| 574 |
+
if fallback is not None:
|
| 575 |
+
_active["backend"], _active["detail"] = fallback[0], ""
|
| 576 |
+
return fallback[1]
|
| 577 |
_active["detail"] = first_error
|
| 578 |
return _empty()
|
| 579 |
|
|
|
|
| 614 |
def storage_note() -> str:
|
| 615 |
if _active["backend"] and not _active["detail"]:
|
| 616 |
name = _mounted_name() if _active["backend"] == "mounted bucket" else _active["backend"]
|
| 617 |
+
line = f"storage: {name}"
|
| 618 |
+
if _active["backend"] != "mounted bucket":
|
| 619 |
+
# Anything other than the mounted bucket means the mount was not found or could
|
| 620 |
+
# not be read, and that is almost always why the list comes back empty. Say where
|
| 621 |
+
# it looked, so the mount path in the Space settings can be checked against it.
|
| 622 |
+
looked = ", ".join(f"`{d}`" for d in _mount_candidates()) or "nothing"
|
| 623 |
+
line += (f" \n<sub>the attached bucket was not used. folders checked: {looked}. "
|
| 624 |
+
f"if your bucket is mounted somewhere else, set `LORA_LIBRARY_DIR` to that "
|
| 625 |
+
f"path in the Space settings.</sub>")
|
| 626 |
+
return line
|
| 627 |
if _active["detail"]:
|
| 628 |
+
return f"storage: not working yet - {_active['detail']}"
|
| 629 |
return "storage: not contacted yet"
|
| 630 |
|
| 631 |
|