ReviewSearch / start.sh
yjoonjang's picture
deploy: Docker Space (qdrant + FastAPI + React SPA)
c47538b verified
Raw History Blame Contribute Delete
1.92 kB
#!/usr/bin/env bash
# Boot: fetch the Qdrant snapshot from the HF dataset, start Qdrant, recover the collection,
# then serve the API + SPA on 7860. Free HF Spaces have no persistent disk, so this runs on every
# cold start (~a few minutes to download the ~3.7GB snapshot + load models).
set -euo pipefail
SNAP_DIR="${QDRANT__STORAGE__SNAPSHOTS_PATH:-/home/user/app/qdrant_data/snapshots}"
STORE_DIR="${QDRANT__STORAGE__STORAGE_PATH:-/home/user/app/qdrant_data/storage}"
mkdir -p "$SNAP_DIR" "$STORE_DIR"
status_of() { # -> collection status string ("" if absent)
curl -s localhost:6333/collections/reviewsearch 2>/dev/null \
| python -c "import sys,json;print(json.load(sys.stdin).get('result',{}).get('status',''))" 2>/dev/null || echo ""
}
echo "[start] fetching Qdrant snapshot from HF dataset..."
python - <<PY
import os, shutil
from huggingface_hub import hf_hub_download
dst = os.path.join("$SNAP_DIR", "reviewsearch.snapshot")
if not os.path.exists(dst):
p = hf_hub_download("yjoonjang/reviewsearch-index", "reviewsearch.snapshot", repo_type="dataset")
shutil.copy(p, dst)
print("[start] snapshot ready:", dst, os.path.getsize(dst), "bytes", flush=True)
PY
echo "[start] launching Qdrant..."
./qdrant > /home/user/app/qdrant.log 2>&1 &
for _ in $(seq 1 60); do curl -sf localhost:6333/healthz >/dev/null 2>&1 && break; sleep 1; done
echo "[start] Qdrant healthy"
if [ "$(status_of)" != "green" ]; then
echo "[start] recovering collection from snapshot..."
curl -s -X PUT localhost:6333/collections/reviewsearch/snapshots/recover \
-H 'content-type: application/json' \
-d "{\"location\":\"file://$SNAP_DIR/reviewsearch.snapshot\"}"
echo
fi
for _ in $(seq 1 300); do [ "$(status_of)" = "green" ] && break; sleep 1; done
echo "[start] collection status: $(status_of)"
echo "[start] launching uvicorn on :7860"
exec uvicorn reviewsearch.serve.app:app --host 0.0.0.0 --port 7860