File size: 1,882 Bytes
15373e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c8106d
15373e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d59a4a
 
 
15373e0
 
 
 
 
 
 
 
 
 
 
b89831e
15373e0
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Build Stage
FROM python:3.10-slim AS builder

WORKDIR /app

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

# Install build dependencies
RUN apt-get update && apt-get install -y \
    gcc \
    python3-dev \
    curl \
    git \
    && rm -rf /var/lib/apt/lists/*

# Install uv
RUN pip install --no-cache-dir uv

COPY requirements.backend.txt .

# Create virtual environment and install dependencies
ENV UV_HTTP_TIMEOUT=300
RUN uv venv .venv && \
    uv pip install --no-cache -r requirements.backend.txt --extra-index-url https://download.pytorch.org/whl/cpu

# --- Runtime Stage ---
FROM python:3.10-slim

WORKDIR /app

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PATH="/app/.venv/bin:$PATH"
ENV PYTHONPATH="/app/src:$PYTHONPATH"
ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8

# Install runtime dependencies (curl for healthcheck)
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*

# Copy virtual environment from builder
COPY --from=builder /app/.venv /app/.venv

# Copy Scripts
COPY scripts/ ./scripts/

# Data & Model Baking
ENV HF_HOME=/app/data/model_cache

# Download Model (Bake into image)
RUN /app/.venv/bin/python scripts/download_model.py

# Download Data (Bake into image)
RUN /app/.venv/bin/python scripts/download_data.py

# Copy Code
COPY src/ ./src/

# Precompute Clusters (Bake into image)
RUN /app/.venv/bin/python scripts/precompute_clusters.py

# Create directories and permissions
RUN mkdir -p data/processed data/feedback logs
#    addgroup --system app && adduser --system --group app && \
#    chown -R app:app /app

# USER app

# Expose port
EXPOSE 7860

# Healthcheck
HEALTHCHECK --interval=30s --timeout=30s --start-period=30s --retries=3 \
  CMD curl -f http://localhost:7860/health || exit 1

# Run Command
CMD ["/app/.venv/bin/uvicorn", "src.book_recommender.api.main:app", "--host", "0.0.0.0", "--port", "7860"]