File size: 1,100 Bytes
a8d742c
1e5154c
a8d742c
 
 
 
1e5154c
a8d742c
1e5154c
 
a8d742c
1e5154c
 
 
 
a8d742c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"
    }