Spaces:
Sleeping
Sleeping
| 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" | |
| 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"} | |
| 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") | |