Spaces:
Sleeping
Sleeping
| # ---- Stage 1: The Builder ---- | |
| # This stage installs dependencies in a clean environment. | |
| FROM python:3.9 as builder | |
| WORKDIR /app | |
| # Install system build tools needed to compile some Python packages | |
| RUN apt-get update && apt-get install -y --no-install-recommends build-essential | |
| # Copy only the requirements file first to leverage Docker's caching | |
| COPY requirements.txt . | |
| # Create a virtual environment inside the builder | |
| RUN python -m venv /opt/venv | |
| # Activate the virtual environment | |
| ENV PATH="/opt/venv/bin:$PATH" | |
| # Install Python dependencies into the virtual environment | |
| # Using --no-cache-dir keeps the layer size smaller | |
| RUN pip install --no-cache-dir -r requirements.txt | |
| # ---- Stage 2: The Final Production Image ---- | |
| # Use a slim base image for a smaller final size | |
| FROM python:3.9-slim | |
| # Set the Hugging Face port environment variable | |
| EXPOSE 7860 | |
| ENV PORT=7860 | |
| WORKDIR /app | |
| # Install ONLY the necessary system dependencies for running OpenCV on a headless server | |
| # This is the key part referenced in the Hugging Face docs for OpenCV | |
| # --- NEW AND CORRECTED CODE --- | |
| RUN apt-get update && apt-get install -y --no-install-recommends \ | |
| ffmpeg \ | |
| libsm6 \ | |
| libxext6 \ | |
| libgl1 \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Copy the virtual environment from the builder stage | |
| COPY --from=builder /opt/venv /opt/venv | |
| # Copy the rest of your application code | |
| COPY . . | |
| # Create a non-root user for better security | |
| RUN useradd --create-home appuser | |
| USER appuser | |
| # Activate the virtual environment for the final command | |
| ENV PATH="/opt/venv/bin:$PATH" | |
| # Command to run the application using Gunicorn (a production WSGI server) | |
| # This is MUCH better than "python app.py" for stability and performance. | |
| # --workers 1 --threads 8: A good starting point for a CPU-based instance. | |
| # --timeout 0: Prevents the server from killing the long-running video stream connection. | |
| # --bind 0.0.0.0:$PORT: Binds to all interfaces and uses the port assigned by Hugging Face. | |
| CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "1", "--threads", "8", "--timeout", "0", "app:app"] |