File size: 2,358 Bytes
da9126b
 
 
 
 
4aa9277
da9126b
c7efece
 
da9126b
 
 
 
c7efece
 
 
 
 
 
da9126b
 
 
 
 
 
 
 
 
 
c7efece
da9126b
c7efece
 
da9126b
c7efece
 
 
 
 
 
 
 
 
 
 
 
a017646
c7efece
a017646
c7efece
 
da9126b
 
 
 
 
 
 
8d0d76d
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# =============================================================================
# Dockerfile for Brain Lesion Segmentation — Hugging Face Spaces (Docker SDK)
# Serves FastAPI backend + VTK.js static frontend on port 7860
# =============================================================================

FROM pytorch/pytorch:2.1.0-cuda11.8-cudnn8-runtime

# Install system dependencies required for nnunetv2 and its transitive deps
# (SimpleITK, blosc2, imagecodecs need C compilation toolchains)
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    curl \
    git \
    pkg-config \
    libjpeg-dev \
    libpng-dev \
    libtiff-dev \
    libopenblas-dev \
    graphviz \
    && rm -rf /var/lib/apt/lists/*

# Create non-root user (HF Spaces requirement)
RUN useradd -m -u 1000 user
USER user
ENV HOME=/home/user \
    PATH=/home/user/.local/bin:$PATH

WORKDIR /home/user/app

# Copy requirements first for Docker cache
COPY --chown=user:user requirements.txt .

# Step 1: Install all deps EXCEPT nnunetv2 (keep base image's CUDA torch)
RUN pip install --no-cache-dir --upgrade pip && \
    grep -v nnunetv2 requirements.txt | pip install --no-cache-dir -r /dev/stdin

# Step 2: Install nnunetv2's non-torch dependencies, then nnunetv2 itself
#         with --no-deps so it does NOT overwrite the CUDA PyTorch
RUN pip install --no-cache-dir \
    acvl-utils batchgenerators batchgeneratorsv2 blosc2 \
    dynamic-network-architectures graphviz imagecodecs \
    pandas requests scikit-image scikit-learn seaborn \
    SimpleITK tifffile tqdm yacs && \
    pip install --no-cache-dir --no-deps nnunetv2>=2.2

# Verify critical packages installed (fail build early if not)
# Note: CUDA is NOT available during docker build, only at runtime
RUN python -c "import nnunetv2; print('nnunetv2 OK')" && \
    python -c "import torch; print('torch', torch.__version__)" && \
    python -c "import fastapi; print('fastapi OK')" && \
    python -c "import monai; print('monai OK')"

# Copy application code
COPY --chown=user:user . .

# Hugging Face Spaces expects port 7860
EXPOSE 7860

# Use entrypoint script that sanitizes OMP_NUM_THREADS before starting.
# HF Spaces Kubernetes injects OMP_NUM_THREADS=3500m which crashes libgomp.
# The entrypoint forcibly overrides it with a valid integer.
CMD ["bash", "entrypoint.sh"]