File size: 3,560 Bytes
5ff4fb2
 
 
9ce51a5
 
 
 
 
 
 
 
 
5ff4fb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ce51a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ff4fb2
 
 
9ce51a5
5ff4fb2
 
 
9ce51a5
 
 
fa32687
5ff4fb2
 
 
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
# ── Base image ────────────────────────────────────────────────────────────────
FROM python:3.11-slim

# ── Environment variables ─────────────────────────────────────────────────────
# HF_HOME / TRANSFORMERS_CACHE must point to /tmp so they are writable
# under any user (HuggingFace Spaces restricts writes outside /tmp and /home).
ENV HF_HOME=/tmp/hf_cache \
    TRANSFORMERS_CACHE=/tmp/hf_cache \
    HF_DATASETS_CACHE=/tmp/hf_cache/datasets \
    PYTHONUNBUFFERED=1 \
    PORT=7860

# ── System dependencies ───────────────────────────────────────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        git \
    && rm -rf /var/lib/apt/lists/*

# ── Working directory ─────────────────────────────────────────────────────────
WORKDIR /app

# ── Install Python dependencies ───────────────────────────────────────────────
COPY requirements.txt setup.py README.md ./
COPY src/ ./src/

RUN pip install --no-cache-dir --upgrade pip \
 && pip install --no-cache-dir -r requirements.txt

# ── Copy application code ────────────────────────────────────────────────────
COPY app.py main.py params.yaml ./
COPY config/ ./config/

# ── Create writable runtime directories ──────────────────────────────────────
# Create artifact dirs and the HF cache dir here (as root) so they already
# exist with correct ownership before we switch to the non-root user.
RUN mkdir -p \
        artifacts/model_trainer \
        artifacts/model_evaluation \
        artifacts/data_ingestion \
        artifacts/data_transformation \
        artifacts/data_validation \
        /tmp/hf_cache \
    && useradd -m -u 1000 appuser \
    && chown -R appuser:appuser /app /tmp/hf_cache

USER appuser

# ── Port ──────────────────────────────────────────────────────────────────────
# HuggingFace Spaces requires exactly 7860.
EXPOSE 7860

# ── Health check ──────────────────────────────────────────────────────────────
# Returns 200 once model is loaded, 503 while still warming up β€” both are
# acceptable to the orchestrator (non-5xx codes keep the container alive).
HEALTHCHECK --interval=20s --timeout=10s --start-period=120s --retries=5 \
    CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:7860/', timeout=8)"]

# ── Start server ──────────────────────────────────────────────────────────────
CMD ["python", "app.py"]