# Multi-stage build for production FROM python:3.11-slim as builder # Install system dependencies for OpenCV RUN apt-get update && apt-get install -y \ gcc \ g++ \ libgl1 \ libglib2.0-0 \ libsm6 \ libxext6 \ libxrender1 \ libgomp1 \ && rm -rf /var/lib/apt/lists/* # Set working directory WORKDIR /app # Copy requirements first for better layer caching COPY requirements.txt . # Install Python dependencies RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir -r requirements.txt # Production stage FROM python:3.11-slim # Install runtime dependencies for OpenCV RUN apt-get update && apt-get install -y \ libgl1 \ libglib2.0-0 \ libsm6 \ libxext6 \ libxrender1 \ libgomp1 \ && rm -rf /var/lib/apt/lists/* \ && apt-get clean # Create non-root user RUN useradd --create-home --shell /bin/bash app # Set working directory WORKDIR /app # Copy Python packages from builder COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages COPY --from=builder /usr/local/bin /usr/local/bin # Copy application code COPY . . # Create necessary directories RUN mkdir -p storage/temp storage/processed storage/rejected # Set ownership and permissions RUN chown -R app:app /app && \ chmod -R 755 /app # Switch to non-root user USER app # Expose port (Hugging Face Spaces uses 7860) EXPOSE 7860 # Set environment variables ENV FLASK_ENV=production ENV PYTHONUNBUFFERED=1 # Health check HEALTHCHECK --interval=30s --timeout=30s --start-period=60s --retries=3 \ CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:7860/api/health')" || exit 1 # Production server command for Hugging Face Spaces CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "2", "--timeout", "120", "--access-logfile", "-", "--error-logfile", "-", "wsgi:app"]