File size: 1,565 Bytes
0f623ae
 
 
 
ebe2347
0f623ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebe2347
0f623ae
 
 
 
 
 
 
ebe2347
0f623ae
 
ebe2347
0f623ae
 
 
ebe2347
 
 
 
 
 
 
 
 
0f623ae
 
 
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
import os
import shutil
import uuid

from fastapi import FastAPI, File, Form, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles

from backend.demucs_runner import run_demucs

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

UPLOAD_DIR = "backend/uploads"


@app.post("/upload")
async def upload_audio(file: UploadFile = File(...), atten_lim_db: int = Form(None)):
    ext = os.path.splitext(file.filename)[1]
    unique_name = f"{uuid.uuid4()}{ext}"
    save_path = os.path.join(UPLOAD_DIR, unique_name)

    with open(save_path, "wb") as buffer:
        shutil.copyfileobj(file.file, buffer)

    output_path, file_type = run_demucs(save_path, atten_lim_db=atten_lim_db)
    track_name = os.path.splitext(unique_name)[0]

    return {"download_url": f"/download/{track_name}", "file_type": file_type, "status": "done"}


@app.get("/download/{track_name}")
def download_file(track_name: str):
    for ext, media_type, label in [
        ("_enhanced.mp4", "video/mp4", "enhanced.mp4"),
        ("_enhanced.wav", "audio/wav", "enhanced.wav"),
    ]:
        file_path = f"separated/{track_name}{ext}"
        if os.path.exists(file_path):
            return FileResponse(file_path, media_type=media_type, filename=label)
    raise HTTPException(status_code=404, detail="File not found")


app.mount("/", StaticFiles(directory="frontend", html=True), name="frontend")