FROM python:3.12-slim # Non-root user (HF Spaces convention; writable HOME for the HF cache the model # is pulled into at boot). RUN useradd -m -u 1000 user USER user ENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH \ HF_HOME=/home/user/.cache/huggingface \ PYTHONUNBUFFERED=1 WORKDIR /home/user/app # CPU-only torch (default PyPI wheel is CUDA and huge). Only needed for the real # model; the mock path works on flask alone. RUN pip install --no-cache-dir --user torch --index-url https://download.pytorch.org/whl/cpu RUN pip install --no-cache-dir --user flask gunicorn COPY --chown=user . . EXPOSE 7860 # One worker (single shared model), threads for static/infer connections. # No --preload: avoids the torch/OpenMP fork-after-threads deadlock. CMD ["gunicorn", "-w", "1", "--threads", "8", "-k", "gthread", "-b", "0.0.0.0:7860", "--timeout", "0", "app:app"]