provinans / scripts /space_startup.sh
reversely's picture
fix(infra): retry the dataset download on a transient stall (#255)
97cb908 verified
Raw
History Blame Contribute Delete
1.7 kB
#!/usr/bin/env bash
# HF Space container entrypoint (issue #26): pull the public serving assets,
# put the OCR CSV where the app expects it, then serve the API + built SPA on
# one port. The dataset is public, so no HF token is needed.
set -euo pipefail
DATASET="${ENDOPATH_DATASET:-reversely/tcga-ucec-embedding}"
DEST="${ENDOPATH_EMBEDDINGS_DIR:-/app/data/images}"
echo "downloading dataset ${DATASET} -> ${DEST} ..."
# The Space has no persistent storage, so every boot re-fetches ~2470 files. A
# single transient hub stall used to abort the boot under `set -e` and leave the
# Space in RUNTIME_ERROR with no auto-retry (#255). snapshot_download resumes a
# partial download, so retrying only fetches what is missing: ride out a transient
# stall across a few attempts, and still fail loud if the download is truly broken.
for attempt in 1 2 3 4 5; do
if python - "$DATASET" "$DEST" <<'PY'
import sys
from huggingface_hub import snapshot_download
snapshot_download(repo_id=sys.argv[1], repo_type="dataset", local_dir=sys.argv[2])
PY
then
echo "dataset download complete on attempt ${attempt}"
break
fi
if [ "${attempt}" -eq 5 ]; then
echo "dataset download failed after ${attempt} attempts, aborting" >&2
exit 1
fi
echo "dataset download attempt ${attempt} failed; retrying in $((attempt * 10))s ..." >&2
sleep "$((attempt * 10))"
done
# The report OCR text ships under ocr/ in the dataset; report_text_for_case
# reads it from data/raw/TCGA_Reports.csv.
mkdir -p /app/data/raw
if [ -f "${DEST}/ocr/TCGA_Reports.csv" ]; then
cp -f "${DEST}/ocr/TCGA_Reports.csv" /app/data/raw/TCGA_Reports.csv
fi
exec uvicorn endopath.api:app --host 0.0.0.0 --port "${PORT:-7860}"