abir614 commited on
Commit
ceef53d
Β·
verified Β·
1 Parent(s): 1d647b0

Upload Dockerfile with huggingface_hub

Browse files
Files changed (1) hide show
  1. Dockerfile +89 -103
Dockerfile CHANGED
@@ -1,166 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # ============================================================
2
  # Stage 1 β€” Builder
3
- # Ultra-optimized wheel + model builder
4
  # ============================================================
5
  FROM python:3.11-slim-bookworm AS builder
6
 
7
  ENV DEBIAN_FRONTEND=noninteractive \
8
  PYTHONDONTWRITEBYTECODE=1 \
9
  PYTHONUNBUFFERED=1 \
 
10
  PIP_DISABLE_PIP_VERSION_CHECK=1 \
11
- PIP_NO_CACHE_DIR=1 \
12
  PIP_ROOT_USER_ACTION=ignore \
13
  VIRTUAL_ENV=/opt/venv \
14
- PATH="/opt/venv/bin:$PATH"
 
 
15
 
16
- # Only absolute minimum build dependencies
 
17
  RUN apt-get update && apt-get install -y --no-install-recommends \
18
- gcc \
19
- g++ \
20
- curl \
21
- ca-certificates \
 
22
  && rm -rf /var/lib/apt/lists/*
23
 
24
- # Create isolated venv
25
  RUN python -m venv $VIRTUAL_ENV
26
 
27
  WORKDIR /app
28
-
29
  COPY backend/requirements.txt .
30
 
31
- # Faster pip + smaller install
32
- RUN pip install --upgrade pip wheel setuptools
33
 
34
- # Install CPU-only torch first
 
35
  RUN --mount=type=cache,target=/root/.cache/pip \
36
- pip install --no-cache-dir \
37
- torch torchvision \
38
- --index-url https://download.pytorch.org/whl/cpu
39
 
40
- # Install remaining deps
41
  RUN --mount=type=cache,target=/root/.cache/pip \
42
- pip install --no-cache-dir -r requirements.txt
43
-
44
- # Copy backend only
45
- COPY backend/main.py .
46
- COPY backend/processing.py .
47
 
48
- # ============================================================
49
- # Pre-download models
50
- # ============================================================
51
 
52
- ENV U2NET_HOME=/opt/models/u2net
53
 
 
54
  RUN python -c "\
55
  from rembg import new_session; \
56
- new_session('isnet-general-use')"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- RUN python -c "\
59
- from iopaint.model.lama import LaMa, LAMA_MODEL_URL, LAMA_MODEL_MD5; \
60
- from iopaint.helper import download_model; \
61
- download_model(LAMA_MODEL_URL, LAMA_MODEL_MD5)"
62
 
63
  # ============================================================
64
- # Aggressive cleanup
65
- # ============================================================
66
-
67
- RUN find /opt/venv -type d -name '__pycache__' -exec rm -rf {} + && \
68
- find /opt/venv -type d -name 'tests' -exec rm -rf {} + && \
69
- find /opt/venv -type d -name 'test' -exec rm -rf {} + && \
70
- find /opt/venv -type f -name '*.pyc' -delete && \
71
- find /opt/venv -type f -name '*.pyo' -delete && \
72
- find /opt/venv -type f -name '*.a' -delete && \
73
- find /opt/venv -type f -name '*.so.debug' -delete && \
74
- strip --strip-unneeded /opt/venv/lib/python3.11/site-packages/**/*.so 2>/dev/null || true
75
-
76
- # ============================================================
77
- # Stage 2 β€” Runtime
78
- # Distroless-style slim runtime
79
  # ============================================================
80
  FROM python:3.11-slim-bookworm
81
 
82
- LABEL maintainer="Ultimate-AI-Container" \
83
- description="Extreme optimized FastAPI + rembg + LaMa container"
84
 
85
- # ============================================================
86
- # Runtime environment optimizations
87
- # ============================================================
88
-
89
- ENV DEBIAN_FRONTEND=noninteractive \
90
- PYTHONDONTWRITEBYTECODE=1 \
91
  PYTHONUNBUFFERED=1 \
92
  PYTHONFAULTHANDLER=1 \
93
- PYTHONHASHSEED=random \
94
- PIP_DISABLE_PIP_VERSION_CHECK=1 \
95
- PIP_ROOT_USER_ACTION=ignore \
96
  VIRTUAL_ENV=/opt/venv \
97
  PATH="/opt/venv/bin:$PATH" \
98
  PYTHONPATH=/app \
 
99
  U2NET_HOME=/opt/models/u2net \
100
- HF_HOME=/tmp/huggingface \
101
- MPLCONFIGDIR=/tmp/matplotlib \
102
- TORCH_HOME=/tmp/torch \
103
- OMP_NUM_THREADS=1 \
104
- OPENBLAS_NUM_THREADS=1 \
105
- MKL_NUM_THREADS=1 \
106
- NUMEXPR_NUM_THREADS=1 \
107
- VECLIB_MAXIMUM_THREADS=1 \
108
  MALLOC_ARENA_MAX=2 \
109
  OPENCV_OPENCL_RUNTIME=disabled \
110
- OPENCV_IO_ENABLE_OPENEXR=0 \
111
  ORT_DISABLE_TELEMETRY=1
112
 
113
- # Runtime-only libs
 
 
 
 
 
 
114
  RUN apt-get update && apt-get install -y --no-install-recommends \
115
- libglib2.0-0 \
116
- libgomp1 \
117
- libstdc++6 \
118
- libgl1 \
119
- curl \
120
- && rm -rf /var/lib/apt/lists/*
121
 
122
- # Non-root user
123
- RUN useradd -r -u 1001 -g users appuser
124
 
125
  WORKDIR /app
126
 
127
- # Copy only final runtime artifacts
128
- COPY --from=builder /opt/venv /opt/venv
129
- COPY --from=builder /opt/models /opt/models
130
-
131
- # Backend
132
- COPY backend/main.py .
133
- COPY backend/processing.py .
134
-
135
- # Static assets
136
- COPY index.html ./static/index.html
137
- COPY style.css ./static/style.css
138
- COPY script.js ./static/script.js
139
  RUN wget -O ./static/favicon.ico "https://raw.githubusercontent.com/abir614/IMGFLOW/refs/heads/main/favicon.ico"
140
 
141
- # Permissions
142
- RUN mkdir -p /app/static && \
143
- chown -R appuser:users /app /opt/models
144
 
145
  USER appuser
146
 
147
- EXPOSE 7860
148
-
149
- # ============================================================
150
- # Healthcheck
151
- # ============================================================
152
-
153
- HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
154
- CMD curl -fsS http://127.0.0.1:7860/api/health || exit 1
155
 
156
- # ============================================================
157
- # Launch
158
- # ============================================================
159
 
160
  CMD ["uvicorn", "main:app", \
161
  "--host", "0.0.0.0", \
162
  "--port", "7860", \
163
  "--workers", "1", \
164
  "--loop", "uvloop", \
165
- "--http", "httptools", \
166
- "--no-access-log"]
 
1
+ # syntax=docker/dockerfile:1.7-labs
2
+
3
+ # ─────────────────────────────────────────────────────────────
4
+ # IMGFLOW β€” Production Dockerfile
5
+ # Target: HuggingFace Spaces (CPU, x86_64, port 7860)
6
+ #
7
+ # Base image rationale β€” python:3.11-slim-bookworm:
8
+ # β€’ Alpine/musl: INCOMPATIBLE β€” torch, numpy, scipy, onnxruntime
9
+ # all ship manylinux (glibc) wheels only; musl libc breaks them
10
+ # β€’ distroless: no shell β†’ can't run healthcheck curl or venv activate
11
+ # β€’ slim-bookworm: smallest Debian 12 (glibc 2.36) image that runs
12
+ # the full stack; libstdc++6 pre-installed; verified compatible
13
+ # with every wheel in requirements.txt
14
+ # β€’ bullseye (Debian 11): glibc 2.31 also works but older security
15
+ # patches and HF build runners default to bookworm
16
+ # ─────────────────────────────────────────────────────────────
17
+
18
  # ============================================================
19
  # Stage 1 β€” Builder
 
20
  # ============================================================
21
  FROM python:3.11-slim-bookworm AS builder
22
 
23
  ENV DEBIAN_FRONTEND=noninteractive \
24
  PYTHONDONTWRITEBYTECODE=1 \
25
  PYTHONUNBUFFERED=1 \
26
+ # Leave pip cache enabled here β€” mount cache handles it efficiently
27
  PIP_DISABLE_PIP_VERSION_CHECK=1 \
 
28
  PIP_ROOT_USER_ACTION=ignore \
29
  VIRTUAL_ENV=/opt/venv \
30
+ PATH="/opt/venv/bin:$PATH" \
31
+ TORCH_HOME=/opt/models/torch \
32
+ U2NET_HOME=/opt/models/u2net
33
 
34
+ # Build-time deps
35
+ # libGL + libglib2.0-0 are needed by cv2 at import time (rembg pre-download step)
36
  RUN apt-get update && apt-get install -y --no-install-recommends \
37
+ gcc \
38
+ curl \
39
+ ca-certificates \
40
+ libgl1 \
41
+ libglib2.0-0 \
42
  && rm -rf /var/lib/apt/lists/*
43
 
 
44
  RUN python -m venv $VIRTUAL_ENV
45
 
46
  WORKDIR /app
 
47
  COPY backend/requirements.txt .
48
 
49
+ RUN pip install --upgrade pip setuptools wheel packaging
 
50
 
51
+ # PyTorch CPU wheel must be installed BEFORE iopaint to avoid torch version
52
+ # conflicts. Explicit CPU index prevents pip pulling a 3 GB CUDA build.
53
  RUN --mount=type=cache,target=/root/.cache/pip \
54
+ pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
 
 
55
 
 
56
  RUN --mount=type=cache,target=/root/.cache/pip \
57
+ pip install -r requirements.txt
 
 
 
 
58
 
59
+ COPY backend/main.py backend/processing.py ./
 
 
60
 
61
+ # ── Pre-download AI models at build time (zero cold-start delay) ──────────
62
 
63
+ # ISNet background removal model (~170 MB)
64
  RUN python -c "\
65
  from rembg import new_session; \
66
+ new_session('isnet-general-use'); \
67
+ print('βœ“ ISNet ready')"
68
+
69
+ # LaMa inpainting model (~200 MB) β€” curl avoids importing iopaint.model
70
+ # which would chain-import diffusers/anytext and fail before torch is ready
71
+ RUN mkdir -p /opt/models/torch/hub/checkpoints && \
72
+ curl -fsSL --retry 3 --retry-delay 2 \
73
+ -o /opt/models/torch/hub/checkpoints/big-lama.pt \
74
+ https://github.com/Sanster/models/releases/download/add_big_lama/big-lama.pt && \
75
+ echo "βœ“ LaMa ready"
76
+
77
+ # ── Aggressive venv cleanup (~40–80 MB saved) ─────────────────────────────
78
+ RUN find $VIRTUAL_ENV \( \
79
+ -type d -name '__pycache__' -o \
80
+ -type d -name 'tests' -o \
81
+ -type d -name 'test' -o \
82
+ -type d -name '*.dist-info' -o \
83
+ -type f -name '*.pyc' -o \
84
+ -type f -name '*.pyo' -o \
85
+ -type f -name '*.pyd' \
86
+ \) -exec rm -rf {} + 2>/dev/null || true
87
 
 
 
 
 
88
 
89
  # ============================================================
90
+ # Stage 2 β€” Runtime (inherits nothing from builder except COPY)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  # ============================================================
92
  FROM python:3.11-slim-bookworm
93
 
94
+ LABEL org.opencontainers.image.description="IMGFLOW β€” FastAPI image processing for HuggingFace Spaces"
 
95
 
96
+ ENV PYTHONDONTWRITEBYTECODE=1 \
 
 
 
 
 
97
  PYTHONUNBUFFERED=1 \
98
  PYTHONFAULTHANDLER=1 \
 
 
 
99
  VIRTUAL_ENV=/opt/venv \
100
  PATH="/opt/venv/bin:$PATH" \
101
  PYTHONPATH=/app \
102
+ TORCH_HOME=/opt/models/torch \
103
  U2NET_HOME=/opt/models/u2net \
104
+ # Thread pinning: prevents CPU oversubscription under single-worker uvicorn
105
+ OMP_NUM_THREADS=2 \
106
+ OPENBLAS_NUM_THREADS=2 \
107
+ MKL_NUM_THREADS=2 \
108
+ NUMEXPR_NUM_THREADS=2 \
109
+ VECLIB_MAXIMUM_THREADS=2 \
110
+ # Limit glibc malloc arena growth (saves ~50–150 MB RSS on long-running AI workloads)
 
111
  MALLOC_ARENA_MAX=2 \
112
  OPENCV_OPENCL_RUNTIME=disabled \
 
113
  ORT_DISABLE_TELEMETRY=1
114
 
115
+ # Runtime native libs β€” deliberately minimal:
116
+ # libglib2.0-0 : cv2 links libglib-2.0.so.0 (not bundled in headless wheel)
117
+ # libgomp1 : OpenMP used by numpy, scipy, torch CPU kernels
118
+ # curl : healthcheck only β€” wget not in slim-bookworm by default
119
+ # NOT needed (already in slim-bookworm or bundled by wheel):
120
+ # libstdc++6 : pre-installed in every slim image
121
+ # libgl1 : cv2 >= 4.6 headless bundles its own libGL stub
122
  RUN apt-get update && apt-get install -y --no-install-recommends \
123
+ libglib2.0-0 \
124
+ libgomp1 \
125
+ curl \
126
+ && apt-get clean && rm -rf /var/lib/apt/lists/*
 
 
127
 
128
+ RUN useradd -m -u 1001 appuser
 
129
 
130
  WORKDIR /app
131
 
132
+ COPY --from=builder /opt/venv /opt/venv
133
+ COPY --from=builder /opt/models /opt/models
134
+ COPY backend/main.py backend/processing.py ./
135
+ COPY index.html style.css script.js ./static/
 
 
 
 
 
 
 
 
136
  RUN wget -O ./static/favicon.ico "https://raw.githubusercontent.com/abir614/IMGFLOW/refs/heads/main/favicon.ico"
137
 
138
+ RUN chown -R appuser:appuser /app /opt/models
 
 
139
 
140
  USER appuser
141
 
142
+ HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
143
+ CMD curl -fsS http://127.0.0.1:7860/api/health || exit 1
 
 
 
 
 
 
144
 
145
+ EXPOSE 7860
 
 
146
 
147
  CMD ["uvicorn", "main:app", \
148
  "--host", "0.0.0.0", \
149
  "--port", "7860", \
150
  "--workers", "1", \
151
  "--loop", "uvloop", \
152
+ "--http", "httptools"]