FROM python:3.11-slim # Set environment variables for Hugging Face Spaces ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ PORT=7860 \ HOME=/home/user \ USE_LOCAL_MODELS=true \ HF_SINGLE_PROCESS_MODE=true # Create user (required for HF Spaces) RUN useradd -m -u 1000 user # Install system dependencies RUN apt-get update && apt-get install -y \ build-essential \ curl \ libpq-dev \ ffmpeg \ git \ && rm -rf /var/lib/apt/lists/* WORKDIR /app # Upgrade pip RUN pip install --no-cache-dir --upgrade pip # Step 1: Install CPU-only Torch first (prevents OOM on HF Spaces) RUN pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu # Step 2: Install remaining requirements COPY --chown=user:user requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Copy application code COPY --chown=user:user . . # Create necessary directories RUN mkdir -p /app/uploads /app/data && \ chown -R 1000:1000 /app # Switch to non-root user USER user # Expose port EXPOSE 7860 # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \ CMD curl -f http://localhost:7860/health || exit 1 # Start the application CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]