File size: 5,146 Bytes
63f0b06
 
 
 
 
 
 
 
 
 
 
 
219a8a4
 
 
 
 
 
 
 
 
 
 
 
 
 
63f0b06
 
 
 
 
 
 
 
 
 
 
 
219a8a4
63f0b06
 
 
 
 
 
 
 
219a8a4
 
 
 
 
14269fb
509c3c0
63f0b06
 
 
219a8a4
63f0b06
 
219a8a4
63f0b06
9ea28c1
 
 
63f0b06
 
 
 
 
 
 
 
 
 
fc390e1
 
 
 
 
 
 
 
 
531a74b
63f0b06
 
 
 
 
 
 
 
 
c7986a9
63f0b06
 
 
 
 
 
 
 
 
 
 
 
 
9ea28c1
531a74b
63f0b06
 
0573fbf
63f0b06
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# =============================================================================
# Fragmenta β€” Hugging Face Spaces Dockerfile (CPU + GPU)
# =============================================================================
# Deploy as a Docker Space on Hugging Face.
# Works on both free CPU Spaces and paid GPU Spaces.
#
# CPU Spaces: torch.cuda.is_available() β†’ False β†’ runs on CPU
# GPU Spaces: NVIDIA runtime injected by HF β†’ torch.cuda.is_available() β†’ True
#
# Port: 7860 (HF Spaces requirement)
# =============================================================================

# ---------------------------------------------------------------------------
# Stage 1: Build the React frontend
# ---------------------------------------------------------------------------
FROM node:20-slim AS frontend-builder

WORKDIR /build/frontend
COPY app/frontend/package.json app/frontend/package-lock.json* ./
RUN npm ci --prefer-offline --no-audit 2>/dev/null || npm install
COPY app/frontend/ ./
RUN npm run build

# ---------------------------------------------------------------------------
# Stage 2: Python backend (CPU + GPU via bundled CUDA in PyTorch wheels)
# ---------------------------------------------------------------------------
FROM python:3.11-slim-bookworm

ENV DEBIAN_FRONTEND=noninteractive

# System deps (audio processing, networking)
RUN apt-get update && apt-get install -y --no-install-recommends \
        libsndfile1 \
        ffmpeg \
        git \
        curl \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

RUN pip install --no-cache-dir --root-user-action=ignore --upgrade pip setuptools wheel

WORKDIR /app

# ---------------------------------------------------------------------------
# Python dependencies
# ---------------------------------------------------------------------------
COPY requirements.txt .

# Install all requirements in one pass. Strip:
#   - PyQt6 / pyqt6-webengine: native desktop UI, not used in the web container.
#   - flash-attn: optional CUDA-only kernel, no wheel for this base image.
#   - extra-index-url: not needed, default PyPI ships the right wheels.
#   - pycairo / pygobject: require libcairo + C compiler absent from slim image.
RUN grep -ivE 'flash-attn|extra-index-url|pycairo|pygobject|pywebview' requirements.txt > requirements_docker.txt \
    && pip install --no-cache-dir --root-user-action=ignore -r requirements_docker.txt \
    && rm requirements_docker.txt

# ---------------------------------------------------------------------------
# Application code
# ---------------------------------------------------------------------------
COPY . .
COPY --from=frontend-builder /build/frontend/build ./app/frontend/build

# Install vendored Stable Audio 3 in-tree (--no-deps: runtime deps come from
# requirements.txt). Makes `import stable_audio_3` resolve.
RUN pip install --no-cache-dir --root-user-action=ignore --no-deps -e ./vendor/stable-audio-3/

# Create writable directories
RUN mkdir -p /app/models/pretrained \
             /app/models/fine_tuned \
             /app/models/config \
             /app/data \
             /app/output \
             /app/logs \
             /app/config

# Fetch pretrained weights from MazCodes/fragmenta-models.
# Uses huggingface_hub (already a dep) instead of git clone so we don't need
# git-lfs and so failures are loud rather than silent LFS-pointer stubs.
RUN python -c "from huggingface_hub import snapshot_download; snapshot_download(repo_id='MazCodes/fragmenta-models', local_dir='/app/models/pretrained', local_dir_use_symlinks=False)"

# Fail the build if either weight file is suspiciously small (e.g. an LFS
# pointer or a partial download). Real files are >1 GB.
RUN test "$(stat -c%s /app/models/pretrained/stable-audio-open-small-model.safetensors)" -gt 1000000000 \
 && test "$(stat -c%s /app/models/pretrained/stable-audio-open-model.safetensors)" -gt 1000000000

# Save default model configs (restored at runtime if dirs are empty)
RUN mkdir -p /opt/fragmenta-defaults/models/config \
    && if [ -d /app/models/config ] && ls /app/models/config/*.json 1>/dev/null 2>&1; then \
         cp /app/models/config/*.json /opt/fragmenta-defaults/models/config/; \
       fi

# ---------------------------------------------------------------------------
# HF Spaces runs as user "user" (uid 1000) β€” ensure write permissions
# ---------------------------------------------------------------------------
COPY docker/docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh

RUN useradd -m -u 1000 user || true
RUN chown -R 1000:1000 /app /opt/fragmenta-defaults
USER user

# ---------------------------------------------------------------------------
# Configuration β€” HF Spaces requires port 7860
# ---------------------------------------------------------------------------
ENV FLASK_HOST=0.0.0.0
ENV FLASK_PORT=7860
ENV FRAGMENTA_LOG_LEVEL=INFO
ENV FRAGMENTA_DOCKER=1
ENV PYTHONPATH=/app/vendor/stable-audio-3
ENV FRAGMENTA_USE_CUSTOM_MODELS=true
ENV HOME=/home/user
ENV PATH="/home/user/.local/bin:${PATH}"
ENV OMP_NUM_THREADS=4

EXPOSE 7860

ENTRYPOINT ["/docker-entrypoint.sh"]