# FROM python:3.10-slim # # Install system dependencies # RUN apt-get update && apt-get install -y \ # tesseract-ocr \ # poppler-utils \ # ffmpeg \ # && rm -rf /var/lib/apt/lists/* # # Set working directory # WORKDIR /app # # Copy requirements first to leverage Docker cache # COPY requirements.txt . # # Install Python dependencies # RUN pip install --no-cache-dir -r requirements.txt # # Copy application code # COPY . . # # Create necessary directories with proper permissions # RUN mkdir -p /data/uploads /tmp/huggingface /tmp/torch /tmp/whisper && \ # chmod -R 777 /data /tmp # # Set environment variables # ENV PYTHONUNBUFFERED=1 # ENV HF_HOME=/tmp/huggingface # ENV HF_HOME=/tmp/huggingface # ENV XDG_CACHE_HOME=/tmp # ENV TORCH_HOME=/tmp/torch # ENV WHISPER_CACHE=/tmp/whisper # ENV PYTHONPATH=/app # # Expose port # EXPOSE 7860 # # Run the application with gunicorn # CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "1", "--threads", "8", "--timeout", "0", "ai_med_extract.app:app"] # # Multi-arch/mode Dockerfile that supports both GPU and CPU builds via build-args. # # Defaults to CPU runtime suitable for Hugging Face Spaces Basic. # ARG BASE_IMAGE=python:3.10-slim # # Stage 1: builder (installs Python deps into a venv with build tooling) # FROM ${BASE_IMAGE} AS builder # ARG DEBIAN_FRONTEND=noninteractive # ENV TZ=Etc/UTC # # Install build tools only in builder # RUN apt-get update && apt-get install -y --no-install-recommends \ # tzdata \ # build-essential \ # python3 \ # python3-pip \ # python3-venv \ # python3-dev \ # tesseract-ocr \ # poppler-utils \ # ffmpeg \ # && ln -fs /usr/share/zoneinfo/$TZ /etc/localtime \ # && dpkg-reconfigure -f noninteractive tzdata \ # && rm -rf /var/lib/apt/lists/* # # Create virtual environment # ENV VIRTUAL_ENV=/opt/venv # RUN python3 -m venv "$VIRTUAL_ENV" # ENV PATH="$VIRTUAL_ENV/bin:$PATH" # WORKDIR /app # COPY requirements.txt ./ # # Upgrade pip and install dependencies # RUN pip install --upgrade pip && \ # pip install --prefer-binary -r requirements.txt # # Stage 2: runtime (minimal runtime deps + venv from builder) # FROM ${BASE_IMAGE} AS runtime # ARG DEBIAN_FRONTEND=noninteractive # ENV TZ=Etc/UTC # # Install only runtime system packages; keep minimal # RUN apt-get update && apt-get install -y --no-install-recommends \ # tzdata \ # tesseract-ocr \ # poppler-utils \ # ffmpeg \ # && ln -fs /usr/share/zoneinfo/$TZ /etc/localtime \ # && dpkg-reconfigure -f noninteractive tzdata \ # && rm -rf /var/lib/apt/lists/* # # Copy Python environment from builder # COPY --from=builder /opt/venv /opt/venv # ENV PATH="/opt/venv/bin:$PATH" # # App # WORKDIR /app # COPY . . # # Reasonable cache dirs at runtime (kept outside image layers) # ENV HF_HOME=/tmp/huggingface \ # XDG_CACHE_HOME=/tmp \ # TORCH_HOME=/tmp/torch \ # WHISPER_CACHE=/tmp/whisper \ # PYTHONUNBUFFERED=1 \ # PYTHONPATH=/app \ # GGUF_N_THREADS=2 \ # GGUF_N_BATCH=64 \ # OMP_NUM_THREADS=2 \ # MKL_NUM_THREADS=2 \ # NUMEXPR_NUM_THREADS=2 # # Ensure writable directories exist (works on Spaces read-only root) # RUN mkdir -p /tmp/uploads /tmp/huggingface /tmp/torch /tmp/whisper && \ # chmod -R 777 /tmp # EXPOSE 7860 # CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "1", "--threads", "8", "--timeout", "0", "ai_med_extract.app:app"] # Multi-arch/mode Dockerfile that supports both GPU and CPU builds via build-args. # Defaults to CPU runtime suitable for Hugging Face Spaces Basic. ARG BASE_IMAGE=python:3.10-slim # Stage 1: builder (installs Python deps into a venv with build tooling) FROM ${BASE_IMAGE} AS builder ARG DEBIAN_FRONTEND=noninteractive ENV TZ=Etc/UTC # Install build tools only in builder RUN apt-get update && apt-get install -y --no-install-recommends \ tzdata \ build-essential \ python3 \ python3-pip \ python3-venv \ python3-dev \ tesseract-ocr \ poppler-utils \ ffmpeg \ && ln -fs /usr/share/zoneinfo/$TZ /etc/localtime \ && dpkg-reconfigure -f noninteractive tzdata \ && rm -rf /var/lib/apt/lists/* # Create virtual environment ENV VIRTUAL_ENV=/opt/venv RUN python3 -m venv "$VIRTUAL_ENV" ENV PATH="$VIRTUAL_ENV/bin:$PATH" WORKDIR /app COPY requirements.txt ./ # Upgrade pip and install dependencies RUN pip install --upgrade pip && \ pip install --prefer-binary -r requirements.txt # Stage 2: runtime (minimal runtime deps + venv from builder) FROM ${BASE_IMAGE} AS runtime ARG DEBIAN_FRONTEND=noninteractive ENV TZ=Etc/UTC # Install only runtime system packages; keep minimal RUN apt-get update && apt-get install -y --no-install-recommends \ tzdata \ tesseract-ocr \ poppler-utils \ ffmpeg \ && ln -fs /usr/share/zoneinfo/$TZ /etc/localtime \ && dpkg-reconfigure -f noninteractive tzdata \ && rm -rf /var/lib/apt/lists/* # Copy Python environment from builder COPY --from=builder /opt/venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" # App WORKDIR /app COPY . . # Runtime cache dirs (kept in /tmp, auto-cleared on restart) ENV HF_HOME=/tmp/huggingface \ XDG_CACHE_HOME=/tmp \ TORCH_HOME=/tmp/torch \ WHISPER_CACHE=/tmp/whisper \ PYTHONUNBUFFERED=1 \ PYTHONPATH=/app \ GGUF_N_THREADS=2 \ GGUF_N_BATCH=64 \ OMP_NUM_THREADS=2 \ MKL_NUM_THREADS=2 \ NUMEXPR_NUM_THREADS=2 # Ensure writable directories exist RUN mkdir -p /tmp/uploads /tmp/huggingface /tmp/torch /tmp/whisper && \ chmod -R 777 /tmp # Add entrypoint script that clears cache/models before app starts RUN echo '#!/bin/bash\n\ echo "[ENTRYPOINT] Clearing Hugging Face / Torch / tmp cache..."\n\ rm -rf /tmp/* ~/.cache/huggingface ~/.cache/torch || true\n\ mkdir -p /tmp/uploads /tmp/huggingface /tmp/torch /tmp/whisper\n\ chmod -R 777 /tmp\n\ exec "$@"' > /entrypoint.sh && chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] EXPOSE 7860 CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "1", "--threads", "2", "--timeout", "0", "ai_med_extract.app:app"]