diff --git a/README.md b/README.md index bdcb129..5a3eaf9 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,16 @@ Because sloppak songs already have an isolated vocals track from Demucs separati ## Requirements -- A running [Slopsmith Demucs Server](https://github.com/byrongamatos/slopsmith-demucs-server) with the `/align` endpoint (v2+) +- A running [Slopsmith Demucs Server](https://github.com/byrongamatos/slopsmith-demucs-server) v2 or later with the `/align` endpoint and `/health` readiness data - The demucs server URL must be configured in Slopsmith settings - Sloppak songs with split stems (use the Sloppak Converter plugin with Demucs splitting enabled) +The alignment server is considered available only when `/health` returns HTTP 200 +and `warmup.whisperx` is exactly `"ready"`. Demucs Server v2 and later expose +this `warmup.whisperx` field; the server can return 200 while WhisperX is still +downloading or loading, so the plugin reports it as unavailable until warmup is +complete. + ## Install ```bash diff --git a/routes.py b/routes.py index f27a3c4..edd5471 100644 --- a/routes.py +++ b/routes.py @@ -1,8 +1,10 @@ """Lyrics Sync plugin — generate time-synced LRC from plain text lyrics + vocals stem.""" import json +import ipaddress import os from pathlib import Path +from urllib.parse import urlparse from fastapi import FastAPI from fastapi.responses import JSONResponse, Response @@ -13,6 +15,25 @@ SLOPPAK_CACHE_DIR = None +def _is_approved_demucs_url(url: object) -> bool: + """Allow only local/private HTTP(S) Demucs server destinations.""" + if not isinstance(url, str): + return False + parsed = urlparse(url) + if parsed.scheme not in {"http", "https"} or parsed.username or parsed.password: + return False + hostname = parsed.hostname + if not hostname or parsed.query or parsed.fragment: + return False + if hostname.lower() == "localhost": + return True + try: + address = ipaddress.ip_address(hostname) + except ValueError: + return False + return address.is_loopback or address.is_private or address.is_link_local + + def _get_demucs_server_url() -> str | None: """Get the configured demucs server URL from config.json.""" config_file = _config_dir / "config.json" @@ -20,7 +41,7 @@ def _get_demucs_server_url() -> str | None: try: cfg = json.loads(config_file.read_text()) url = cfg.get("demucs_server_url", "") - if url: + if _is_approved_demucs_url(url): return url.rstrip("/") except Exception: pass @@ -96,16 +117,45 @@ def setup(app: FastAPI, context: dict): @app.get("/api/plugins/lyrics_sync/status") def ls_status(): - """Check if alignment server is reachable.""" + """Check if the alignment server is reachable AND actually ready to + align — not just that /health returns 200. + + The demucs-server's /health responds 200 with `status: "ok"` as soon + as the process is up, well before its models finish loading — during + that window its `warmup` block reports fields like `whisperx: + "downloading"`. Treating any 200 as "ready" (the previous behavior) + showed a green "Alignment server ready" notice while a request would + still hit the server mid-warmup, surfacing as a confusing + "Cannot connect to alignment server" once the user actually clicked + Align. `/align` needs whisperx (transcription, used for VAD-anchored + alignment windows — see the demucs-server's own /align docs) loaded + first; per-language aligners load lazily on demand and are not + gated on here. + """ url = _get_demucs_server_url() if not url: return {"available": False, "reason": "No demucs server configured"} try: import requests - resp = requests.get(f"{url}/health", timeout=5) - if resp.status_code == 200: - return {"available": True, "server_url": url} - return {"available": False, "reason": f"Server returned {resp.status_code}"} + resp = requests.get(f"{url}/health", timeout=5, allow_redirects=False) + if resp.status_code != 200: + return {"available": False, "reason": f"Server returned {resp.status_code}"} + body = resp.json() + warmup = body.get("warmup") or {} + whisperx_state = warmup.get("whisperx") + if whisperx_state != "ready": + if whisperx_state in {"downloading", "loading", "starting", "warming_up"}: + reason = ( + f"Alignment model still loading ({whisperx_state}) " + "— try again in a moment." + ) + else: + reason = f"Alignment model is not ready ({whisperx_state or 'unknown'})" + return { + "available": False, + "reason": reason, + } + return {"available": True, "server_url": url} except Exception as e: return {"available": False, "reason": str(e)} @@ -152,6 +202,7 @@ def ls_align(data: dict): "granularity": granularity, }, timeout=300, + allow_redirects=False, ) if resp.status_code != 200: