# 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"] # Ultra-minimal Dockerfile optimized for Hugging Face Spaces # Based on Spaces best practices for maximum cache efficiency FROM python:3.10-slim # Minimize layers and maximize cache hits RUN apt-get update \ && apt-get install -y --no-install-recommends \ tzdata \ tesseract-ocr \ poppler-utils \ ffmpeg \ && ln -fs /usr/share/zoneinfo/Etc/UTC /etc/localtime \ && dpkg-reconfigure -f noninteractive tzdata \ && rm -rf /var/lib/apt/lists/* \ && apt-get clean \ && rm -rf /tmp/* WORKDIR /app # Copy and install requirements in one layer for better caching COPY requirements.txt . RUN pip install --no-cache-dir --upgrade pip \ && pip install --no-cache-dir -r requirements.txt # Copy application code - only essential files COPY ai_med_extract.py . COPY services/ai-service/src/ai_med_extract ./ai_med_extract/ COPY services/ai-service/src/app.py ./ai_med_extract/ COPY services/ai-service/src/config_settings.py ./ai_med_extract/ COPY services/ai-service/src/gradio_app.py ./ai_med_extract/ COPY services/ai-service/src/wsgi.py ./ai_med_extract/ # Essential environment variables for Spaces compatibility ENV PYTHONUNBUFFERED=1 \ PYTHONPATH=/app \ HF_HOME=/tmp \ XDG_CACHE_HOME=/tmp \ TORCH_HOME=/tmp \ WHISPER_CACHE=/tmp \ FAST_MODE=true \ PRELOAD_SMALL_MODELS=false # Create necessary directories with correct permissions RUN mkdir -p /tmp/uploads /tmp/huggingface /tmp/torch /tmp/whisper \ && chmod -R 777 /tmp # Simple entrypoint - minimal script to avoid cache issues RUN echo '#!/bin/bash\nrm -rf /tmp/* ~/.cache/* || true\nmkdir -p /tmp/uploads /tmp/huggingface /tmp/torch /tmp/whisper\nchmod -R 777 /tmp\nexec "$@"' > /start.sh && chmod +x /start.sh ENTRYPOINT ["/start.sh"] EXPOSE 7860 # Use uvicorn to serve the FastAPI app directly CMD ["uvicorn", "ai_med_extract.app:app", "--host", "0.0.0.0", "--port", "7860"]