#!/usr/bin/env python3 """ Deploy SPHEREx Data Explorer to Hugging Face Spaces (or update Dataset) without git clone or external git CLI dependencies. Uses the huggingface_hub Python library and your existing authenticated session. """ import argparse import os import sys from huggingface_hub import HfApi, get_token IGNORE_PATTERNS = [ ".git/*", ".git", ".gitignore", ".dockerignore", ".pytest_cache/*", ".pytest_cache", "__pycache__/*", "__pycache__", "*.pyc", "*.fits", "*.fits.gz", "*.tar", "*.zip", "spherex_data/*", "spherex_data", "hips_cache/*", "hips_cache", "cache_cutouts/*", "cache_cutouts", "cache_remote/*", "cache_remote", "cache_previews/*", "cache_previews", "catalog_output/*", "catalog_output", ".agents/*", ".agents", "huggingface-space/*", "huggingface-space", "*.bak", "index_prev_landing.html", "downloaded_index_1.html", ] def main(): parser = argparse.ArgumentParser(description="Deploy SPHEREx Explorer to Hugging Face") parser.add_argument("--space-name", default="spherex-projects", help="Space repo name (e.g. spherex-projects or spherex-explorer)") parser.add_argument("--target", choices=["space", "dataset", "both"], default="space", help="Target repository to push to") parser.add_argument("--private", action="store_true", help="Create space as private (default is public)") args = parser.parse_args() token = get_token() if not token: print("[Error] No Hugging Face token found. Run 'huggingface-cli login' first.", file=sys.stderr) sys.exit(1) api = HfApi(token=token) user_info = api.whoami() username = user_info.get("name") print(f"[Authenticated] Logged in as: {username}") base_dir = os.path.dirname(os.path.abspath(__file__)) if args.target in ("space", "both"): space_id = f"{username}/{args.space_name}" print(f"\n[Step 1] Ensuring Space exists: {space_id} (Static SDK)...") api.create_repo( repo_id=space_id, repo_type="space", space_sdk="static", private=args.private, exist_ok=True, ) print(f"[Step 2] Uploading application files to Space: {space_id}...") api.upload_folder( folder_path=base_dir, repo_id=space_id, repo_type="space", ignore_patterns=IGNORE_PATTERNS, commit_message="Deploy SPHEREx Data Explorer (Static)", ) print(f"\n[Success] Space deployed! View build and app at:") print(f" https://huggingface.co/spaces/{space_id}") if args.target in ("dataset", "both"): dataset_id = f"{username}/spherex-projects" print(f"\n[Step 3] Uploading current application files to Dataset: {dataset_id}...") api.upload_folder( folder_path=base_dir, repo_id=dataset_id, repo_type="dataset", ignore_patterns=IGNORE_PATTERNS, commit_message="Update SPHEREx application source files", ) print(f"\n[Success] Dataset files updated at:") print(f" https://huggingface.co/datasets/{dataset_id}") if __name__ == "__main__": main()