Wrapping Demucs in FastAPI: Building a Music Stem Separation Web App A two-person team built MusicSpliter, a prototype web app that wraps Meta AI's Demucs stem-separation model in a FastAPI backend and React frontend, letting users upload an MP3 and download isolated vocal and accompaniment WAV files. The backend invokes the Demucs CLI via Python's subprocess module and serves output stems by mounting Demucs' predictable output directory as FastAPI static files, avoiding custom streaming endpoints. Audio stem separation has improved dramatically over the past few years, largely driven by open-source deep learning architectures like Demucs from Meta AI. If you want to isolate clean vocals from a track or strip them away to create an instrumental backing, the results from models like htdemucs Hybrid Transformer Demucs are genuinely impressive. However, running these tools out of the box is strictly a terminal-bound workflow. You have to clone a repository, set up a Python virtual environment, configure PyTorch and CUDA drivers, and run terminal commands with custom parameter flags. For non-technical musicians, producers, or colleagues who just need an isolated vocal track for a demo, the terminal is an unnecessary barrier. At the same time, commercial online stem splitters often gate their services behind steep subscription paywalls, intrusive advertisements, or strict file size limits. As a two-person team, we built MusicSpliter as a lightweight prototype to explore how to wrap an open-source deep learning CLI tool into a straightforward web application. The concept is simple: drop an MP3 into a browser window, click a button, let the model process the audio on the backend, and download the isolated vocal and accompaniment stems as WAV files. Here is how we structured the application between FastAPI and React, how we handled the CLI process execution, and the practical challenges you run into when serving heavy machine learning workloads over standard HTTP endpoints. When building a prototype around a deep learning CLI tool, simplicity helps you validate the core experience quickly. We chose a two-tier architecture: subprocess module, and serves the generated audio files back to the client using static file mounting. Instead of writing custom audio streaming endpoints or dealing with temporary memory buffers, we leveraged Demucs' predictable directory conventions. When Demucs processes an audio track, it writes its output to a structured directory hierarchy based on the model name and track title. By mounting that output folder as a static directory in FastAPI, the frontend can download the processed stems directly via URL. Let us walk through how this is implemented step by step. In backend/main.py , we initialize FastAPI, enable Cross-Origin Resource Sharing CORS so our Vite development server can communicate with the backend, and set up our path variables. python from fastapi import FastAPI, UploadFile, File from fastapi.responses import JSONResponse from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles import subprocess import os app = FastAPI app.add middleware CORSMiddleware, allow origins= "http://localhost:5173" , allow credentials=True, allow methods= " " , allow headers= " " , BASE DIR = os.path.dirname os.path.dirname os.path.abspath file OUTPUT FOLDER = os.path.join BASE DIR, "demucs output" MODEL = "htdemucs" os.makedirs OUTPUT FOLDER, exist ok=True Serve demucs output as static files app.mount "/files", StaticFiles directory=OUTPUT FOLDER , name="files" Setting up OUTPUT FOLDER at the project root ensures all processed files live in a single predictable location. Mounting StaticFiles at /files allows any file written inside demucs output to be downloaded directly by the browser without writing dedicated file-streaming routes. Next, in backend/main.py , we define our /split endpoint. When a user uploads a track, FastAPI receives it as an UploadFile object. python @app.post "/split" async def split song file: UploadFile = File ... : Save uploaded file input path = os.path.join BASE DIR, file.filename with open input path, "wb" as f: f.write await file.read FastAPI handles multipart form uploads efficiently. By reading the file asynchronously and writing it to disk at input path , we ensure the file is fully persisted before we invoke the Demucs command-line tool. Once the MP3 is saved to disk, we trigger the separation process using Python's subprocess.run . We specify the model, the output directory, and the separation target. Run demucs try: subprocess.run "demucs", input path, "-o", OUTPUT FOLDER, "-n", MODEL, "--two-stems=vocals" , check=True except subprocess.CalledProcessError as e: return JSONResponse status code=500, content={"error": str e } song name = os.path.splitext os.path.basename input path 0 vocals url = f"/files/{MODEL}/{song name}/vocals.wav" karaoke url = f"/files/{MODEL}/{song name}/no vocals.wav" return {"vocals": vocals url, "karaoke": karaoke url} A few important details are happening here: --two-stems=vocals flag htdemucs model with vocals.wav and no vocals.wav . OUTPUT FOLDER to /files , we construct the client-accessible URLs directly using the model name and track name, returning them in a JSON payload. On the frontend, we built a React component in frontend/src/App.tsx that manages the user interaction: file selection, uploading, progress indication, and downloading the resulting files. python import React, { useState } from "react"; const API URL = "http://127.0.0.1:8000/split"; function App { const file, setFile = useState