FROM python:3.10-slim AS builder WORKDIR /app_build # Install build-time system dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ pkg-config \ libssl-dev \ curl \ && apt-get clean && rm -rf /var/lib/apt/lists/* # Create and activate a virtual environment RUN python -m venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" # Install Python dependencies COPY pyproject.toml ./ RUN pip install --no-cache-dir --upgrade pip RUN pip install --no-cache-dir speechbrain==0.5.16 # Install the rest of the project dependencies RUN pip install --no-cache-dir . # --- Final Stage --- FROM python:3.10-slim AS final ARG USER_UID=10002 ARG USER_GID=$USER_UID ARG USERNAME=modelapi # Create user and its home directory /home/modelapi RUN groupadd --gid $USER_GID $USERNAME && \ useradd --uid $USER_UID --gid $USER_GID -m $USERNAME # -m creates /home/modelapi WORKDIR /app # Install runtime system dependencies (as root) RUN apt-get update && apt-get install -y --no-install-recommends \ libsndfile1 \ ffmpeg \ && apt-get clean && rm -rf /var/lib/apt/lists/* # Copy the virtual environment from the builder stage to /app/.venv COPY --from=builder /opt/venv /app/.venv # Copy model assets and application code COPY ./model/sepformer_dns4_16k /app/model/sepformer_dns4_16k/ COPY ./app /app/app # Setup permissions for user 'modelapi' (as root) # 'modelapi' will own its home directory and the entire /app directory (incl. venv) RUN chown -R $USERNAME:$USERNAME /home/$USERNAME && \ chown -R $USERNAME:$USERNAME /app # Switch to the non-root user USER $USERNAME # Set environment variables for caches to use the user's home directory. # Also ensures Python uses the venv from /app/.venv. # These are set while context is $USERNAME. ENV PATH="/app/.venv/bin:$PATH" ENV PYTHONUNBUFFERED=1 ENV HF_HOME="/home/$USERNAME/.cache/huggingface" ENV TORCH_HOME="/home/$USERNAME/.cache/torch" # Optional: For more comprehensive cache control if issues persist with other tools # ENV XDG_CACHE_HOME="/home/$USERNAME/.cache" # ENV PIP_CACHE_DIR="/home/$USERNAME/.cache/pip" # Create the cache directories as $USERNAME to ensure correct ownership and permissions. # This also creates the parent /home/$USERNAME/.cache directory. RUN mkdir -p "$HF_HOME" "$TORCH_HOME" # If PIP_CACHE_DIR was set: # RUN mkdir -p "/home/$USERNAME/.cache/pip" # Create application-specific data directories within the user's home. # Assumes your app will use paths like /home/modelapi/.modelapi/... # If your app code is changed to use Path.home() / '.modelapi', this is belt-and-suspenders. RUN mkdir -p "/home/$USERNAME/.modelapi/noisy_audio" "/home/$USERNAME/.modelapi/enhanced_audio" # Expose the port the app runs on EXPOSE 6500 # Define the command to run the application # Uvicorn will run as $USERNAME and use the venv via PATH. CMD ["python", "-m", "uvicorn", "app.app:api_service.app", "--host", "0.0.0.0", "--port", "6500", "--log-level", "debug"] # We need to ensure there's an 'app_instance.app' available in app.app for uvicorn # This requires app/app.py to have something like: # api = ModelAPI(host="0.0.0.0", port=6500) # or get from env # app_instance = api # or however the FastAPI app object is exposed # if __name__ == "__main__": # api.run() # And the ModelAPI class needs its FastAPI app at self.app