| #!/usr/bin/env bash |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| set -euo pipefail |
|
|
| if [[ $# -lt 1 ]]; then |
| echo "Usage: HF_TOKEN=hf_xxx $0 <hf-user>/<space-name>" |
| echo "Example: HF_TOKEN=hf_xxx $0 W1nd5pac/microclimate-x" |
| exit 2 |
| fi |
|
|
| REPO_ID="$1" |
| ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| cd "$ROOT" |
|
|
| |
| unset PYTHONPATH VIRTUAL_ENV PYTHONHOME |
|
|
| |
| if [[ ! -x ".venv/bin/hf" ]]; then |
| echo "βΆ Installing huggingface_hub CLI into .venv/ β¦" |
| .venv/bin/pip install -q -U "huggingface_hub[cli,hf_transfer]" |
| fi |
| HF=".venv/bin/hf" |
|
|
| |
| export HF_HUB_ENABLE_HF_TRANSFER=1 |
|
|
| |
| if [[ -z "${HF_TOKEN:-}" ]]; then |
| if ! $HF auth whoami >/dev/null 2>&1; then |
| echo "β HF_TOKEN env not set and not already logged in." |
| echo " Get a Write token at https://huggingface.co/settings/tokens and run:" |
| echo " HF_TOKEN=hf_xxx $0 $REPO_ID" |
| exit 1 |
| fi |
| fi |
|
|
| if [[ -n "${HF_TOKEN:-}" ]]; then |
| |
| echo "βΆ Authenticating as the token's owner β¦" |
| echo "$HF_TOKEN" | $HF auth login --token "$HF_TOKEN" --add-to-git-credential >/dev/null 2>&1 || true |
| fi |
|
|
| WHOAMI=$($HF auth whoami 2>/dev/null | head -1 || echo "?") |
| echo " Logged in as: $WHOAMI" |
|
|
| |
| echo "βΆ Ensuring Space $REPO_ID exists (Docker SDK) β¦" |
| CREATE_OUTPUT=$($HF repos create "$REPO_ID" --repo-type space --space-sdk docker 2>&1 || true) |
| if echo "$CREATE_OUTPUT" | grep -q "Successfully created"; then |
| echo " Created fresh Space." |
| elif echo "$CREATE_OUTPUT" | grep -qi "already created\|409"; then |
| echo " Space already exists β will push to it." |
| else |
| echo "$CREATE_OUTPUT" |
| echo "β Unexpected response from 'hf repos create'. Aborting." |
| exit 1 |
| fi |
|
|
| |
| MODEL="models/rf_model.pkl" |
| if [[ ! -f "$MODEL" ]]; then |
| echo "β οΈ $MODEL not found β the Space will fall back to a heuristic predictor." |
| read -r -p "Continue without the trained model? [y/N] " ans |
| [[ "$ans" =~ ^[Yy]$ ]] || exit 1 |
| fi |
|
|
| |
| echo "βΆ Uploading repo β spaces/$REPO_ID β¦" |
| echo " (217 MB rf_model.pkl uses HF's server-side Xet/LFS β no local LFS needed)" |
|
|
| DEPLOY_MSG="Deploy $(date -u +%Y-%m-%dT%H:%M:%SZ) β $(git rev-parse --short HEAD 2>/dev/null || echo local)" |
|
|
| |
| |
| $HF upload \ |
| "$REPO_ID" \ |
| . \ |
| . \ |
| --repo-type=space \ |
| --commit-message="$DEPLOY_MSG (code)" \ |
| --exclude "data/*" \ |
| --exclude "figures/*" \ |
| --exclude "tests/*" \ |
| --exclude ".venv/*" \ |
| --exclude ".local/*" \ |
| --exclude ".pytest_cache/*" \ |
| --exclude ".ruff_cache/*" \ |
| --exclude ".mypy_cache/*" \ |
| --exclude "**/__pycache__/*" \ |
| --exclude "*.sqlite3" \ |
| --exclude "*.sqlite3-*" \ |
| --exclude "*.pyc" \ |
| --exclude ".DS_Store" \ |
| --exclude ".git/*" \ |
| --exclude ".github/*" |
|
|
| |
| |
| |
| if [[ -f "$MODEL" ]]; then |
| echo "βΆ Uploading models/rf_model.pkl (217 MB) β bypassing .gitignore β¦" |
| $HF upload \ |
| "$REPO_ID" \ |
| "$MODEL" \ |
| "models/rf_model.pkl" \ |
| --repo-type=space \ |
| --commit-message="$DEPLOY_MSG (model)" |
| fi |
|
|
| echo |
| echo "β
Upload complete. Space is rebuilding now." |
| echo " Status: https://huggingface.co/spaces/$REPO_ID" |
| echo " Live URL: https://huggingface.co/spaces/$REPO_ID (β 3-5 min for first build)" |
| echo |
| echo "Tip: once the green Running badge shows, send the Live URL to your supervisor." |
|
|