File size: 2,360 Bytes
bb6df4e
b32fd79
 
 
bb6df4e
 
b32fd79
bb6df4e
 
 
b32fd79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: StemSplit Backend
emoji: 🎵
colorFrom: indigo
colorTo: pink
sdk: docker
app_port: 7860
pinned: false
---

# StemSplit Backend

FastAPI service that splits a song into stems (vocals / instrumental,
or vocals / drums / bass / other) using Meta's Demucs (`htdemucs`)
model. Runs as a Docker Space on Hugging Face's free CPU tier.

## How it works

1. `POST /separate` — upload an audio file (mp3/wav/m4a/flac/ogg, max
   25MB) with a `mode` field of `2stem` or `4stem`. Returns a `job_id`.
2. `GET /status/{job_id}` — poll this. Returns `queued`, `processing`
   (with a 0-100 `progress`), `done`, or `error`.
3. `GET /download/{job_id}/{stem}` — once `status` is `done`, download
   each stem as a WAV. `stem` is one of `vocals`/`instrumental` (2stem
   mode) or `vocals`/`drums`/`bass`/`other` (4stem mode).

## Why these specific design choices

- **htdemucs**: Meta's current best general-purpose open-source
  separation model, and the only one that gives you both 2-stem and
  4-stem output from a single model (no need to juggle two different
  checkpoints).
- **In-memory asyncio queue, one job at a time**: the free CPU tier
  has 2 vCPUs and no GPU. Running jobs concurrently would just slow
  every job down and risk OOM. A simple sequential queue is the most
  reliable choice at this scale.
- **Model weights baked into the Docker image** (`download_model.py`
  runs at build time): avoids any network dependency or download
  delay/failure on cold start, which is the most common cause of
  "it worked yesterday, now it's broken" reports for Demucs-on-Spaces
  deployments.
- **25MB upload limit**: keeps CPU-only inference time bounded to a
  few minutes for typical songs, instead of letting someone upload a
  40-minute DJ set that never finishes in time.
- **Ephemeral `/tmp` storage with a sweeper**: HF Spaces wipes
  container disk on restart/sleep anyway, so there's no point trying
  to persist job files. The sweeper just keeps the *running* container
  from filling up its disk during a long session.

## Cold starts

Free CPU Spaces sleep after 48 hours of inactivity. The first request
after a sleep will take longer (container boot + model load into RAM),
even though the model weights themselves don't need downloading. The
frontend should show a "waking up the server, this can take a minute"
state on first load.