from __future__ import annotations import os from typing import Iterable from .assets import ensure_assets_from_hub, local_asset_status def _is_true(value: str | None, default: bool = True) -> bool: if value is None: return default return value.strip().lower() not in {"0", "false", "no", "off"} def _missing_status_keys() -> Iterable[str]: status = local_asset_status() return [key for key, ok in status.items() if not ok] def bootstrap_assets_on_start() -> None: repo_id = os.getenv("HF_ASSETS_REPO_ID", "SalmaHassan/HyperClinical-assets").strip() revision = (os.getenv("HF_ASSETS_REVISION", "main") or "main").strip() or "main" required = _is_true(os.getenv("HF_ASSETS_REQUIRED_ON_START"), default=True) force_download = _is_true(os.getenv("HF_ASSETS_FORCE_DOWNLOAD_ON_START"), default=False) if not repo_id: msg = "HF_ASSETS_REPO_ID is empty; cannot bootstrap server assets." if required: raise RuntimeError(msg) print(f"[bootstrap-assets] WARNING: {msg}") return print( f"[bootstrap-assets] Syncing assets from repo='{repo_id}' revision='{revision}' " f"(required={required}, force_download={force_download})" ) try: ensure_assets_from_hub( repo_id=repo_id, revision=revision, force_download=force_download, ) except Exception as exc: if required: raise RuntimeError(f"Asset bootstrap failed: {exc}") from exc print(f"[bootstrap-assets] WARNING: asset bootstrap failed: {exc}") return missing = list(_missing_status_keys()) if missing: msg = f"Server asset bootstrap incomplete; missing: {', '.join(missing)}" if required: raise RuntimeError(msg) print(f"[bootstrap-assets] WARNING: {msg}") return print("[bootstrap-assets] Server assets ready.") def main() -> None: bootstrap_assets_on_start() if __name__ == "__main__": main()