65 lines
2.1 KiB
Python
Executable File
65 lines
2.1 KiB
Python
Executable File
"""ABC notation conversion endpoint."""
|
|
import shutil
|
|
from pathlib import Path
|
|
from typing import List
|
|
|
|
from fastapi import APIRouter, File, Form, UploadFile, HTTPException
|
|
|
|
from .. import job_manager as jm
|
|
from ..config import UPLOAD_DIR
|
|
from ..services.abc_service import run_abc_conversion
|
|
|
|
router = APIRouter(prefix="/api/abc-convert", tags=["abc-convert"])
|
|
|
|
|
|
@router.post("")
|
|
async def abc_convert(
|
|
files: List[UploadFile] = File(...),
|
|
song_title: str = Form("Untitled"),
|
|
):
|
|
if not files:
|
|
raise HTTPException(status_code=400, detail="At least one MIDI file required")
|
|
|
|
job_id = jm.create_job("abc-convert")
|
|
upload_dir = UPLOAD_DIR / job_id
|
|
upload_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
midi_paths = []
|
|
for upload in files:
|
|
dest = upload_dir / upload.filename
|
|
with open(dest, "wb") as f:
|
|
shutil.copyfileobj(upload.file, f)
|
|
midi_paths.append(str(dest))
|
|
|
|
jm.submit(run_abc_conversion, job_id, midi_paths, song_title)
|
|
return {"job_id": job_id}
|
|
|
|
|
|
@router.post("/from-job")
|
|
async def abc_convert_from_job(
|
|
source_job_id: str = Form(...),
|
|
song_title: str = Form(""),
|
|
):
|
|
"""
|
|
Convert MIDI files from a completed transcription (or pipeline) job to ABC notation.
|
|
Reuses the already-generated MIDI files on disk — no re-upload needed.
|
|
"""
|
|
source_job = jm.get_job(source_job_id)
|
|
if source_job is None:
|
|
raise HTTPException(status_code=404, detail=f"Source job not found: {source_job_id}")
|
|
if source_job["status"] != "done":
|
|
raise HTTPException(status_code=400, detail="Source job has not completed successfully")
|
|
|
|
midi_paths = [
|
|
f["path"] for f in source_job.get("files", [])
|
|
if Path(f.get("path", "")).suffix.lower() == ".mid"
|
|
and Path(f.get("path", "")).exists()
|
|
]
|
|
if not midi_paths:
|
|
raise HTTPException(status_code=404, detail="No accessible MIDI files found in source job")
|
|
|
|
title = song_title.strip() or source_job_id[:8]
|
|
job_id = jm.create_job("abc-convert")
|
|
jm.submit(run_abc_conversion, job_id, midi_paths, title)
|
|
return {"job_id": job_id}
|