# Use official Python base image FROM python:3.9-slim # Install system dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ libgl1 \ libglib2.0-0 \ libsm6 \ libxext6 \ libxrender-dev \ && rm -rf /var/lib/apt/lists/* # Set up a new user named "user" with user ID 1000 RUN useradd -m -u 1000 user # Switch to the "user" user USER user # Set TensorFlow environment variables ENV TF_CPP_MIN_LOG_LEVEL=2 \ TF_ENABLE_ONEDNN_OPTS=0 \ CUDA_VISIBLE_DEVICES=-1 \ XLA_FLAGS=--xla_gpu_cuda_data_dir=/usr/local/cuda \ TF_FORCE_GPU_ALLOW_GROWTH=true \ PYTHONWARNINGS="ignore" \ PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python \ TF_KERAS_BACKEND=tensorflow # Set home to the user's home directory ENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH # Set the working directory to the user's home directory WORKDIR $HOME/app # Create directory for saved models RUN mkdir -p $HOME/app/saved_models # Copy requirements first - with proper ownership COPY --chown=user requirements.txt . # Install Python dependencies RUN pip install --no-cache-dir -r requirements.txt # Copy all files with proper ownership COPY --chown=user . . # Expose the port Hugging Face Spaces uses EXPOSE 7860 # Start Gunicorn server with optimized settings CMD ["gunicorn", \ "--bind", "0.0.0.0:7860", \ "--timeout", "300", \ "--workers", "1", \ "--threads", "4", \ "--worker-class", "gthread", \ "--preload", \ "app:app"]