FROM python:3.10-slim # HF Spaces requires a non-root user with uid 1000 RUN useradd -m -u 1000 user WORKDIR /app # Install CPU-only PyTorch before requirements.txt so sentence-transformers # doesn't pull the full CUDA build (~2 GB) as a transitive dependency. RUN pip install --no-cache-dir \ torch \ --index-url https://download.pytorch.org/whl/cpu # Copy and install remaining dependencies as a separate layer so source-only # changes don't invalidate the (slow) pip install cache. COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Pre-download the embedding model into the image so first-request latency # on the Space is not penalised by a ~90 MB download at runtime. RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2')" # Copy source last — this layer changes most often COPY --chown=user:user . . USER user ENV PYTHONUNBUFFERED=1 \ # Bind to all interfaces so HF Spaces proxy can reach the server GRADIO_SERVER_NAME=0.0.0.0 \ GRADIO_SERVER_PORT=7860 \ # Keep HuggingFace model cache inside the container's home HF_HOME=/home/user/.cache/huggingface EXPOSE 7860 CMD ["python", "app_gradio.py"]