Files
stemengine_old/backend/routers/pipeline.py
T
2026-03-03 14:52:19 +09:00

54 lines
1.7 KiB
Python
Executable File

"""Full pipeline endpoint: separate → transcribe → ABC."""
import shutil
from fastapi import APIRouter, File, Form, UploadFile, HTTPException
from .. import job_manager as jm
from ..config import (
UPLOAD_DIR, ALL_MODEL_IDS, OUTPUT_FORMATS, TRANSCRIBE_MODES,
DEFAULT_MODEL, DEFAULT_OUTPUT_FORMAT, DEFAULT_TRANSCRIBE_MODE,
)
from ..services.pipeline_service import run_pipeline
router = APIRouter(prefix="/api/pipeline", tags=["pipeline"])
VALID_FORMATS = {f["id"] for f in OUTPUT_FORMATS}
VALID_MODES = {m["id"] for m in TRANSCRIBE_MODES}
@router.post("")
async def pipeline(
file: UploadFile = File(...),
sep_model: str = Form(DEFAULT_MODEL),
sep_format: str = Form(DEFAULT_OUTPUT_FORMAT),
transcribe_mode: str = Form(DEFAULT_TRANSCRIBE_MODE),
song_title: str = Form(""),
):
if sep_model not in ALL_MODEL_IDS:
raise HTTPException(status_code=400, detail=f"Unknown model: {sep_model}")
if sep_format not in VALID_FORMATS:
raise HTTPException(status_code=400, detail=f"Unknown format: {sep_format}")
if transcribe_mode not in VALID_MODES:
raise HTTPException(status_code=400, detail=f"Unknown mode: {transcribe_mode}")
# Fall back to filename as song title
effective_title = song_title.strip() or file.filename.rsplit(".", 1)[0]
job_id = jm.create_job("pipeline")
upload_path = UPLOAD_DIR / job_id / file.filename
upload_path.parent.mkdir(parents=True, exist_ok=True)
with open(upload_path, "wb") as f:
shutil.copyfileobj(file.file, f)
jm.submit_gpu(
run_pipeline,
job_id,
str(upload_path),
sep_model,
sep_format,
transcribe_mode,
effective_title,
)
return {"job_id": job_id}