Files changed (4) hide show
  1. .dockerignore +37 -0
  2. Dockerfile +73 -0
  3. README.md +1 -10
  4. docker-compose.yml +59 -0
.dockerignore ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python cache
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.pyo
5
+ .pytest_cache/
6
+
7
+ # Virtual envs
8
+ .venv/
9
+ venv/
10
+ env/
11
+
12
+ # Generated outputs (mounted as volume at runtime)
13
+ outputs/
14
+
15
+ # Model cache (mounted as volume at runtime)
16
+ model_cache/
17
+
18
+ # Dev / IDE
19
+ .env
20
+ .env.*
21
+ !.env.example
22
+ .idea/
23
+ .vscode/
24
+ *.code-workspace
25
+
26
+ # Git
27
+ .git/
28
+ .gitignore
29
+
30
+ # Docs / assets
31
+ *.md
32
+ *.zip
33
+ assets/
34
+
35
+ # OS
36
+ .DS_Store
37
+ Thumbs.db
Dockerfile ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ── Stage 1: base ─────────────────────────────────────────────────────────────
2
+ FROM python:3.11-slim AS base
3
+
4
+ ENV PYTHONDONTWRITEBYTECODE=1 \
5
+ PYTHONUNBUFFERED=1 \
6
+ PIP_NO_CACHE_DIR=1 \
7
+ PIP_DISABLE_PIP_VERSION_CHECK=1
8
+
9
+ WORKDIR /app
10
+
11
+ # System deps for Pillow + diffusers
12
+ RUN apt-get update && apt-get install -y --no-install-recommends \
13
+ libgl1 \
14
+ libglib2.0-0 \
15
+ libgomp1 \
16
+ curl \
17
+ && rm -rf /var/lib/apt/lists/*
18
+
19
+ # ── Stage 2: dependencies ──────────────────────────────────────────────────────
20
+ FROM base AS deps
21
+
22
+ COPY requirements.txt .
23
+
24
+ # PyTorch CPU-only wheel first (avoids pulling the massive CUDA build)
25
+ RUN pip install torch torchvision \
26
+ --index-url https://download.pytorch.org/whl/cpu \
27
+ && pip install -r requirements.txt
28
+
29
+ # ── Stage 3: final image ───────────────────────────────────────────────────────
30
+ FROM base AS final
31
+
32
+ # Copy installed packages from deps stage
33
+ COPY --from=deps /usr/local/lib/python3.11 /usr/local/lib/python3.11
34
+ COPY --from=deps /usr/local/bin /usr/local/bin
35
+
36
+ # Copy application source
37
+ COPY . .
38
+
39
+ # Create runtime directories
40
+ RUN mkdir -p outputs model_cache
41
+
42
+ # Non-root user for security
43
+ RUN useradd -m -u 1000 appuser \
44
+ && chown -R appuser:appuser /app
45
+ USER appuser
46
+
47
+ # Expose API port
48
+ EXPOSE 8000
49
+
50
+ # Health check — hits the /health endpoint every 30 s
51
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
52
+ CMD curl -f http://localhost:8000/health || exit 1
53
+
54
+ # Default env (override with docker run -e or docker-compose)
55
+ ENV HOST=0.0.0.0 \
56
+ PORT=8000 \
57
+ MODEL_ID=nota-ai/bk-sdm-small \
58
+ MODEL_CACHE_DIR=/app/model_cache \
59
+ AUTO_LOAD_MODEL=true \
60
+ CPU_THREADS=0 \
61
+ ENABLE_ATTENTION_SLICING=true \
62
+ DEFAULT_STEPS=20 \
63
+ MAX_STEPS=50 \
64
+ DEFAULT_WIDTH=512 \
65
+ DEFAULT_HEIGHT=512 \
66
+ MAX_WIDTH=768 \
67
+ MAX_HEIGHT=768 \
68
+ DEFAULT_GUIDANCE_SCALE=7.5 \
69
+ OUTPUTS_DIR=/app/outputs \
70
+ JOB_TTL_SECONDS=0
71
+
72
+ CMD ["python", "-m", "uvicorn", "main:app", \
73
+ "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]
README.md CHANGED
@@ -1,12 +1,3 @@
1
- ---
2
- license: mit
3
- title: hf-image-api
4
- sdk: static
5
- emoji: 💻
6
- colorFrom: yellow
7
- colorTo: yellow
8
- pinned: true
9
- ---
10
  # HF CPU Image Generation API
11
 
12
  A production-ready REST API for **Stable Diffusion image generation on CPU** using Hugging Face Diffusers. Built on top of the architecture patterns from [Local AI Image Generator](https://github.com/techjarves/Local-AI-Image-Generator) and redesigned as a stateless, job-queue-based HTTP API.
@@ -349,4 +340,4 @@ hf-image-api/
349
 
350
  ## License
351
 
352
- MIT — free to use and modify.
 
 
 
 
 
 
 
 
 
 
1
  # HF CPU Image Generation API
2
 
3
  A production-ready REST API for **Stable Diffusion image generation on CPU** using Hugging Face Diffusers. Built on top of the architecture patterns from [Local AI Image Generator](https://github.com/techjarves/Local-AI-Image-Generator) and redesigned as a stateless, job-queue-based HTTP API.
 
340
 
341
  ## License
342
 
343
+ MIT — free to use and modify.
docker-compose.yml ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ services:
2
+ hf-image-api:
3
+ build:
4
+ context: .
5
+ dockerfile: Dockerfile
6
+ target: final
7
+
8
+ image: hf-image-api:latest
9
+ container_name: hf-image-api
10
+
11
+ ports:
12
+ - "8000:8000"
13
+
14
+ # ── Persistent volumes ─────────────────────────────────────────────────────
15
+ volumes:
16
+ # Model weights persist between container restarts (avoids re-downloading)
17
+ - model_cache:/app/model_cache
18
+ # Generated images persist and are accessible on the host
19
+ - ./outputs:/app/outputs
20
+
21
+ # ── Environment (edit here or in .env) ────────────────────────────────────
22
+ environment:
23
+ MODEL_ID: ${MODEL_ID:-nota-ai/bk-sdm-small}
24
+ AUTO_LOAD_MODEL: ${AUTO_LOAD_MODEL:-true}
25
+ CPU_THREADS: ${CPU_THREADS:-0}
26
+ ENABLE_ATTENTION_SLICING: ${ENABLE_ATTENTION_SLICING:-true}
27
+ DEFAULT_STEPS: ${DEFAULT_STEPS:-20}
28
+ MAX_STEPS: ${MAX_STEPS:-50}
29
+ DEFAULT_WIDTH: ${DEFAULT_WIDTH:-512}
30
+ DEFAULT_HEIGHT: ${DEFAULT_HEIGHT:-512}
31
+ MAX_WIDTH: ${MAX_WIDTH:-768}
32
+ MAX_HEIGHT: ${MAX_HEIGHT:-768}
33
+ DEFAULT_GUIDANCE_SCALE: ${DEFAULT_GUIDANCE_SCALE:-7.5}
34
+ OUTPUTS_DIR: /app/outputs
35
+ MODEL_CACHE_DIR: /app/model_cache
36
+
37
+ # ── CPU resource limits ────────────────────────────────────────────────────
38
+ # Remove or adjust based on your machine
39
+ deploy:
40
+ resources:
41
+ limits:
42
+ cpus: "4.0" # cap at 4 cores (set to your core count)
43
+ memory: 8G # 8 GB for bk-sdm-small; use 12 GB for SD v1.5
44
+ reservations:
45
+ memory: 2G
46
+
47
+ restart: unless-stopped
48
+
49
+ healthcheck:
50
+ test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
51
+ interval: 30s
52
+ timeout: 10s
53
+ retries: 3
54
+ start_period: 90s # give extra time for model download on first run
55
+
56
+ # ── Named volumes ──────────────────────────────────────────────────────────────
57
+ volumes:
58
+ model_cache:
59
+ driver: local