| # Limit PyTorch background threads so Gunicorn isn't starved | |
| export OMP_NUM_THREADS=1 | |
| export MKL_NUM_THREADS=1 | |
| # Start Celery worker in background | |
| # We use concurrency=2 to avoid memory overload on the 16GB free tier | |
| celery -A tasks worker --loglevel=info --concurrency=2 -O fair -B & | |
| CELERY_PID=$! | |
| # Trap SIGTERM and SIGINT for graceful shutdown | |
| trap "kill $CELERY_PID; exit 0" SIGTERM SIGINT | |
| # Run database migrations/initialization | |
| flask --app app_new init-db | |
| # Start Gunicorn in foreground | |
| # Hugging Face Spaces expects the app to listen on port 7860 | |
| gunicorn -w 4 -b 0.0.0.0:${PORT:-7860} --timeout 300 app_new:app | |