Spaces:
Configuration error
Configuration error
File size: 3,637 Bytes
525124a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | #!/usr/bin/env python3
"""Create and optionally push this repo to a Hugging Face Docker Space."""
from __future__ import annotations
import argparse
import os
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
try:
import certifi
os.environ.setdefault("SSL_CERT_FILE", certifi.where())
os.environ.setdefault("REQUESTS_CA_BUNDLE", certifi.where())
except Exception:
pass
def run(command: list[str], *, check: bool = True) -> subprocess.CompletedProcess[str]:
return subprocess.run(
command,
cwd=ROOT,
check=check,
text=True,
capture_output=True,
)
def remote_exists(name: str) -> bool:
result = run(["git", "remote"], check=True)
return name in result.stdout.splitlines()
def configure_insecure_hf_http() -> None:
import httpx
from huggingface_hub.utils import set_client_factory
def factory() -> httpx.Client:
return httpx.Client(follow_redirects=True, timeout=None, verify=False)
set_client_factory(factory)
def main() -> int:
parser = argparse.ArgumentParser(description="Prepare/push a Hugging Face Docker Space.")
parser.add_argument("repo_id", help="Space repo id, for example: shon98/PyCatan-AI")
parser.add_argument("--push", action="store_true", help="Push HEAD to the Space main branch.")
parser.add_argument("--remote", default="hf", help="Git remote name to create/update.")
args = parser.parse_args()
try:
from huggingface_hub import HfApi
except ImportError:
print("huggingface_hub is not installed. Run: python -m pip install -U huggingface_hub")
return 1
print("[1/4] Checking committable files for API-key patterns...")
secret_check = run([sys.executable, "scripts/check_no_secrets.py"], check=False)
print(secret_check.stdout, end="")
if secret_check.returncode != 0:
print(secret_check.stderr, end="")
return secret_check.returncode
print("[2/4] Checking Hugging Face login...")
api = HfApi()
try:
whoami = api.whoami()
except Exception as exc:
if "CERTIFICATE_VERIFY_FAILED" not in str(exc):
print(f"Could not verify Hugging Face login: {exc}")
print("Run: hf auth login")
return 1
print("SSL verification failed with the local certificate store; retrying with verification disabled for this deploy process.")
configure_insecure_hf_http()
api = HfApi()
try:
whoami = api.whoami()
except Exception as retry_exc:
print(f"Could not verify Hugging Face login: {retry_exc}")
print("Run: hf auth login")
return 1
print(f"Logged in as: {whoami.get('name') or whoami.get('fullname') or 'unknown'}")
print("[3/4] Creating/updating Docker Space repo...")
api.create_repo(
repo_id=args.repo_id,
repo_type="space",
space_sdk="docker",
exist_ok=True,
)
remote_url = f"https://huggingface.co/spaces/{args.repo_id}"
if remote_exists(args.remote):
run(["git", "remote", "set-url", args.remote, remote_url])
else:
run(["git", "remote", "add", args.remote, remote_url])
print(f"Remote '{args.remote}' -> {remote_url}")
print("[4/4] Ready.")
if args.push:
print("Pushing HEAD to Space main...")
push = subprocess.run(["git", "push", args.remote, "HEAD:main"], cwd=ROOT)
return push.returncode
print(f"To deploy, run: git push {args.remote} HEAD:main")
return 0
if __name__ == "__main__":
sys.exit(main())
|