Spaces:
Running
Running
auto-cache sd-cli binary in vendor/ after first compile
Browse files- .gitattributes +1 -0
- app.py +36 -1
.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
vendor/sd-cli filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
|
@@ -38,6 +38,8 @@ os.makedirs(BIN_DIR, exist_ok=True)
|
|
| 38 |
SDCPP_REPO = "https://github.com/leejet/stable-diffusion.cpp"
|
| 39 |
SDCPP_COMMIT = "67dda3f"
|
| 40 |
_SDCLI_NAME = "sd-cli"
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
def _ensure_file(repo_id, filename, dest_dir):
|
|
@@ -88,8 +90,37 @@ def _build_sdcli():
|
|
| 88 |
print(f"[init] sd-cli compiled with OpenBLAS in {elapsed:.0f}s")
|
| 89 |
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
def _ensure_sdcli():
|
| 92 |
-
"""Find sd-cli in
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
for candidate in [
|
| 94 |
os.path.join(BIN_DIR, _SDCLI_NAME),
|
| 95 |
os.path.join(BIN_DIR, _SDCLI_NAME + ".exe"),
|
|
@@ -102,10 +133,14 @@ def _ensure_sdcli():
|
|
| 102 |
print("[init] sd-cli not in PATH. On Windows, build from source or add to PATH.")
|
| 103 |
sys.exit(1)
|
| 104 |
|
|
|
|
| 105 |
_build_sdcli()
|
| 106 |
cli_path = os.path.join(BIN_DIR, _SDCLI_NAME)
|
| 107 |
if not os.path.isfile(cli_path):
|
| 108 |
raise RuntimeError("sd-cli build failed")
|
|
|
|
|
|
|
|
|
|
| 109 |
return cli_path
|
| 110 |
|
| 111 |
|
|
|
|
| 38 |
SDCPP_REPO = "https://github.com/leejet/stable-diffusion.cpp"
|
| 39 |
SDCPP_COMMIT = "67dda3f"
|
| 40 |
_SDCLI_NAME = "sd-cli"
|
| 41 |
+
_VENDOR_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "vendor")
|
| 42 |
+
SPACE_REPO_ID = "Luminia/Anima-2B-CPU"
|
| 43 |
|
| 44 |
|
| 45 |
def _ensure_file(repo_id, filename, dest_dir):
|
|
|
|
| 90 |
print(f"[init] sd-cli compiled with OpenBLAS in {elapsed:.0f}s")
|
| 91 |
|
| 92 |
|
| 93 |
+
def _upload_sdcli_to_repo(binary_path):
|
| 94 |
+
"""Upload compiled sd-cli to the Space repo so next boot skips compilation."""
|
| 95 |
+
token = os.environ.get("HF_TOKEN")
|
| 96 |
+
if not token:
|
| 97 |
+
print("[init] No HF_TOKEN, skipping binary upload to repo")
|
| 98 |
+
return
|
| 99 |
+
try:
|
| 100 |
+
from huggingface_hub import HfApi
|
| 101 |
+
api = HfApi(token=token)
|
| 102 |
+
api.upload_file(
|
| 103 |
+
repo_id=SPACE_REPO_ID,
|
| 104 |
+
repo_type="space",
|
| 105 |
+
path_or_fileobj=binary_path,
|
| 106 |
+
path_in_repo="vendor/sd-cli",
|
| 107 |
+
commit_message="cache compiled sd-cli with OpenBLAS",
|
| 108 |
+
)
|
| 109 |
+
print("[init] Uploaded sd-cli to repo (next boot will skip compile)")
|
| 110 |
+
except Exception as e:
|
| 111 |
+
print(f"[init] Failed to upload sd-cli to repo: {e}")
|
| 112 |
+
|
| 113 |
+
|
| 114 |
def _ensure_sdcli():
|
| 115 |
+
"""Find sd-cli in vendor/, cache, PATH, or compile from source."""
|
| 116 |
+
# 1. Check vendor/ in the Space repo (pre-compiled, fastest)
|
| 117 |
+
vendor_bin = os.path.join(_VENDOR_DIR, _SDCLI_NAME)
|
| 118 |
+
if os.path.isfile(vendor_bin):
|
| 119 |
+
os.chmod(vendor_bin, os.stat(vendor_bin).st_mode | stat.S_IEXEC)
|
| 120 |
+
print(f"[init] Using cached sd-cli from vendor/")
|
| 121 |
+
return vendor_bin
|
| 122 |
+
|
| 123 |
+
# 2. Check /tmp cache or PATH
|
| 124 |
for candidate in [
|
| 125 |
os.path.join(BIN_DIR, _SDCLI_NAME),
|
| 126 |
os.path.join(BIN_DIR, _SDCLI_NAME + ".exe"),
|
|
|
|
| 133 |
print("[init] sd-cli not in PATH. On Windows, build from source or add to PATH.")
|
| 134 |
sys.exit(1)
|
| 135 |
|
| 136 |
+
# 3. Compile from source
|
| 137 |
_build_sdcli()
|
| 138 |
cli_path = os.path.join(BIN_DIR, _SDCLI_NAME)
|
| 139 |
if not os.path.isfile(cli_path):
|
| 140 |
raise RuntimeError("sd-cli build failed")
|
| 141 |
+
|
| 142 |
+
# 4. Upload to repo for next boot
|
| 143 |
+
_upload_sdcli_to_repo(cli_path)
|
| 144 |
return cli_path
|
| 145 |
|
| 146 |
|