Files
stemengine_old/tmp/run_pipeline_benchmark.py
2026-04-16 15:52:46 +09:00

255 lines
9.4 KiB
Python

#!/usr/bin/env python3
import json
import time
from datetime import datetime
from pathlib import Path
from urllib import parse, request, error
BASE = "http://127.0.0.1:8000/api"
TRACK_PATH = "/app/media/imports/Olivia Rodrigo - good 4 u.flac"
PRESET_FILE = Path("/home/translator/docker/appdata/stemengine/backend/presets/auto_pipeline/default_presets.json")
OUT_DIR = Path("/home/translator/docker/appdata/stemengine/tmp")
OUT_DIR.mkdir(parents=True, exist_ok=True)
RUN_TAG = datetime.now().strftime("%Y%m%d_%H%M%S")
RESULT_PATH = OUT_DIR / f"pipeline_benchmark_{RUN_TAG}.json"
def _http_json(url: str, method: str = "GET", data=None, is_form: bool = False):
headers = {"Accept": "application/json"}
body = None
if data is not None:
if is_form:
body = parse.urlencode(data).encode("utf-8")
headers["Content-Type"] = "application/x-www-form-urlencoded"
else:
body = json.dumps(data).encode("utf-8")
headers["Content-Type"] = "application/json"
req = request.Request(url, method=method, data=body, headers=headers)
try:
with request.urlopen(req, timeout=120) as r:
raw = r.read()
return json.loads(raw.decode("utf-8")) if raw else {}
except error.HTTPError as e:
detail = e.read().decode("utf-8", errors="replace")
raise RuntimeError(f"HTTP {e.code} {url}: {detail}")
def get_json(path: str):
return _http_json(f"{BASE}{path}")
def post_form(path: str, data: dict):
return _http_json(f"{BASE}{path}", method="POST", data=data, is_form=True)
def post_json(path: str, data: dict):
return _http_json(f"{BASE}{path}", method="POST", data=data)
def delete_json(path: str, data: dict):
return _http_json(f"{BASE}{path}", method="DELETE", data=data)
def wait_job(job_id: str, poll_sec: float = 2.5, timeout_sec: int = 7200):
t0 = time.time()
while True:
j = get_json(f"/jobs/{job_id}")
status = j.get("status")
progress = j.get("progress", "")
print(f" [job {job_id[:8]}] {status}: {progress}", flush=True)
if status == "done":
return j, time.time() - t0
if status in ("error", "cancelled"):
raise RuntimeError(f"Job {job_id} failed: {j.get('error') or status}")
if time.time() - t0 > timeout_sec:
raise RuntimeError(f"Job {job_id} timed out after {timeout_sec}s")
time.sleep(poll_sec)
def find_project_id_by_name(name: str):
projects = get_json("/projects")
for p in projects:
if p.get("name") == name:
return p.get("id")
return None
def stem_names_for_project(project_id: str):
detail = get_json(f"/projects/{project_id}")
files = (detail.get("sep_job") or {}).get("files") or []
stems = [f.get("stem") for f in files if f.get("type") == "stem" and f.get("stem")]
return stems, detail
def run_preset(preset: dict):
name = preset["name"]
safe_name = name.replace(" ", "_")
project_name = f"BENCH4_{safe_name}_{RUN_TAG}"
print(f"\n=== {name} ===", flush=True)
result = {
"preset": name,
"project_name": project_name,
"model": preset.get("model"),
"format": preset.get("format"),
"instrumentModels": preset.get("instrumentModels", {}),
"steps": [],
"status": "running",
}
# Stage 0: Separation
sep_payload = {
"file_path": TRACK_PATH,
"model": preset.get("model", "htdemucs"),
"output_format": preset.get("format", "mp3"),
"preset_type": "stems_only",
"project_name": project_name,
}
inst_models = preset.get("instrumentModels") or {}
if inst_models:
sep_payload["instrument_models"] = json.dumps(inst_models)
t0 = time.time()
sep_submit = post_form("/separate/from-path", sep_payload)
sep_job_id = sep_submit["job_id"]
print(f" separation job: {sep_job_id}", flush=True)
sep_job, sep_wait = wait_job(sep_job_id)
result["separation"] = {
"job_id": sep_job_id,
"wait_seconds": round(sep_wait, 2),
"status": sep_job.get("status"),
"progress": sep_job.get("progress"),
"result": sep_job.get("result", {}),
}
project_id = (sep_job.get("result") or {}).get("project_id") or find_project_id_by_name(project_name)
if not project_id:
raise RuntimeError(f"Could not resolve project_id for {project_name}")
result["project_id"] = project_id
last_lyric_stem = "vocals"
# Stage 1+: preset steps
for idx, step in enumerate(preset.get("steps", []), start=1):
stype = step.get("type")
srec = {"index": idx, "type": stype, "payload": step}
s0 = time.time()
try:
if stype == "renameStem":
from_stem = (step.get("fromStem") or "").strip()
to_stem = (step.get("toStem") or "").strip()
resp = post_json(f"/projects/{project_id}/rename-stem", {
"stem_name": from_stem,
"new_name": to_stem,
})
srec["response"] = resp
elif stype == "deleteStem":
stem = (step.get("stem") or "").strip()
resp = delete_json(f"/projects/{project_id}/stem", {"stem_name": stem})
srec["response"] = resp
elif stype == "lyrics":
last_lyric_stem = step.get("stem") or "vocals"
body = {
"stem_name": last_lyric_stem,
"engine": step.get("engine", "whisperx"),
"model": step.get("model", "medium"),
"task": "transcribe",
"output_formats": ["elrc", "json", "txt"],
"diarize": bool(step.get("diarize", False)),
"lsync_align": bool(step.get("lsyncAlign", False)),
"two_pass": bool(step.get("twoPass", False)),
"vocal_style": step.get("vocalStyle", "general"),
"explicit": bool(step.get("explicit", False)),
"vocal_source": "as_is",
"lrc_power_preset": "standard",
}
j = post_json(f"/projects/{project_id}/lyric-transcribe", body)
jid = j["job_id"]
jj, w = wait_job(jid)
srec["job_id"] = jid
srec["wait_seconds"] = round(w, 2)
srec["job_status"] = jj.get("status")
srec["job_progress"] = jj.get("progress")
srec["job_result"] = jj.get("result", {})
elif stype == "lyricsAlign":
body = {
"stem_name": last_lyric_stem,
"lyrics_source": "auto",
"timing_precision": step.get("timingPrecision", "word"),
"lrc_power_preset": "standard",
}
j = post_json(f"/projects/{project_id}/lrc", body)
jid = j["job_id"]
jj, w = wait_job(jid)
srec["job_id"] = jid
srec["wait_seconds"] = round(w, 2)
srec["job_status"] = jj.get("status")
srec["job_progress"] = jj.get("progress")
elif stype == "sekPackage":
body = {
"package_name": step.get("packageName"),
"include_video": bool(step.get("includeVideo", True)),
"video_source": None,
"sync_reference_track": None,
"auto_sync": True,
}
resp = post_json(f"/projects/{project_id}/export-sek", body)
srec["response"] = resp
else:
srec["skipped"] = True
srec["reason"] = "unsupported step in benchmark runner"
srec["status"] = "done"
except Exception as exc:
srec["status"] = "error"
srec["error"] = str(exc)
result["steps"].append(srec)
raise
finally:
srec["elapsed_seconds"] = round(time.time() - s0, 2)
result["steps"].append(srec)
stems, detail = stem_names_for_project(project_id)
result["final_stems"] = stems
result["project_detail_summary"] = {
"lrc_jobs": len(detail.get("lrc_jobs") or {}),
"trans_jobs": len(detail.get("trans_jobs") or {}),
"abc_jobs": len(detail.get("abc_jobs") or {}),
}
result["total_seconds"] = round(time.time() - t0, 2)
result["status"] = "done"
return result
def main():
presets = json.loads(PRESET_FILE.read_text(encoding="utf-8"))
targets = [p for p in presets if p.get("name") in ("Quick Karaoke", "Pro Karaoke", "Rich Karaoke")]
if len(targets) != 3:
raise SystemExit("Could not load expected 3 target presets")
run = {
"run_tag": RUN_TAG,
"started_at": datetime.now().isoformat(),
"track_path": TRACK_PATH,
"preset_file": str(PRESET_FILE),
"results": [],
"status": "running",
}
try:
for p in targets:
run["results"].append(run_preset(p))
run["status"] = "done"
except Exception as exc:
run["status"] = "error"
run["error"] = str(exc)
finally:
run["finished_at"] = datetime.now().isoformat()
RESULT_PATH.write_text(json.dumps(run, ensure_ascii=False, indent=2), encoding="utf-8")
print(f"\nRESULT_PATH={RESULT_PATH}", flush=True)
print(f"STATUS={run['status']}", flush=True)
if __name__ == "__main__":
main()