FROM python:3.10-slim # Set the working directory WORKDIR /app # Install system dependencies required for OpenCV, git, and SAM 2 RUN apt-get update && apt-get install -y \ build-essential \ git \ wget \ && rm -rf /var/lib/apt/lists/* # Copy requirements file first to leverage Docker cache COPY requirements.txt . # Install pip upgrades and build tools before requirements RUN pip install --no-cache-dir --upgrade pip setuptools wheel ninja # Install Python dependencies RUN pip install --no-cache-dir -r requirements.txt # Hugging Face Spaces require running as a non-root user with UID 1000 RUN useradd -m -u 1000 user USER user ENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH # Change working directory to the user's home WORKDIR $HOME/app # Copy the rest of the application code COPY --chown=user . $HOME/app # Run the download models script during the build RUN python download_models.py # Create output directories with correct permissions RUN mkdir -p uploads static/crops templates static/css static/js && chmod 777 uploads static/crops # Auto-fix: If files were uploaded directly to the root, move them to their correct folders RUN mv index.html templates/ 2>/dev/null || true && \ mv styles.css static/css/ 2>/dev/null || true && \ mv main.js static/js/ 2>/dev/null || true # Expose the default port for Hugging Face Spaces EXPOSE 7860 # Run the Flask app directly so background threads work correctly with global state CMD ["python", "app.py"]