desirepath / start.sh
msradam's picture
Warm the model in the background so the app listens immediately
d83ee96 verified
Raw History Blame Contribute Delete
1.12 kB
#!/bin/sh
# Ollama in the background, the app in the foreground on the port Spaces expect.
set -e
ollama serve &
# Wait for the daemon rather than racing it. A Space that starts serving before
# the model is loadable answers its first queries with "Ollama unreachable".
i=0
until curl -sf http://127.0.0.1:11434/api/tags >/dev/null 2>&1; do
i=$((i + 1))
if [ "$i" -gt 120 ]; then
echo "ollama did not come up within 120s; serving the app anyway so the"
echo "coverage screen still works and the router can fall back to WebGPU."
break
fi
sleep 1
done
# Resident before the first visitor, so nobody pays the load. In the
# background, because loading 2.1 GB is slow on a CPU Space and the app has to
# be listening on $PORT before the platform's startup window closes. The
# coverage screen needs no model at all, so it should not wait for one.
(
curl -sf http://127.0.0.1:11434/api/generate \
-d '{"model":"granite4:micro","keep_alive":"-1"}' >/dev/null 2>&1 \
&& echo "model resident" \
|| echo "model warm-up failed; first query will pay the load"
) &
exec node /app/server.mjs