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

750 lines
26 KiB
Python
Executable File

"""
Project management endpoints.
A Project is created automatically when a separation job completes.
It links the separation job to downstream jobs (transcription, ABC, mix).
"""
import shutil
from pathlib import Path
from typing import Optional
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from .. import job_manager as jm
from ..config import PROJECTS_DIR, UPLOAD_DIR, TRANSCRIBE_MODES, DEFAULT_TRANSCRIBE_MODE, STATIC_OPTIONS
from ..services.project_metadata import write_project_metadata, read_project_metadata
from ..services.transcriber_service import run_transcription
from ..services.abc_service import run_abc_conversion
from ..services.mix_service import run_mix
from ..services.lrc_service import run_lrc_alignment, clean_lrc_content
from ..services.lyric_transcriber_service import (
run_lyric_transcription,
WHISPER_MODELS,
DEFAULT_WHISPER_MODEL,
)
from ..job_manager import set_project_trans_job, set_project_abc_job, set_project_lrc_job
router = APIRouter(prefix="/api/projects", tags=["projects"])
VALID_TRANSCRIBE_MODES = {m["id"] for m in TRANSCRIBE_MODES}
VALID_STATIC_OPTIONS = {o["id"] for o in STATIC_OPTIONS}
_AUDIO_EXTS = {".mp3", ".wav", ".flac"}
# ---------------------------------------------------------------------------
# Request bodies
# ---------------------------------------------------------------------------
class TranscribeRequest(BaseModel):
stem_name: str # which stem to transcribe (key from sep job)
mode: str = DEFAULT_TRANSCRIBE_MODE
class AbcRequest(BaseModel):
stem_name: str # which stem's MIDI to convert
song_title: Optional[str] = None
class LrcRequest(BaseModel):
stem_name: str # which stem to align (usually vocals)
lyrics_source: str = "text" # text | auto | tag
lyrics_text: Optional[str] = None # required when lyrics_source='text'
output_filename: Optional[str] = None
style_options: Optional[dict] = None
timing_precision: str = "word" # 'word' | 'syllable'
lrc_power_preset: str = "standard" # 'eco' | 'standard' | 'performance'
class MixRequest(BaseModel):
preset_type: str # 'static' | 'dynamic'
static_option: Optional[str] = None
dynamic_levels: Optional[dict] = None # stem_name → float volume
output_format: Optional[str] = None # inferred from stems if absent
class LyricTranscribeRequest(BaseModel):
"""Request body for AI-based lyric transcription using Whisper."""
stem_name: str # which stem to transcribe (usually vocals)
model: str = "large-v2" # whisper model: tiny, base, small, medium, large, large-v2, large-v3
language: Optional[str] = None # ISO-639-1 code (auto-detect if None)
task: str = "transcribe" # 'transcribe' or 'translate' (to English)
output_formats: list = ["elrc", "json", "txt"] # lrc, elrc, json, txt, srt, vtt
initial_prompt: Optional[str] = None # optional prompt to guide the model
temperature: float = 0.0 # sampling temperature
condition_on_previous_text: bool = False # if True, pass previous output as prompt; can cause hallucination loops
engine: str = "whisperx" # 'whisperx' (only supported engine)
diarize: bool = False # run speaker diarization (WhisperX only)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _get_project_or_404(project_id: str) -> dict:
project = jm.get_project(project_id)
if project is None:
raise HTTPException(status_code=404, detail=f"Project not found: {project_id}")
return project
def _get_job_or_none(job_id: Optional[str]) -> Optional[dict]:
if not job_id:
return None
return jm.get_job(job_id)
def _stem_files_from_job(sep_job: dict) -> dict:
"""Return {stem_name: abs_path} for all stem files in the separation job."""
return {
f["stem"]: f["path"]
for f in sep_job.get("files", [])
if f.get("type") == "stem" and f.get("stem") and Path(f.get("path", "")).exists()
}
def _infer_output_format(stem_files: dict) -> str:
"""Infer output format from stem file extensions."""
for path in stem_files.values():
ext = Path(path).suffix.lower().lstrip(".")
if ext in ("mp3", "flac", "wav"):
return ext
return "mp3"
def _project_output_dir(project: dict) -> Path:
"""Return the project's root directory under MEDIA_DIR."""
sep_job = jm.get_job(project["sep_job_id"]) or {}
result = sep_job.get("result") or {}
project_path = result.get("project_path")
if project_path and Path(project_path).is_dir():
return Path(project_path)
# Fallback: PROJECTS_DIR / project.name
return PROJECTS_DIR / project["name"]
def _find_stem_path(stem_files: dict, stem_name: str) -> Optional[str]:
"""Return path for the named stem, matching by key or by file basename/stem."""
if stem_name in stem_files:
return stem_files[stem_name]
for path in stem_files.values():
p = Path(path)
if p.name == stem_name or p.stem == stem_name:
return path
return None
# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------
@router.get("")
def list_projects():
"""Return all projects, newest first. Migrates orphaned separation jobs."""
return jm.list_projects()
@router.get("/{project_id}")
def get_project_detail(project_id: str):
"""Return project with all linked jobs expanded."""
project = _get_project_or_404(project_id)
trans_jobs_expanded = {
stem: jm.get_job(jid)
for stem, jid in (project.get("trans_jobs") or {}).items()
}
abc_jobs_expanded = {
stem: jm.get_job(jid)
for stem, jid in (project.get("abc_jobs") or {}).items()
}
lrc_jobs_expanded = {
stem: jm.get_job(jid)
for stem, jid in (project.get("lrc_jobs") or {}).items()
}
return {
**project,
"sep_job": jm.get_job(project["sep_job_id"]),
"trans_jobs": trans_jobs_expanded,
"abc_jobs": abc_jobs_expanded,
"lrc_jobs": lrc_jobs_expanded,
"mix_job": _get_job_or_none(project["mix_job_id"]),
}
@router.delete("/{project_id}")
def delete_project(project_id: str):
"""Remove the project record and all associated files from disk."""
project = _get_project_or_404(project_id)
# Delete project directory from disk
project_dir = _project_output_dir(project)
if project_dir.exists():
shutil.rmtree(project_dir, ignore_errors=True)
# Delete the upload directory for the originating separation job
upload_dir = UPLOAD_DIR / project["sep_job_id"]
if upload_dir.exists():
shutil.rmtree(upload_dir, ignore_errors=True)
from sqlalchemy.orm import Session
from ..job_manager import engine, ProjectRecord
with Session(engine) as session:
record = session.get(ProjectRecord, project_id)
if record:
session.delete(record)
session.commit()
return {"detail": "Project deleted"}
@router.post("/{project_id}/transcribe")
def transcribe_project(project_id: str, body: TranscribeRequest):
"""Transcribe a specific stem from this project to MIDI."""
if body.mode not in VALID_TRANSCRIBE_MODES:
raise HTTPException(status_code=400, detail=f"Unknown transcribe mode: {body.mode}")
project = _get_project_or_404(project_id)
sep_job = jm.get_job(project["sep_job_id"])
if sep_job is None:
raise HTTPException(status_code=404, detail="Separation job not found")
stem_files = _stem_files_from_job(sep_job)
if not stem_files:
raise HTTPException(status_code=404, detail="No stem files found in separation job")
stem_path = _find_stem_path(stem_files, body.stem_name)
if stem_path is None:
raise HTTPException(status_code=404, detail=f"Stem not found: {body.stem_name}")
job_id = jm.create_job("transcribe")
set_project_trans_job(project_id, body.stem_name, job_id)
jm.submit_gpu(run_transcription, job_id, stem_path, body.mode)
return {"job_id": job_id}
@router.post("/{project_id}/abc")
def abc_project(project_id: str, body: AbcRequest):
"""Convert MIDI from a specific stem's transcription job to ABC notation."""
project = _get_project_or_404(project_id)
trans_jobs_dict = project.get("trans_jobs") or {}
trans_job_id = trans_jobs_dict.get(body.stem_name)
if not trans_job_id:
raise HTTPException(
status_code=400,
detail=f"No transcription job for stem '{body.stem_name}'",
)
trans_job = jm.get_job(trans_job_id)
if trans_job is None:
raise HTTPException(status_code=404, detail="Transcription job not found")
if trans_job["status"] != "done":
raise HTTPException(status_code=400, detail="Transcription job has not completed successfully")
midi_paths = [
f["path"]
for f in trans_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 MIDI files found in transcription job")
title = (body.song_title or "").strip() or project["name"]
job_id = jm.create_job("abc-convert")
set_project_abc_job(project_id, body.stem_name, job_id)
jm.submit(run_abc_conversion, job_id, midi_paths, title)
return {"job_id": job_id}
@router.post("/{project_id}/mix")
def mix_project(project_id: str, body: MixRequest):
"""Create a static or dynamic mix from this project's stems."""
if body.preset_type not in ("static", "dynamic"):
raise HTTPException(status_code=400, detail="preset_type must be 'static' or 'dynamic'")
if body.preset_type == "static" and body.static_option and body.static_option not in VALID_STATIC_OPTIONS:
raise HTTPException(status_code=400, detail=f"Unknown static_option: {body.static_option}")
project = _get_project_or_404(project_id)
sep_job = jm.get_job(project["sep_job_id"])
if sep_job is None:
raise HTTPException(status_code=404, detail="Separation job not found")
stem_files = _stem_files_from_job(sep_job)
if not stem_files:
raise HTTPException(status_code=404, detail="No stem files found in separation job")
output_format = body.output_format or _infer_output_format(stem_files)
ext = "mp3" if output_format.startswith("mp3") else output_format
# Determine output directory and filename
project_dir = _project_output_dir(project)
if body.preset_type == "static":
sub = "Static"
suffix = _static_label(body.static_option)
else:
sub = "Dynamic"
suffix = "Dynamic Mix"
mix_dir = project_dir / sub
mix_dir.mkdir(parents=True, exist_ok=True)
output_path = str(mix_dir / f"{project['name']} - {suffix}.{ext}")
job_id = jm.create_job("mix")
jm.update_project(project_id, mix_job_id=job_id)
jm.submit(
run_mix,
job_id,
stem_files,
body.preset_type,
output_path,
output_format,
body.static_option,
{str(k): float(v) for k, v in (body.dynamic_levels or {}).items()},
)
return {"job_id": job_id}
@router.post("/{project_id}/lrc")
def lrc_project(project_id: str, body: LrcRequest):
"""Align lyrics with a specific stem from this project to produce an ELRC file."""
if body.lyrics_source not in ("auto", "tag", "text"):
raise HTTPException(status_code=400, detail=f"Invalid lyrics_source: {body.lyrics_source}")
if body.lyrics_source == "text" and not (body.lyrics_text or "").strip():
raise HTTPException(status_code=400, detail="lyrics_text required when lyrics_source='text'")
project = _get_project_or_404(project_id)
sep_job = jm.get_job(project["sep_job_id"])
if sep_job is None:
raise HTTPException(status_code=404, detail="Separation job not found")
project_dir = _project_output_dir(project)
# Find the source audio file (for output filename derivation)
source_file = None
for f in sep_job.get("files", []):
if f.get("type") == "source":
source_file = f
break
if source_file is None:
raise HTTPException(status_code=404, detail="Source audio file not found in project")
base_name = Path(source_file["name"]).stem
output_filename = (body.output_filename or "").strip() or f"{base_name}.elrc"
# Prefer the already-separated stem file to avoid re-running Demucs.
# If the stem file exists on disk, pass it directly with is_vocals=True.
stem_files = _stem_files_from_job(sep_job)
stem_path = _find_stem_path(stem_files, body.stem_name)
if stem_path and Path(stem_path).exists():
audio_path = stem_path
is_vocals = True
else:
# Stem file missing — fall back to the original mix and let the
# alignment service extract vocals via Demucs.
audio_path = source_file["path"]
is_vocals = False
job_id = jm.create_job("lrc_align")
set_project_lrc_job(project_id, body.stem_name, job_id)
jm.submit_gpu(
run_lrc_alignment,
job_id,
audio_path,
lyrics_source=body.lyrics_source,
lyrics_content=body.lyrics_text,
output_filename=output_filename,
style_options=body.style_options,
is_vocals=is_vocals,
output_dir=str(project_dir),
timing_precision=body.timing_precision,
lrc_power_preset=body.lrc_power_preset,
)
return {"job_id": job_id}
@router.post("/{project_id}/lyric-transcribe")
def lyric_transcribe_project(project_id: str, body: LyricTranscribeRequest):
"""
Transcribe lyrics from a vocal stem using AI (Whisper).
Unlike /lrc which aligns existing lyrics, this endpoint generates
lyrics from audio using speech recognition. It produces:
- ELRC with word-level timestamps
- LRC with line-level timestamps
- JSON with full transcription data
- Plain text lyrics
"""
# Validate model
if body.model not in WHISPER_MODELS:
raise HTTPException(status_code=400, detail=f"Unknown model: {body.model}")
if body.task not in ("transcribe", "translate"):
raise HTTPException(status_code=400, detail=f"Invalid task: {body.task}")
project = _get_project_or_404(project_id)
sep_job = jm.get_job(project["sep_job_id"])
if sep_job is None:
raise HTTPException(status_code=404, detail="Separation job not found")
# Find the stem file
stem_files = _stem_files_from_job(sep_job)
if body.stem_name not in stem_files:
raise HTTPException(status_code=404, detail=f"Stem not found: {body.stem_name}")
stem_path = stem_files[body.stem_name]
# Create output directory
project_dir = _project_output_dir(project)
output_dir = project_dir / "Transcriptions"
output_dir.mkdir(parents=True, exist_ok=True)
job_id = jm.create_job("lyric-transcribe")
# Store job reference keyed by stem name; same key for both engines so the
# frontend always shows the most-recent AI transcription for that stem.
set_project_lrc_job(project_id, f"{body.stem_name}_whisper", job_id)
jm.submit_gpu(
run_lyric_transcription,
job_id,
stem_path,
model=body.model,
language=body.language,
task=body.task,
output_formats=body.output_formats,
initial_prompt=body.initial_prompt,
temperature=body.temperature,
condition_on_previous_text=body.condition_on_previous_text,
output_dir=str(output_dir),
engine=body.engine,
diarize=body.diarize,
)
return {"job_id": job_id, "output_dir": str(output_dir)}
# ---------------------------------------------------------------------------
# Stem Reprocessing
# ---------------------------------------------------------------------------
class ReprocessRequest(BaseModel):
stem_name: str
model: str
output_format: str = "mp3"
@router.post("/{project_id}/reprocess")
def reprocess_stem_endpoint(project_id: str, body: ReprocessRequest):
"""
Reprocess an individual stem using a specialized model.
Useful for applying per-instrument models like MDX-Net or MelBand.
"""
from ..config import ALL_MODEL_IDS
from ..services.separator_service import run_separation
if body.model not in ALL_MODEL_IDS:
raise HTTPException(status_code=400, detail=f"Unknown model: {body.model}")
project = _get_project_or_404(project_id)
sep_job = jm.get_job(project["sep_job_id"])
if sep_job is None:
raise HTTPException(status_code=404, detail="Separation job not found")
stem_files = _stem_files_from_job(sep_job)
stem_path = _find_stem_path(stem_files, body.stem_name)
if stem_path is None:
raise HTTPException(status_code=404, detail=f"Stem not found: {body.stem_name}")
# Create a new separation job that processes just this stem
job_id = jm.create_job("separate")
# Submit to GPU executor - reprocess the stem file as if it were a source
jm.submit_gpu(
run_separation,
job_id,
stem_path,
body.model,
body.output_format,
preset_type="stems_only",
project_name_override=f"{project['name']}_{body.stem_name}_reprocessed",
)
return {"job_id": job_id}
# ---------------------------------------------------------------------------
# Stem Renaming
# ---------------------------------------------------------------------------
class RenameStemRequest(BaseModel):
stem_name: str
new_name: str
@router.post("/{project_id}/rename-stem")
def rename_stem_endpoint(project_id: str, body: RenameStemRequest):
"""Rename a stem file and update the job record."""
import os
project = _get_project_or_404(project_id)
sep_job = jm.get_job(project["sep_job_id"])
if sep_job is None:
raise HTTPException(status_code=404, detail="Separation job not found")
# Find the stem file
stem_files = sep_job.get("files", [])
stem_file = None
stem_idx = None
for idx, f in enumerate(stem_files):
if f.get("stem") == body.stem_name or f.get("name") == body.stem_name:
stem_file = f
stem_idx = idx
break
if stem_file is None:
raise HTTPException(status_code=404, detail=f"Stem not found: {body.stem_name}")
old_path = Path(stem_file["path"])
if not old_path.exists():
raise HTTPException(status_code=404, detail=f"Stem file not found on disk: {old_path}")
# Generate new filename
new_name = body.new_name.strip()
if not new_name:
raise HTTPException(status_code=400, detail="New name cannot be empty")
# Keep the same extension
new_filename = f"{new_name}{old_path.suffix}"
new_path = old_path.parent / new_filename
# Avoid overwriting existing files
if new_path.exists() and new_path != old_path:
raise HTTPException(status_code=409, detail=f"A file with name '{new_filename}' already exists")
try:
# Rename the file on disk
os.rename(str(old_path), str(new_path))
# Update the job record
stem_files[stem_idx]["name"] = new_filename
stem_files[stem_idx]["stem"] = new_name
stem_files[stem_idx]["path"] = str(new_path)
jm.update_job(project["sep_job_id"], files=stem_files)
return {
"success": True,
"old_name": body.stem_name,
"new_name": new_name,
"new_path": str(new_path),
}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to rename stem: {e}")
def _static_label(static_option: Optional[str]) -> str:
labels = {
"no_vocals": "No Vocals",
"no_drums": "No Drums",
"no_bass": "No Bass",
"no_vocals_no_bass": "No Vocals No Bass",
"no_drums_no_bass": "No Drums No Bass",
}
return labels.get(static_option or "", "Static Mix")
# ---------------------------------------------------------------------------
# Cleanup / maintenance endpoints
# ---------------------------------------------------------------------------
@router.get("/cleanup/verify")
def cleanup_verify():
"""
Check every project's stem files are actually on disk.
Returns a list of projects with missing files — no mutations performed.
"""
projects = jm.list_projects()
results = []
for proj in projects:
sep_job = jm.get_job(proj["sep_job_id"]) or {}
files = sep_job.get("files", [])
missing = [
f["path"]
for f in files
if f.get("type") == "stem" and not Path(f.get("path", "")).exists()
]
if missing:
results.append({
"project_id": proj["id"],
"name": proj["name"],
"missing_files": missing,
})
return {"projects_with_missing_files": results, "total": len(results)}
@router.post("/cleanup/write-metadata")
def cleanup_write_metadata():
"""
Write/refresh project.json for every project in the database.
Uses current DB state so trans_jobs, lrc_jobs etc. are included.
"""
projects = jm.list_projects()
written = 0
errors = []
for proj in projects:
try:
project_dir = _project_output_dir(proj)
if not project_dir.exists():
continue
sep_job = jm.get_job(proj["sep_job_id"]) or {}
files = sep_job.get("files", [])
stem_files = {
f["stem"]: f["path"]
for f in files
if f.get("type") == "stem" and f.get("stem")
}
source_file = next(
(f["path"] for f in files if f.get("type") == "source"), ""
)
result = sep_job.get("result") or {}
write_project_metadata(
project_dir,
project_id=proj["id"],
name=proj["name"],
model=result.get("model_label", ""),
model_label=result.get("model_label", ""),
format=result.get("format_label", ""),
format_label=result.get("format_label", ""),
source_file=source_file,
stems=stem_files,
sep_job_id=proj["sep_job_id"],
trans_jobs=proj.get("trans_jobs") or {},
abc_jobs=proj.get("abc_jobs") or {},
lrc_jobs=proj.get("lrc_jobs") or {},
mix_job_id=proj.get("mix_job_id"),
created_at=proj.get("created_at"),
)
written += 1
except Exception as e:
errors.append({"project": proj.get("name", proj["id"]), "error": str(e)})
return {"written": written, "errors": errors}
@router.post("/cleanup/rename-stems")
def cleanup_rename_stems():
"""
Rename all stem files that still use the old 'filename - Vocals.ext' format
to the new 'Artist - Title (vocals).ext' format.
Updates the DB job file records after each rename.
"""
from ..services.separator_service import _stem_filename
projects = jm.list_projects()
renamed_total = 0
errors = []
for proj in projects:
sep_job = jm.get_job(proj["sep_job_id"])
if not sep_job:
continue
files = list(sep_job.get("files", []))
proj_name = proj["name"]
changed = False
for i, f in enumerate(files):
if f.get("type") != "stem" or not f.get("stem"):
continue
old_path = Path(f["path"])
if not old_path.exists():
continue
# Build the expected new name and skip if already correct
new_filename = _stem_filename(proj_name, f["stem"], old_path.suffix)
new_path = old_path.parent / new_filename
if old_path == new_path:
continue
try:
old_path.rename(new_path)
files[i] = {**f, "name": new_filename, "path": str(new_path)}
changed = True
renamed_total += 1
except Exception as e:
errors.append({
"project": proj_name,
"file": str(old_path),
"error": str(e),
})
if changed:
jm.update_job(proj["sep_job_id"], files=files)
return {"renamed": renamed_total, "errors": errors}
@router.post("/cleanup/purge-orphaned")
def cleanup_purge_orphaned():
"""
Delete separation job rows (job_type='separate') that have no matching
ProjectRecord. Also deletes jobs of any type whose parent project is gone.
"""
project_job_ids = {
proj["sep_job_id"]
for proj in jm.list_projects()
if proj.get("sep_job_id")
}
all_jobs = jm.list_all_jobs()
purged = 0
for job in all_jobs:
if job.get("job_type") == "separate" and job["id"] not in project_job_ids:
jm.delete_job(job["id"])
purged += 1
return {"purged": purged}
@router.post("/cleanup/clean-lrc-files")
def cleanup_clean_lrc_files():
"""
Scan every LRC/ELRC output file in all lrc jobs and strip section-marker
lines (e.g. [01:30.00][Chorus]). Returns counts of files and lines affected.
"""
all_jobs = jm.list_all_jobs()
files_modified = 0
lines_removed = 0
errors = []
for job in all_jobs:
if job.get("job_type") != "lrc":
continue
for file_entry in (job.get("files") or []):
ftype = file_entry.get("type", "")
if ftype not in ("elrc", "lrc"):
continue
fpath = file_entry.get("path")
if not fpath:
continue
p = Path(fpath)
if not p.exists():
continue
try:
content = p.read_text(encoding="utf-8")
cleaned, removed = clean_lrc_content(content)
if removed > 0:
p.write_text(cleaned, encoding="utf-8")
files_modified += 1
lines_removed += removed
except Exception as exc:
errors.append({"file": fpath, "error": str(exc)})
return {
"files_modified": files_modified,
"lines_removed": lines_removed,
"errors": errors,
}