File size: 2,031 Bytes
7a3b3da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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 <user>/<space>
#
# 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 <user>/<space>     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)"