# Dockerfile - With models baked into the image FROM python:3.11-slim AS builder WORKDIR /app # Install build dependencies RUN apt-get update && apt-get install -y \ wget \ git \ && rm -rf /var/lib/apt/lists/* # Copy and install Python dependencies COPY requirements.txt . RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir -r requirements.txt # Download models during build ENV U2NET_HOME=/tmp/u2net_models RUN mkdir -p /tmp/u2net_models && \ python -c "from rembg import new_session; new_session('u2netp')" && \ echo "✅ u2netp model downloaded" # Try to download silueta as well (optional, may fail) RUN python -c "from rembg import new_session; new_session('silueta')" 2>/dev/null || echo "⚠️ silueta download skipped" # Final stage FROM python:3.11-slim WORKDIR /app # Install runtime dependencies only RUN apt-get update && apt-get install -y \ ffmpeg \ libsm6 \ libxext6 \ libgomp1 \ && rm -rf /var/lib/apt/lists/* # Create directories RUN mkdir -p /tmp/u2net_models /tmp/uploads /tmp/outputs # 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 pre-downloaded models from builder COPY --from=builder /tmp/u2net_models /tmp/u2net_models # Copy application code COPY app.py . # Set environment variables ENV U2NET_HOME=/tmp/u2net_models ENV OMP_NUM_THREADS=1 ENV CUDA_VISIBLE_DEVICES="" ENV PYTHONUNBUFFERED=1 EXPOSE 7860 CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--timeout-keep-alive", "300"]