a98686898's picture
Upload 4 files
936818f verified
Raw
History Blame Contribute Delete
1.55 kB
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.responses import JSONResponse, FileResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
import shutil, os
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.mount("/", StaticFiles(directory=".", html=True), name="static")
@app.post("/process")
async def process_music(
music: UploadFile = File(...),
vocal: UploadFile = File(...),
lyrics: str = Form(...),
model: str = Form(...)
):
os.makedirs("temp", exist_ok=True)
music_path = f"temp/{music.filename}"
vocal_path = f"temp/{vocal.filename}"
with open(music_path, "wb") as f:
shutil.copyfileobj(music.file, f)
with open(vocal_path, "wb") as f:
shutil.copyfileobj(vocal.file, f)
# 模擬選擇模型結果處理
result_file = "demo/ai_sing_sample.mp3"
if os.path.exists(result_file):
shutil.copy(result_file, "temp/output_demo.mp3")
return JSONResponse({ "status": "success", "download_url": "/download/output_demo.mp3" })
return JSONResponse({ "status": "failed", "error": "No output file." })
@app.get("/download/{filename}")
async def download_file(filename: str):
path = f"temp/{filename}"
if os.path.exists(path):
return FileResponse(path, media_type="audio/mpeg", filename=filename)
return JSONResponse({ "error": "File not found" }, status_code=404)