Spaces:
Sleeping
Sleeping
File size: 3,187 Bytes
f0f84fb | 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 60 | #!/bin/bash
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# start.sh β PsyPredict HF Spaces Startup Orchestrator
#
# Execution order:
# 1. Start Ollama server daemon in the background
# 2. Wait until Ollama API is healthy (up to 60 seconds)
# 3. Pull the Phi-3.5 quantized model (skips if already cached in this run)
# 4. Launch FastAPI / Uvicorn on port 7860
#
# Environment variables (set in Dockerfile or HF Space secrets):
# OLLAMA_MODEL β model tag to pull (default: phi3.5:3.8b-mini-instruct-q4_0)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
set -e # Exit immediately on any error
echo "βββββββββββββββββββββββββββββββββββββββββββββββ"
echo "π PsyPredict β Hugging Face Spaces Startup"
echo "βββββββββββββββββββββββββββββββββββββββββββββββ"
# ββ Step 1: Start Ollama server in the background βββββββββββββββββββββββββββββ
echo "βΆ Starting Ollama server..."
ollama serve &
OLLAMA_PID=$!
# ββ Step 2: Wait for Ollama to become healthy (max 60 seconds) ββββββββββββββββ
echo "β³ Waiting for Ollama to be ready..."
RETRIES=30
for i in $(seq 1 $RETRIES); do
if curl -sf http://localhost:11434/api/tags > /dev/null 2>&1; then
echo "β
Ollama is ready (attempt $i/$RETRIES)."
break
fi
if [ "$i" -eq "$RETRIES" ]; then
echo "β Ollama failed to start within 60 seconds. Exiting."
exit 1
fi
sleep 2
done
# ββ Step 3: Pull the Phi-3.5 model ββββββββββββββββββββββββββββββββββββββββββββ
# 'ollama pull' is idempotent β safe to call even if the model is cached.
# On HF Spaces, the first pull will download ~2.4 GB; subsequent restarts
# are faster because the container's /root/.ollama layer is reused.
MODEL="${OLLAMA_MODEL:-phi3.5:3.8b-mini-instruct-q4_0}"
echo "βΆ Pulling model: $MODEL"
echo " (First run downloads ~2.4 GB β may take several minutes on CPU)"
ollama pull "$MODEL"
echo "β
Model ready: $MODEL"
# ββ Step 4: Launch FastAPI on port 7860 βββββββββββββββββββββββββββββββββββββββ
echo "βΆ Starting FastAPI (Uvicorn) on port 7860..."
echo " API docs β http://localhost:7860/docs"
echo "βββββββββββββββββββββββββββββββββββββββββββββββ"
exec uvicorn app.main:app \
--host 0.0.0.0 \
--port 7860 \
--workers 1 \
--log-level info
|