detection-segmentation-studio / deploy_hf_space.py
Alicesong's picture
Deploy Detection + Segmentation Studio
1efe34a verified
Raw
History Blame Contribute Delete
1.93 kB
"""Deploy this project to a Hugging Face Space with one command."""
from __future__ import annotations
import argparse
import os
from pathlib import Path
from huggingface_hub import HfApi
ROOT = Path(__file__).resolve().parent
IGNORE_PATTERNS = [
"__pycache__/**",
"*.pyc",
".gradio/**",
"assets/models/**",
"assets/cache/**",
"assets/cloudflared.exe",
"*.log",
]
def resolve_token(explicit_token: str | None) -> str:
token = explicit_token or os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
if not token:
raise SystemExit(
"Missing Hugging Face token. Pass --token or set HF_TOKEN / HUGGINGFACEHUB_API_TOKEN."
)
return token
def main() -> None:
parser = argparse.ArgumentParser(description="Create or update a Hugging Face Space.")
parser.add_argument(
"--repo-id",
required=True,
help="Space repo id, e.g. username/detection-segmentation-studio",
)
parser.add_argument("--token", help="Hugging Face write token. Optional if HF_TOKEN is set.")
parser.add_argument("--private", action="store_true", help="Create the Space as private.")
parser.add_argument(
"--commit-message",
default="Deploy Detection + Segmentation Studio",
help="Commit message for the uploaded Space files.",
)
args = parser.parse_args()
token = resolve_token(args.token)
api = HfApi(token=token)
api.create_repo(
repo_id=args.repo_id,
repo_type="space",
space_sdk="gradio",
private=args.private,
exist_ok=True,
)
api.upload_folder(
repo_id=args.repo_id,
repo_type="space",
folder_path=ROOT,
commit_message=args.commit_message,
ignore_patterns=IGNORE_PATTERNS,
)
print(f"Deployed: https://huggingface.co/spaces/{args.repo_id}")
if __name__ == "__main__":
main()