FROM python:3.11-slim # Set environment variables ENV PYTHONUNBUFFERED=1 \ PORT=7860 \ HOME=/home/user \ HF_HOME=/tmp/huggingface \ TRANSFORMERS_CACHE=/tmp/huggingface # Install system dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ curl \ git \ && rm -rf /var/lib/apt/lists/* # Create non-root user for Hugging Face Spaces (UID 1000) RUN useradd -m -u 1000 user WORKDIR /home/user/app # Install PyTorch CPU first to keep image lightweight and fast RUN pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu # Install remaining Python dependencies COPY --chown=user:user requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Create HF cache directory with correct permissions RUN mkdir -p /tmp/huggingface && chown -R user:user /tmp/huggingface # Copy application files COPY --chown=user:user . . USER user # Hugging Face Space default port EXPOSE 7860 # Start FastAPI application CMD ["python", "api_server.py"]