| # Use an official Python runtime as a parent image | |
| FROM python:3.9-slim | |
| # Set the working directory in the container | |
| WORKDIR /app | |
| # Install system dependencies required by some Python packages | |
| RUN apt-get update && apt-get install -y --no-install-recommends build-essential curl | |
| # Copy the requirements file into the container | |
| COPY requirements.txt . | |
| # Install any needed packages specified in requirements.txt | |
| RUN pip install --no-cache-dir -r requirements.txt | |
| # Download the local LLM model during the build process to avoid runtime downloads and egress costs. | |
| RUN mkdir -p /app/backend/models && \ | |
| curl -L "https://gpt4all.io/models/gguf/orca-mini-3b-gguf2-q4_0.gguf" -o /app/backend/models/orca-mini-3b-gguf2-q4_0.gguf | |
| # Copy the rest of the application's code into the container | |
| COPY . . | |
| # Create directories for runtime data and set permissions for the non-root user (Hugging Face uses user 1000) | |
| RUN mkdir -p /app/papers /app/backend/jobs /app/backend/sessions && \ | |
| chown -R 1000:1000 /app/papers /app/backend/jobs /app/backend/sessions | |
| # Set the HOME directory for the non-root user to ensure correct cache path resolution | |
| ENV HOME=/app | |
| # Set Hugging Face cache directory to a writable location | |
| ENV HF_HOME /app/.cache/huggingface | |
| RUN mkdir -p ${HF_HOME} && chown -R 1000:1000 ${HF_HOME} | |
| # Create and set permissions for the GPT4All cache directory | |
| RUN mkdir -p /app/.cache/gpt4all && \ | |
| chown -R 1000:1000 /app/.cache/gpt4all | |
| # Tell the container to listen on the port provided by Cloud Run | |
| EXPOSE 7860 | |
| # Run app.py when the container launches using a production-grade server | |
| CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "1", "--threads", "8", "--timeout", "0", "backend.app:app"] |