Spaces:
Runtime error
Runtime error
File size: 2,314 Bytes
9d79680 6624945 9d79680 6624945 9d79680 6624945 9d79680 6624945 9d79680 6624945 9d79680 6624945 9d79680 6624945 9d79680 6624945 9d79680 6624945 9d79680 6624945 | 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #!/usr/bin/env bash
set -euo pipefail
echo "===== Application Startup at $(date) ====="
# start.sh - robust startup flow: start Ollama, wait for readiness, create model (if provided), then run gunicorn
APP_DIR=/app
MODEL_NAME=aj-mini
cd "$APP_DIR"
# Check if ollama is installed
if ! command -v ollama >/dev/null 2>&1; then
echo "Error: ollama binary not found in container. Make sure Ollama was installed."
exit 1
fi
# Start ollama serve in background
echo "Starting ollama serve..."
ollama serve > /tmp/ollama.log 2>&1 &
OLLAMA_PID=$!
echo "Ollama PID: $OLLAMA_PID"
# Tail Ollama logs in background for visibility
tail -n +1 -f /tmp/ollama.log &
# Wait for ollama to be ready (with retries)
echo "Waiting for Ollama to be ready (http://localhost:11434)..."
for i in {1..60}; do
if curl -s http://127.0.0.1:11434/api/tags > /dev/null 2>&1; then
echo "Ollama is ready (after $i attempts)!"
break
fi
echo "Waiting for Ollama to start... ($i/60)"
sleep 2
if [ $i -eq 60 ]; then
echo "Warning: Ollama did not become ready after retries. Check /tmp/ollama.log for details but continuing startup." >&2
fi
done
# Function to create model with retries
create_model() {
local tries=0
local max=5
until [ $tries -ge $max ]; do
if ollama list | grep -q "^$MODEL_NAME\b"; then
echo "Model $MODEL_NAME already exists."
return 0
fi
echo "Attempting to create model $MODEL_NAME (try $((tries+1))/$max)..."
if ollama create "$MODEL_NAME" -f Modelfile-aj-mini-v2 >> /tmp/ollama.log 2>&1; then
echo "Model $MODEL_NAME created successfully."
return 0
fi
tries=$((tries+1))
sleep 3
done
echo "Failed to create model $MODEL_NAME after $max attempts. Continuing; API may return errors until model is available." >&2
return 1
}
# Create model if Modelfile exists
if [ -f "$APP_DIR/Modelfile-aj-mini-v2" ]; then
echo "Found Modelfile at $APP_DIR/Modelfile-aj-mini-v2"
create_model || true
else
echo "Modelfile not found at $APP_DIR/Modelfile-aj-mini-v2 - skipping model creation. If you expect a local model, add the Modelfile and push again." >&2
fi
# Start Gunicorn for Flask app
echo "Starting gunicorn for API..."
echo "===== API Server Ready ====="
exec gunicorn api_server:app --bind 0.0.0.0:5000 --workers 2 --timeout 120
|