sachinchandrankallar commited on
Commit
5547093
·
1 Parent(s): 232a26e

`Refactor Dockerfile to support multi-arch/mode builds and Spaces compatibility`

Browse files
Files changed (1) hide show
  1. Dockerfile +86 -42
Dockerfile CHANGED
@@ -1,3 +1,4 @@
 
1
  # FROM python:3.10-slim
2
 
3
  # # Install system dependencies
@@ -127,53 +128,96 @@
127
 
128
  # CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "1", "--threads", "8", "--timeout", "0", "ai_med_extract.app:app"]
129
 
130
- # Ultra-minimal Spaces-optimized Dockerfile
131
- FROM python:3.11-slim
132
-
133
- # Minimize layers and maximize cache hits
134
- RUN apt-get update \
135
- && apt-get install -y --no-install-recommends \
136
- tzdata \
137
- tesseract-ocr \
138
- poppler-utils \
139
- ffmpeg \
140
- && ln -fs /usr/share/zoneinfo/Etc/UTC /etc/localtime \
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  && dpkg-reconfigure -f noninteractive tzdata \
142
- && rm -rf /var/lib/apt/lists/* \
143
- && apt-get clean \
144
- && rm -rf /tmp/*
 
 
 
145
 
146
  WORKDIR /app
 
147
 
148
- # Copy and install requirements in one layer for better caching
149
- COPY requirements.txt .
150
- RUN pip install --no-cache-dir --upgrade pip \
151
- && pip install --no-cache-dir -r requirements.txt gunicorn
152
-
153
- # Copy only essential application files
154
- COPY ai_med_extract.py .
155
- COPY services/ai-service/src/ai_med_extract ./ai_med_extract/
156
- COPY services/ai-service/src/app.py .
157
- COPY services/ai-service/src/config_settings.py ./ai_med_extract/
158
- COPY services/ai-service/src/gradio_app.py ./ai_med_extract/
159
- COPY services/ai-service/src/wsgi.py .
160
-
161
- # Essential environment variables for Spaces compatibility
162
- ENV PYTHONUNBUFFERED=1 \
163
- PYTHONDONTWRITEBYTECODE=1 \
164
- PYTHONPATH=/app \
165
- HF_HOME=/tmp \
166
- XDG_CACHE_HOME=/tmp \
167
- TORCH_HOME=/tmp \
168
- WHISPER_CACHE=/tmp \
169
- FAST_MODE=true \
170
- PRELOAD_SMALL_MODELS=false
171
 
172
- # Create necessary directories with correct permissions
173
- RUN mkdir -p /tmp/uploads /tmp/huggingface /tmp/torch /tmp/whisper \
174
- && chmod -R 777 /tmp
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
 
176
  EXPOSE 7860
177
 
178
- # Use Gunicorn with WSGI app (Spaces standard) - single worker for stability
179
- CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "1", "--threads", "2", "--timeout", "300", "--max-requests", "100", "--max-requests-jitter", "10", "wsgi:app"]
 
1
+
2
  # FROM python:3.10-slim
3
 
4
  # # Install system dependencies
 
128
 
129
  # CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "1", "--threads", "8", "--timeout", "0", "ai_med_extract.app:app"]
130
 
131
+
132
+ # Multi-arch/mode Dockerfile that supports both GPU and CPU builds via build-args.
133
+ # Defaults to CPU runtime suitable for Hugging Face Spaces Basic.
134
+
135
+ ARG BASE_IMAGE=python:3.10-slim
136
+
137
+ # Stage 1: builder (installs Python deps into a venv with build tooling)
138
+ FROM ${BASE_IMAGE} AS builder
139
+
140
+ ARG DEBIAN_FRONTEND=noninteractive
141
+ ENV TZ=Etc/UTC
142
+
143
+ # Install build tools only in builder
144
+ RUN apt-get update && apt-get install -y --no-install-recommends \
145
+ tzdata \
146
+ build-essential \
147
+ python3 \
148
+ python3-pip \
149
+ python3-venv \
150
+ python3-dev \
151
+ tesseract-ocr \
152
+ poppler-utils \
153
+ ffmpeg \
154
+ && ln -fs /usr/share/zoneinfo/$TZ /etc/localtime \
155
  && dpkg-reconfigure -f noninteractive tzdata \
156
+ && rm -rf /var/lib/apt/lists/*
157
+
158
+ # Create virtual environment
159
+ ENV VIRTUAL_ENV=/opt/venv
160
+ RUN python3 -m venv "$VIRTUAL_ENV"
161
+ ENV PATH="$VIRTUAL_ENV/bin:$PATH"
162
 
163
  WORKDIR /app
164
+ COPY requirements.txt ./
165
 
166
+ # Upgrade pip and install dependencies
167
+ RUN pip install --upgrade pip && \
168
+ pip install --prefer-binary -r requirements.txt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
+ # Stage 2: runtime (minimal runtime deps + venv from builder)
171
+ FROM ${BASE_IMAGE} AS runtime
172
+
173
+ ARG DEBIAN_FRONTEND=noninteractive
174
+ ENV TZ=Etc/UTC
175
+
176
+ # Install only runtime system packages; keep minimal
177
+ RUN apt-get update && apt-get install -y --no-install-recommends \
178
+ tzdata \
179
+ tesseract-ocr \
180
+ poppler-utils \
181
+ ffmpeg \
182
+ && ln -fs /usr/share/zoneinfo/$TZ /etc/localtime \
183
+ && dpkg-reconfigure -f noninteractive tzdata \
184
+ && rm -rf /var/lib/apt/lists/*
185
+
186
+ # Copy Python environment from builder
187
+ COPY --from=builder /opt/venv /opt/venv
188
+ ENV PATH="/opt/venv/bin:$PATH"
189
+
190
+ # App
191
+ WORKDIR /app
192
+ COPY . .
193
+
194
+ # Runtime cache dirs (kept in /tmp, auto-cleared on restart)
195
+ ENV HF_HOME=/tmp/huggingface \
196
+ XDG_CACHE_HOME=/tmp \
197
+ TORCH_HOME=/tmp/torch \
198
+ WHISPER_CACHE=/tmp/whisper \
199
+ PYTHONUNBUFFERED=1 \
200
+ PYTHONPATH=/app \
201
+ GGUF_N_THREADS=2 \
202
+ GGUF_N_BATCH=64 \
203
+ OMP_NUM_THREADS=2 \
204
+ MKL_NUM_THREADS=2 \
205
+ NUMEXPR_NUM_THREADS=2
206
+
207
+ # Ensure writable directories exist
208
+ RUN mkdir -p /tmp/uploads /tmp/huggingface /tmp/torch /tmp/whisper && \
209
+ chmod -R 777 /tmp
210
+
211
+ # Add entrypoint script that clears cache/models before app starts
212
+ RUN echo '#!/bin/bash\n\
213
+ echo "[ENTRYPOINT] Clearing Hugging Face / Torch / tmp cache..."\n\
214
+ rm -rf /tmp/* ~/.cache/huggingface ~/.cache/torch || true\n\
215
+ mkdir -p /tmp/uploads /tmp/huggingface /tmp/torch /tmp/whisper\n\
216
+ chmod -R 777 /tmp\n\
217
+ exec "$@"' > /entrypoint.sh && chmod +x /entrypoint.sh
218
+
219
+ ENTRYPOINT ["/entrypoint.sh"]
220
 
221
  EXPOSE 7860
222
 
223
+ CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "1", "--threads", "2", "--timeout", "0", "ai_med_extract.app:app"]