Spaces:
Sleeping
Sleeping
| # ---------- Builder Stage ---------- | |
| FROM python:3.11-bookworm AS builder | |
| WORKDIR /app | |
| # Install system build dependencies | |
| RUN apt-get update && apt-get install -y \ | |
| build-essential \ | |
| pkg-config \ | |
| libhdf5-dev \ | |
| libopenblas-dev \ | |
| && apt-get clean && rm -rf /var/lib/apt/lists/* | |
| # Install Python packages | |
| COPY requirements.txt . | |
| RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \ | |
| pip install --no-cache-dir -r requirements.txt | |
| # Set Hugging Face cache environment variables | |
| ENV HF_HOME=/app/hf_cache/huggingface | |
| ENV HF_DATASETS_CACHE=/app/hf_cache/datasets | |
| # Pre-download model | |
| RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2')" | |
| # ---------- Production Stage ---------- | |
| FROM python:3.11-bookworm AS production | |
| # Create non-root user | |
| RUN groupadd -r appuser && useradd -r -g appuser appuser | |
| # Install runtime dependencies | |
| RUN apt-get update && apt-get install -y --no-install-recommends \ | |
| libgomp1 \ | |
| libopenblas0 \ | |
| libhdf5-103 \ | |
| curl \ | |
| && apt-get clean && rm -rf /var/lib/apt/lists/* | |
| WORKDIR /app | |
| # Copy Python environment from builder | |
| COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages | |
| COPY --from=builder /usr/local/bin /usr/local/bin | |
| # Copy Hugging Face cache from builder | |
| COPY --from=builder /app/hf_cache /app/cache | |
| # Copy application code | |
| COPY . . | |
| # Fix ownership of everything for non-root user | |
| RUN mkdir -p \ | |
| /app/database/faiss_index \ | |
| /app/cache/datasets \ | |
| /home/appuser/.cache/huggingface \ | |
| /app/vector_store \ | |
| /app/temp_vector_store && \ | |
| chown -R appuser:appuser /app /home/appuser | |
| # Important: Make faiss_index files writable | |
| RUN chmod -R u+w /app/database/faiss_index | |
| # Switch to non-root user | |
| USER appuser | |
| # Expose app port | |
| EXPOSE 7860 | |
| # Health check | |
| HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ | |
| CMD curl -f http://localhost:7860/health || exit 1 | |
| # Environment variables | |
| ENV PYTHONPATH=/app \ | |
| PYTHONUNBUFFERED=1 \ | |
| PYTHONDONTWRITEBYTECODE=1 \ | |
| HF_HOME=/app/cache/huggingface \ | |
| HF_DATASETS_CACHE=/app/cache/datasets | |
| # Launch FastAPI app | |
| CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"] | |