# Hugging Face Space (Docker SDK) — serves the Flask app on port 7860. FROM python:3.10-slim # System libraries required by OpenCV RUN apt-get update && apt-get install -y --no-install-recommends \ libgl1 \ libglib2.0-0 \ && rm -rf /var/lib/apt/lists/* # HF Spaces run the container as a non-root user (uid 1000) RUN useradd -m -u 1000 user USER user ENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH \ PYTHONUNBUFFERED=1 \ TORCH_NUM_THREADS=1 \ OMP_NUM_THREADS=1 \ MPLCONFIGDIR=/home/user/.cache/matplotlib \ YOLO_CONFIG_DIR=/home/user/.config/Ultralytics WORKDIR /home/user/app # Install CPU-only PyTorch + torchvision FIRST, both from the CPU index so their # compiled ops match. (If torchvision comes from default PyPI it's the CUDA build # and fails at runtime with "operator torchvision::nms does not exist".) RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir torch==2.7.1 torchvision==0.22.1 \ --index-url https://download.pytorch.org/whl/cpu # Dependency layer (cached unless requirements.txt changes) COPY --chown=user requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Application code COPY --chown=user . . # Download the OCR models at build time (best.pt ships in the repo) RUN python setup_models.py EXPOSE 7860 CMD ["gunicorn", "main:app", \ "--workers", "1", "--threads", "2", \ "--timeout", "300", "--graceful-timeout", "300", \ "--bind", "0.0.0.0:7860"]