# Use official Python 3.11 slim image FROM python:3.11-slim # Set environment variables ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ PORT=7860 \ HOME=/home/user \ USE_OLLAMA=false \ LLAMA_THREADS=2 # Install system dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ python3-dev \ tesseract-ocr \ curl \ git \ && apt-get clean && rm -rf /var/lib/apt/lists/* # Set up user 1000 for Hugging Face Spaces compatibility RUN useradd -m -u 1000 user WORKDIR $HOME/app # Pre-create required directories and assign ownership RUN mkdir -p models vector_store documents && chown -R user:user models vector_store documents # Copy requirements and install COPY requirements.txt . RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir -r requirements.txt # Download Gemma 3 1B Instruct GGUF model during build RUN curl -L -o models/google_gemma-3-1b-it-Q4_K_M.gguf \ "https://huggingface.co/bartowski/google_gemma-3-1b-it-GGUF/resolve/main/google_gemma-3-1b-it-Q4_K_M.gguf" # Copy the rest of the application files COPY --chown=user:user . . # Switch to the non-root user USER user # Pre-download/cache the embedding model under the user's home cache RUN python -c "from langchain_huggingface import HuggingFaceEmbeddings; HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')" # Expose Hugging Face Space port EXPOSE 7860 # Start application using uvicorn on port 7860 CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]