# Use an official lightweight Python base image FROM python:3.9-slim # Set system environment variables ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ HF_HOME=/code/model_cache \ PORT=7860 # Set working directory WORKDIR /code # Copy dependencies first to leverage Docker layer caching COPY ./requirements.txt /code/requirements.txt # Install python dependencies RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt # Create cache folder and make it accessible RUN mkdir -p /code/model_cache # Pre-download the embedding model at build time to prevent cold-start latency RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2')" # Set up a non-root user (Hugging Face Spaces requirement) RUN useradd -m -u 1000 user # Change ownership of the model cache directory to the non-root user RUN chown -R user:user /code/model_cache USER user # Set home directory for user ENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH # Copy application files, changing ownership to the non-root user COPY --chown=user:user . /code # Run the FastAPI server on port 7860 (Hugging Face Spaces default port) CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]