SchemeImpactNet / hf_start.sh
sammeeer's picture
Inital schemeimpactnet deployment
f87e795
#!/bin/bash
# hf_start.sh β€” SchemeImpactNet HuggingFace Spaces entrypoint
# Runs pipeline (if needed), starts FastAPI on 8000, Streamlit on 7860
set -euo pipefail
echo "============================================================"
echo " SchemeImpactNet β€” HuggingFace Spaces Startup"
echo "============================================================"
cd /app
# ── Step 1: Generate / verify processed data ─────────────────────────────────
echo ""
echo "β†’ Checking processed data..."
NEEDS_PIPELINE=false
for f in data/processed/mnrega_cleaned.csv \
data/processed/mnrega_predictions.csv \
data/processed/optimized_budget_allocation.csv; do
if [[ ! -f "$f" ]]; then
echo " Missing: $f"
NEEDS_PIPELINE=true
fi
done
if [[ "$NEEDS_PIPELINE" == true ]]; then
echo "β†’ Running data pipeline (Stage 3)..."
python main.py --stage 3
echo "βœ“ Pipeline complete"
else
echo "βœ“ Processed data found β€” skipping pipeline"
fi
# ── Step 2: Start FastAPI backend on port 8000 (background) ──────────────────
echo ""
echo "β†’ Starting FastAPI backend on port 8000..."
python -m uvicorn backend.main:app \
--host 0.0.0.0 \
--port 8000 \
--log-level warning &
BACKEND_PID=$!
# Wait for backend health
MAX_WAIT=20
WAITED=0
until curl -sf "http://localhost:8000/health" >/dev/null 2>&1; do
sleep 1
WAITED=$((WAITED + 1))
if [[ $WAITED -ge $MAX_WAIT ]]; then
echo " ⚠ Backend health timeout β€” continuing"
break
fi
done
echo "βœ“ Backend live"
# ── Step 3: Start Streamlit on HF port 7860 (foreground) ────────────────────
echo ""
echo "β†’ Starting Streamlit frontend on port 7860..."
echo "βœ“ Dashboard: https://huggingface.co/spaces/{YOUR_SPACE}"
echo ""
exec python -m streamlit run frontend/app.py \
--server.port 7860 \
--server.address 0.0.0.0 \
--server.headless true \
--server.enableCORS false \
--server.enableXsrfProtection false \
--browser.gatherUsageStats false