# ============================================================================= # 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/ ./ # Repo-root VERSION isn't on vite.config.js's relative path inside this # flattened build stage; pass it through FRAGMENTA_VERSION so the real # version is baked into the bundle (vite.config.js reads the env first). COPY VERSION /build/VERSION RUN FRAGMENTA_VERSION="$(cat /build/VERSION)" npm run build # --------------------------------------------------------------------------- # Stage 2: Python backend (CPU + GPU via bundled CUDA in PyTorch wheels) # --------------------------------------------------------------------------- FROM python:3.14-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 — copied owned by uid 1000 (the HF Spaces "user") so no # post-hoc `chown -R /app` is needed; a recursive chown after large COPYs # duplicates every touched file into a new image layer. # --------------------------------------------------------------------------- COPY --chown=1000:1000 . . COPY --chown=1000:1000 --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/ # Writable directories. No weights are baked into the image: the runtime is # SA3-only and checkpoints are fetched on demand through the in-app Checkpoint # Manager into models/pretrained/sa3/. The old build-time snapshot_download # pulled all of MazCodes/fragmenta-models (~13.7 GB of pre-SA3 # stable-audio-open weights the app no longer loads) and bloated the Space # image for nothing. RUN mkdir -p /app/models/pretrained/sa3 \ /app/models/fine_tuned \ /app/models/config \ /app/data \ /app/output \ /app/logs \ /app/config \ /app/uploads \ /app/projects # 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 # App code is already uid-1000-owned via COPY --chown; only the dirs created # by RUN mkdir (root-owned, empty) and the defaults stash need fixing up. RUN chown -R 1000:1000 /app/models /app/data /app/output /app/logs /app/config \ /app/uploads /app/projects /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 # Marks this build as the public HF Spaces demo. The frontend reads it back # through GET /api/environment and switches on demo-only affordances (the # demo notice, no "don't show again", HF sign-in disabled). Never set on # desktop installs or the regular Docker images. ENV FRAGMENTA_HF_SPACE=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"]