File size: 1,243 Bytes
801ea6e
 
2704b98
8a0a2e0
2704b98
cf8da63
 
 
 
 
2704b98
cf8da63
ed0367a
25946bb
801ea6e
 
 
 
7c3e785
30638e1
 
 
 
7ce8d71
 
 
 
87aca49
 
 
 
801ea6e
8a0a2e0
2704b98
801ea6e
8a0a2e0
 
cf8da63
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# ๐Ÿ—๏ธ 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"]