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