Spaces:
Sleeping
Sleeping
| # ποΈ Stage 1: Build dependencies in a lightweight Python image | |
| FROM python:3.9-slim AS builder | |
| WORKDIR /app | |
| # Install dependencies in a single step to optimize layers | |
| COPY requirements.txt . | |
| RUN pip install --no-cache-dir -r requirements.txt \ | |
| && pip install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu \ | |
| && pip install --no-cache-dir uvicorn[standard] transformers | |
| # Verify installation | |
| RUN python -c "import torch; print(torch.__version__); print(torch.cuda.is_available())" | |
| # π Stage 2: Final optimized production image | |
| FROM python:3.9-slim | |
| WORKDIR /app | |
| # Set up a persistent cache for Hugging Face models | |
| RUN mkdir -p /app/cache && chmod -R 777 /app/cache | |
| ENV HF_HOME=/app/cache | |
| # Copy dependencies from builder stage | |
| COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages | |
| COPY --from=builder /usr/local/bin /usr/local/bin | |
| # Create non-root user for security | |
| RUN useradd --create-home appuser | |
| USER appuser | |
| # Copy application code | |
| COPY . . | |
| # Expose port | |
| EXPOSE 7860 | |
| # Start FastAPI with optimized settings | |
| CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1", "--timeout-keep-alive", "10"] |