# Base image - using bullseye for better compatibility FROM python:3.9-bullseye # Install ALL system dependencies for OpenCV RUN apt-get update && apt-get install -y \ libglib2.0-0 \ libsm6 \ libxext6 \ libxrender1 \ libgomp1 \ libgl1-mesa-glx \ libgl1-mesa-dri \ libglu1-mesa \ libgtk-3-0 \ libavcodec58 \ libavformat58 \ libswscale5 \ libv4l-0 \ libxvidcore4 \ libx264-160 \ libjpeg62-turbo \ libpng16-16 \ libtiff5 \ libatlas-base-dev \ gfortran \ mesa-utils \ xvfb \ && rm -rf /var/lib/apt/lists/* # Set working directory WORKDIR /app # Create writable directories for Hugging Face cache RUN mkdir -p /app/.cache /app/.config && \ chmod 777 /app/.cache /app/.config # Set environment variables for Hugging Face ENV HF_HOME=/app/.cache ENV TRANSFORMERS_CACHE=/app/.cache ENV YOLO_CONFIG_DIR=/app/.config # Copy requirements and install Python packages COPY requirements.txt . # Force remove any existing OpenCV packages and install only headless RUN pip install --no-cache-dir --upgrade pip && \ pip uninstall -y opencv-python opencv-contrib-python opencv-python-headless || true && \ pip install --no-cache-dir opencv-python-headless==4.8.1.78 # Install other dependencies RUN pip install --no-cache-dir -r requirements.txt # Copy project files COPY . . # Expose Hugging Face Space port EXPOSE 7860 # Start FastAPI server CMD ["uvicorn", "inference_server:app", "--host", "0.0.0.0", "--port", "7860"]