from fastapi import FastAPI, UploadFile, File from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles import subprocess import os import shutil app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) os.makedirs("separated", exist_ok=True) app.mount("/stems", StaticFiles(directory="separated"), name="stems") @app.post("/process-audio") async def process_audio(file: UploadFile = File(...)): temppa = f"temp_{file.filename}" with open(temppa, "wb") as buffer: shutil.copyfileobj(file.file, buffer) subprocess.run(["demucs", "-n", "htdemucs_6s", temppa]) foldname = temppa.rsplit('.', 1)[0] url = f"/stems/htdemucs_6s/{foldname}" os.remove(temppa) return { "vocals": f"{url}/vocals.wav", "drums": f"{url}/drums.wav", "bass": f"{url}/bass.wav", "other": f"{url}/other.wav", "guitar": f"{url}/guitar.wav", "piano": f"{url}/piano.wav" }