#!/usr/bin/env python3 """Apply organization repository secrets and variables to an existing Space.""" from __future__ import annotations import os import re from pathlib import Path def configure(api, env): names = ("HF_TOKEN", "TSFM_API_KEY", "TABPFN_TOKEN") missing = [name for name in names if not env.get(name)] if missing: raise ValueError("Missing repository secrets: " + ", ".join(missing)) repo = env.get("HF_SPACE_REPO") or "ThinkcatLab/LiveHouse-TS" results = env.get("HF_RESULTS_REPO") or repo + "-results" state = env.get("HF_STATE_REPO") or repo + "-state" enabled = env.get("TSFM_EVALUATOR_ENABLED", "0") if enabled not in {"0", "1"}: raise ValueError("TSFM_EVALUATOR_ENABLED must be 0 or 1") acceptance = env.get("LIVEHOUSE_ACCEPTANCE_ID", "") if acceptance and not re.fullmatch(r"[a-z0-9][a-z0-9-]{1,60}", acceptance): raise ValueError("Invalid cloud acceptance ID") # Validate all destinations before the first secret/variable write. api.space_info(repo) if not api.dataset_info(state).private: raise ValueError("Operator state Dataset must be private") if api.dataset_info(results).private: raise ValueError("Leaderboard results Dataset must be public") if "state/manifest.json" not in api.list_repo_files(state, repo_type="dataset"): raise ValueError("Initialize and verify durable state before configuring the worker") for name in names: api.add_space_secret(repo, name, env[name]) variables = {"HF_SPACE_REPO":repo, "HF_RESULTS_REPO":results, "HF_STATE_REPO":state, "TSFM_EVALUATOR_ENABLED":enabled, "LIVEHOUSE_ACCEPTANCE_ID":acceptance} for name, value in variables.items(): api.add_space_variable(repo, name, value) return {"space":repo, "secret_names":list(names), "variables":variables} def main(): import json from dotenv import load_dotenv from huggingface_hub import HfApi load_dotenv(Path(__file__).resolve().parents[1] / ".env") if not os.getenv("HF_TOKEN"): raise SystemExit("HF_TOKEN is required") print(json.dumps(configure(HfApi(token=os.environ["HF_TOKEN"]), os.environ), indent=2)) if __name__ == "__main__": main()