Spaces:
Sleeping

File size: 2,586 Bytes
b83bd3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
576a898
b83bd3c
 
 
 
576a898
 
b83bd3c
576a898
 
 
b83bd3c
576a898
b83bd3c
576a898
b83bd3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
576a898
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
# ── Stage 1: base ─────────────────────────────────────────────────────────────
FROM python:3.11-slim AS base

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1

WORKDIR /app

# System deps for Pillow + diffusers
RUN apt-get update && apt-get install -y --no-install-recommends \
        libgl1 \
        libglib2.0-0 \
        libgomp1 \
        curl \
    && rm -rf /var/lib/apt/lists/*

# ── Stage 2: dependencies ──────────────────────────────────────────────────────
FROM base AS deps

COPY requirements.txt .

# PyTorch CPU-only wheel first (avoids pulling the massive CUDA build)
RUN pip install torch torchvision \
        --index-url https://download.pytorch.org/whl/cpu \
    && pip install -r requirements.txt

# ── Stage 3: final image ───────────────────────────────────────────────────────
FROM base AS final

# Copy installed packages from deps stage
COPY --from=deps /usr/local/lib/python3.11 /usr/local/lib/python3.11
COPY --from=deps /usr/local/bin /usr/local/bin

# Copy application source
COPY . .

# Create runtime directories
RUN mkdir -p outputs model_cache

# HF Spaces runs containers as UID 1000 β€” match it
RUN useradd -m -u 1000 appuser \
    && chown -R appuser:appuser /app
USER appuser

# ── Port 7860 is REQUIRED by Hugging Face Spaces ──────────────────────────────
EXPOSE 7860

# Health check β€” longer start_period for model download on cold start
HEALTHCHECK --interval=30s --timeout=10s --start-period=120s --retries=5 \
    CMD curl -f http://localhost:7860/health || exit 1

# Default env (override via HF Spaces Secrets or docker-compose)
ENV HOST=0.0.0.0 \
    PORT=7860 \
    MODEL_ID=nota-ai/bk-sdm-small \
    MODEL_CACHE_DIR=/app/model_cache \
    AUTO_LOAD_MODEL=true \
    CPU_THREADS=0 \
    ENABLE_ATTENTION_SLICING=true \
    DEFAULT_STEPS=20 \
    MAX_STEPS=50 \
    DEFAULT_WIDTH=512 \
    DEFAULT_HEIGHT=512 \
    MAX_WIDTH=768 \
    MAX_HEIGHT=768 \
    DEFAULT_GUIDANCE_SCALE=7.5 \
    OUTPUTS_DIR=/app/outputs \
    JOB_TTL_SECONDS=0

CMD ["python", "-m", "uvicorn", "main:app", \
     "--host", "0.0.0.0", "--port", "7860", "--workers", "1"]