apriori-agent / deploy_space.py
Tasosh's picture
Deploy aPriori agent
086be34 verified
Raw History Blame Contribute Delete
3.75 kB
"""Publish this folder as a Hugging Face Gradio Space.
pip install huggingface_hub
huggingface-cli login # or pass --token / set HF_TOKEN
python deploy_space.py --repo-id your-name/apriori-agent
python deploy_space.py --repo-id your-name/apriori-agent --private \
--hf-token hf_... # stored as a Space secret, never in git
Re-running uploads changed files to the same Space. Set HF_TOKEN locally to the
write token used for deployment; the app itself uses the HF_TOKEN Space secret.
"""
from __future__ import annotations
import argparse
import os
import sys
from pathlib import Path
from dotenv import load_dotenv
ROOT = Path(__file__).parent
load_dotenv(ROOT / ".env")
IGNORE = [
"__pycache__/*",
"*.pyc",
".git/*",
".venv/*",
".env",
"*.png",
"datasets/*",
"flagged/*",
]
def main() -> int:
ap = argparse.ArgumentParser(description="Deploy the aPriori agent to a HF Space")
ap.add_argument("--repo-id", required=True, help="e.g. your-name/apriori-agent")
ap.add_argument(
"--token",
default=os.environ.get("HF_TOKEN") or os.environ.get("HF_ACCESS_TOKEN"),
help="HF write token",
)
ap.add_argument("--private", action="store_true", help="create the Space as private")
ap.add_argument(
"--hardware",
default=None,
help="Space hardware, e.g. cpu-basic (free), cpu-upgrade. Omit to keep the default.",
)
ap.add_argument(
"--hf-token",
default=os.environ.get("HF_TOKEN") or os.environ.get("HF_ACCESS_TOKEN"),
help="store this inference token as the HF_TOKEN Space secret so visitors need no key",
)
ap.add_argument("--message", default="Deploy aPriori agent", help="commit message")
args = ap.parse_args()
try:
from huggingface_hub import HfApi
except ImportError:
print("pip install huggingface_hub", file=sys.stderr)
return 1
if not args.token:
print(
"No token. Run `huggingface-cli login`, set HF_TOKEN, or pass --token.",
file=sys.stderr,
)
return 1
for required in ("app.py", "requirements.txt", "README.md"):
if not (ROOT / required).exists():
print(f"Missing {required} — run this from the project folder.", file=sys.stderr)
return 1
api = HfApi(token=args.token)
print(f"Creating or reusing Space {args.repo_id} …")
url = api.create_repo(
repo_id=args.repo_id,
repo_type="space",
space_sdk="gradio",
space_hardware=args.hardware,
private=args.private,
exist_ok=True,
)
print(f" {url}")
if args.hf_token:
api.add_space_secret(
repo_id=args.repo_id,
key="HF_TOKEN",
value=args.hf_token,
description="Token used for Hugging Face Inference API calls",
)
print(" stored HF_TOKEN as a Space secret")
print("Uploading files …")
api.upload_folder(
repo_id=args.repo_id,
repo_type="space",
folder_path=str(ROOT),
ignore_patterns=IGNORE,
commit_message=args.message,
)
name = args.repo_id.split("/")[-1]
owner = args.repo_id.split("/")[0]
print(
f"\nDone. The Space builds in a few minutes at\n"
f" https://huggingface.co/spaces/{args.repo_id}\n"
f" https://{owner}-{name}.hf.space\n\n"
"Cantera and aPrioriDNS make the first build slow — watch the build log if it "
"seems stuck. Without a Space secret, visitors use the Guided run tab or bring "
"their own key."
)
return 0
if __name__ == "__main__":
raise SystemExit(main())