Spaces:
Sleeping
Sleeping
Download scripts/deploy_hf_space.py from Neetu143/meta_hackathon: direct link, hf CLI and curl.
- Browser
- Download file 4.14 kB
-
https://huggingface.co/spaces/Neetu143/meta_hackathon/resolve/main/scripts/deploy_hf_space.py
- Command line
-
hf download hf://spaces/Neetu143/meta_hackathon/scripts/deploy_hf_space.py
-
curl -L -o deploy_hf_space.py https://huggingface.co/spaces/Neetu143/meta_hackathon/resolve/main/scripts/deploy_hf_space.py
4.14 kB
| from __future__ import annotations | |
| import argparse | |
| import os | |
| from pathlib import Path | |
| from dotenv import dotenv_values | |
| from huggingface_hub import HfApi | |
| IGNORE_PATTERNS = [ | |
| ".git/*", | |
| ".venv/*", | |
| ".uv-cache/*", | |
| ".docker-config/*", | |
| ".pytest_cache/*", | |
| "__pycache__/*", | |
| ".codex", | |
| ".env", | |
| "*.log", | |
| "*.docx", | |
| "*.pdf", | |
| ] | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser( | |
| description="Create or update a Hugging Face Docker Space from this folder." | |
| ) | |
| parser.add_argument( | |
| "--repo-id", | |
| required=True, | |
| help="Target Space repo id in the form <user-or-org>/<space-name>.", | |
| ) | |
| parser.add_argument( | |
| "--private", | |
| action="store_true", | |
| help="Create the Space as private if it does not already exist.", | |
| ) | |
| parser.add_argument( | |
| "--skip-secrets", | |
| action="store_true", | |
| help="Do not sync API_BASE_URL / MODEL_NAME / HF_TOKEN into Space settings.", | |
| ) | |
| parser.add_argument( | |
| "--skip-upload", | |
| action="store_true", | |
| help="Create the Space and sync config, but do not upload the local folder.", | |
| ) | |
| parser.add_argument( | |
| "--skip-restart", | |
| action="store_true", | |
| help="Do not request a Space restart after upload/config sync.", | |
| ) | |
| return parser.parse_args() | |
| def require_env(name: str) -> str: | |
| value = os.getenv(name) | |
| if not value: | |
| raise SystemExit(f"Missing required environment variable: {name}") | |
| return value | |
| def load_runtime_config() -> dict[str, str]: | |
| env_path = Path(".env") | |
| if not env_path.exists(): | |
| raise SystemExit("Missing .env file. This script reads runtime values from .env.") | |
| values = dotenv_values(env_path) | |
| runtime = {} | |
| for key in ("API_BASE_URL", "MODEL_NAME"): | |
| value = values.get(key) | |
| if not value: | |
| raise SystemExit(f"Missing required key in .env: {key}") | |
| runtime[key] = value | |
| api_key = values.get("HF_TOKEN") or values.get("OPENAI_API_KEY") | |
| if not api_key: | |
| raise SystemExit("Missing required key in .env: HF_TOKEN or OPENAI_API_KEY") | |
| runtime["HF_TOKEN"] = str(api_key) | |
| return runtime | |
| def main() -> None: | |
| args = parse_args() | |
| hub_token = require_env("HF_HUB_TOKEN") | |
| runtime = load_runtime_config() | |
| api = HfApi(token=hub_token) | |
| repo_url = api.create_repo( | |
| repo_id=args.repo_id, | |
| repo_type="space", | |
| space_sdk="docker", | |
| private=args.private, | |
| exist_ok=True, | |
| ) | |
| print(f"Space ready: {repo_url}") | |
| if not args.skip_secrets: | |
| api.add_space_variable( | |
| args.repo_id, | |
| "API_BASE_URL", | |
| runtime["API_BASE_URL"], | |
| description="OpenAI-compatible provider base URL", | |
| token=hub_token, | |
| ) | |
| api.add_space_variable( | |
| args.repo_id, | |
| "MODEL_NAME", | |
| runtime["MODEL_NAME"], | |
| description="Inference model name", | |
| token=hub_token, | |
| ) | |
| api.add_space_secret( | |
| args.repo_id, | |
| "HF_TOKEN", | |
| runtime["HF_TOKEN"], | |
| description="Runtime provider API key used by inference.py", | |
| token=hub_token, | |
| ) | |
| print("Space variables/secrets synced from .env") | |
| if not args.skip_upload: | |
| commit = api.upload_folder( | |
| repo_id=args.repo_id, | |
| repo_type="space", | |
| folder_path=".", | |
| ignore_patterns=IGNORE_PATTERNS, | |
| commit_message="Upload support triage environment", | |
| token=hub_token, | |
| ) | |
| print(f"Upload commit: {commit.commit_url}") | |
| if not args.skip_restart: | |
| runtime_info = api.restart_space(args.repo_id, token=hub_token) | |
| print(f"Space restart requested. Stage: {runtime_info.stage}") | |
| else: | |
| runtime_info = api.get_space_runtime(args.repo_id, token=hub_token) | |
| print(f"Current Space stage: {runtime_info.stage}") | |
| print(f"Space URL: https://huggingface.co/spaces/{args.repo_id}") | |
| if __name__ == "__main__": | |
| main() | |