kink-discovery / scripts /deploy_hf_bootstrap_catalog.py
Perplexed7675's picture
Sync from kink_cli (Docker Space)
c6be2a7 verified
Raw
History Blame Contribute Delete
3.93 kB
#!/usr/bin/env python3
"""When B2 is capped: upload ``store_slim_b2.db`` to a Hub **dataset** and point the Space at it.
Uses ``[huggingface].token`` from parser ``secrets.toml``. Sets Space **variables** (not secrets):
``KINK_HF_REQUIRE_FULL_CATALOG=1``, ``KINK_HF_DATASET_REPO``, and
``KINK_HF_DATASET_FILENAME`` (default ``store_slim_bootstrap.db``) so the image downloads the
smaller full-catalog LFS object instead of copying the bundled demo seed.
After this, run ``python scripts/publish_hf_space.py --verify`` to push code and smoke the app.
Example::
export HF_SPACE_REPO=ronheichman/kink-discovery
python scripts/deploy_hf_bootstrap_catalog.py
"""
from __future__ import annotations
import argparse
import os
import sys
import tomllib
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
from backend.repo_dotenv import load_repo_dotenv
DEFAULT_SECRETS = Path(os.environ.get("KINK_PARSER_SECRETS_TOML", Path.home() / "PycharmProjects/parser/secrets.toml"))
def main() -> int:
load_repo_dotenv()
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("--secrets-toml", type=Path, default=DEFAULT_SECRETS)
ap.add_argument(
"--local-path",
type=Path,
default=ROOT / "data" / "store_slim_b2.db",
)
ap.add_argument("--repo-id", default="ronheichman/kink-catalog-slim")
ap.add_argument("--remote-filename", default="store_slim_bootstrap.db")
ap.add_argument("--skip-hub-upload", action="store_true")
args = ap.parse_args()
repo_id = (os.environ.get("HF_SPACE_REPO") or "").strip()
if not repo_id:
print("deploy_hf_bootstrap_catalog: set HF_SPACE_REPO=owner/slug", file=sys.stderr)
return 1
local = args.local_path.resolve()
if not local.is_file():
print(f"deploy_hf_bootstrap_catalog: missing {local}", file=sys.stderr)
return 1
token = (os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN") or "").strip()
if not token and args.secrets_toml.is_file():
with args.secrets_toml.open("rb") as f:
data = tomllib.load(f)
token = ((data.get("huggingface") or {}).get("token") or "").strip()
if not token:
print(
"deploy_hf_bootstrap_catalog: set HF_TOKEN / HUGGING_FACE_HUB_TOKEN or [huggingface].token in secrets.toml",
file=sys.stderr,
)
return 1
from huggingface_hub import HfApi
api = HfApi(token=token)
if not args.skip_hub_upload:
api.create_repo(args.repo_id, repo_type="dataset", exist_ok=True)
print(
f"deploy_hf_bootstrap_catalog: uploading {local}{args.repo_id}/{args.remote_filename} …",
file=sys.stderr,
)
api.upload_file(
path_or_fileobj=str(local),
path_in_repo=args.remote_filename,
repo_id=args.repo_id,
repo_type="dataset",
)
api.add_space_variable(
repo_id,
"KINK_HF_DATASET_FILENAME",
args.remote_filename,
description="Bootstrap SQLite filename in kink-catalog-slim (slim ~280MiB)",
token=token,
)
api.add_space_variable(
repo_id,
"KINK_HF_REQUIRE_FULL_CATALOG",
"1",
description="Require remote full catalog bootstrap instead of bundled demo seed",
token=token,
)
api.add_space_variable(
repo_id,
"KINK_HF_DATASET_REPO",
args.repo_id,
description="Hub dataset for catalog (matches Dockerfile default)",
token=token,
)
print("deploy_hf_bootstrap_catalog: Space variables updated.", file=sys.stderr)
print(
f" HF_TOKEN=*** HF_SPACE_REPO={repo_id} python scripts/publish_hf_space.py --verify",
file=sys.stderr,
)
return 0
if __name__ == "__main__":
raise SystemExit(main())