#!/bin/sh set -e echo "============================================" echo " Certificate Verification AI Service" echo "============================================" echo "Starting at $(date)" echo "Port: ${PORT:-8080}" echo "Workers: ${WORKERS:-1}" echo "Log level: ${LOG_LEVEL:-info}" echo "Skip models: ${SKIP_MODEL_LOADING:-false}" echo "============================================" # Validate required environment variables missing_vars="" if [ -z "$SUPABASE_URL" ]; then missing_vars="$missing_vars SUPABASE_URL" fi if [ -z "$SUPABASE_KEY" ]; then missing_vars="$missing_vars SUPABASE_KEY" fi if [ -z "$OCRSPACE_API_KEY" ]; then missing_vars="$missing_vars OCRSPACE_API_KEY" fi if [ -n "$missing_vars" ]; then echo "WARNING: Missing environment variables:$missing_vars" echo "The service may not function correctly without these." echo "See .env.example for required configuration." fi # Pre-download models if not present and not skipped if [ "${SKIP_MODEL_LOADING}" != "true" ]; then echo "Checking AI model files..." # Check YOLO model if [ ! -f "models/best.pt" ] && [ ! -f "yolo_seal_model/best.pt" ]; then echo "YOLO model not found locally. It will be downloaded on first request." else echo "YOLO model: OK" fi # Check ViT model if [ ! -f "vit_seal_checker.pth" ]; then echo "ViT model not found locally. It will be downloaded on first request." else echo "ViT model: OK" fi else echo "Model loading SKIPPED (SKIP_MODEL_LOADING=true)" echo "Running in lightweight mode (OCR + database verification only)" fi echo "============================================" echo "Launching Gunicorn with Uvicorn workers..." echo "============================================" # Start Gunicorn with Uvicorn worker class exec gunicorn api:app \ --bind "0.0.0.0:${PORT:-8080}" \ --worker-class uvicorn.workers.UvicornWorker \ --workers "${WORKERS:-1}" \ --timeout "${GUNICORN_TIMEOUT:-300}" \ --keep-alive "${GUNICORN_KEEP_ALIVE:-5}" \ --graceful-timeout "${GUNICORN_GRACEFUL_TIMEOUT:-120}" \ --log-level "${LOG_LEVEL:-info}" \ --access-logfile - \ --error-logfile - \ --config gunicorn.conf.py