#!/usr/bin/env bash # Publish this directory to a Hugging Face static Space. # # Prerequisite: the Space must already exist. Create it once at # https://huggingface.co/new-space -> SDK: "Static" # Then run, from anywhere: # tools/deploy_hf.sh / # # Auth: uses $HF_TOKEN if set, otherwise prompts. Needs a token with write # access (https://huggingface.co/settings/tokens). set -euo pipefail SPACE="${1:-}" if [[ -z "$SPACE" || "$SPACE" != */* ]]; then echo "usage: $0 / e.g. $0 yifanzhang/darwinx" >&2 exit 2 fi USER="${SPACE%%/*}" SRC="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$SRC" [[ -f index.html && -f README.md ]] || { echo "run this from the project page directory" >&2; exit 1; } if [[ -z "${HF_TOKEN:-}" ]]; then read -rsp "Hugging Face write token (hf_...): " HF_TOKEN; echo fi [[ -n "$HF_TOKEN" ]] || { echo "no token given" >&2; exit 1; } WORK="$(mktemp -d)" trap 'rm -rf "$WORK"' EXIT echo "==> cloning https://huggingface.co/spaces/$SPACE" git -c credential.helper= \ clone --quiet "https://$USER:$HF_TOKEN@huggingface.co/spaces/$SPACE" "$WORK/space" 2>&1 \ | sed 's/'"$HF_TOKEN"'/***/g' \ || { echo "clone failed - does the Space exist, and is the token writable?" >&2; exit 1; } echo "==> copying the page" # Ship only what the page needs; drop local scratch files. rsync -a --delete \ --exclude '.git' --exclude '.DS_Store' --exclude '_*' --exclude '*.log' \ ./ "$WORK/space/" cd "$WORK/space" git add -A if git diff --cached --quiet; then echo "==> nothing changed; Space is already up to date" exit 0 fi echo "==> files being published:" git diff --cached --name-status | sed 's/^/ /' git -c user.name="$USER" -c user.email="$USER@users.noreply.huggingface.co" \ commit --quiet -m "DarwinX project page" git push --quiet origin HEAD:main 2>&1 | sed 's/'"$HF_TOKEN"'/***/g' echo echo "==> live at https://huggingface.co/spaces/$SPACE" echo " (a static Space builds in a few seconds; hard-refresh if you see the old copy)"