# ── ml-service/Dockerfile (Hugging Face Spaces compatible) ─────────────────── FROM python:3.10-slim WORKDIR /app # System deps RUN apt-get update && apt-get install -y --no-install-recommends \ gcc \ libgl1 \ libglib2.0-0 \ && rm -rf /var/lib/apt/lists/* # Create non-root user required by Hugging Face Spaces RUN useradd -m -u 1000 user USER user ENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH WORKDIR /home/user/app # Install Python deps as non-root user RUN pip install --upgrade pip --user # Install PyTorch CPU first (large, separate layer for better caching) RUN pip install --no-cache-dir --user \ torch>=2.9.0 torchvision>=0.20.0 \ --index-url https://download.pytorch.org/whl/cpu # Install remaining dependencies RUN pip install --no-cache-dir --user \ fastapi>=0.110.0 \ uvicorn>=0.29.0 \ python-multipart>=0.0.9 \ transformers>=4.35.2 \ Pillow>=10.4.0 \ numpy>=2.0.0 \ opencv-python-headless \ anthropic>=0.25.0 \ pinecone>=3.0.0 \ python-dotenv>=1.0.0 COPY --chown=user . . # Download models at build time so they're baked into the image (no cold-start download) RUN python download_models.py # HF Spaces requires port 7860 EXPOSE 7860 CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]