File size: 1,507 Bytes
4adc200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d5dd00
 
 
4adc200
5d5dd00
 
4adc200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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"]