Spaces:
Running
Running
Prefer Python Verovio for score rendering
Browse files- apps/theme_lab/app.py +19 -5
apps/theme_lab/app.py
CHANGED
|
@@ -116,10 +116,14 @@ def index() -> FileResponse:
|
|
| 116 |
|
| 117 |
@app.get("/api/health")
|
| 118 |
def health() -> dict[str, object]:
|
|
|
|
|
|
|
| 119 |
return {
|
| 120 |
"ok": True,
|
| 121 |
"database": DB_PATH.exists(),
|
| 122 |
-
"verovio":
|
|
|
|
|
|
|
| 123 |
"markov_cache_entries": len(_markov_cache),
|
| 124 |
"default_markov_cache": DEFAULT_MARKOV_CACHE.exists(),
|
| 125 |
"transformer_checkpoint": DEFAULT_TRANSFORMER_CHECKPOINT.exists(),
|
|
@@ -366,6 +370,10 @@ def sequence_note_events(sequence: tuple[Symbol, ...], key_name: str) -> list[di
|
|
| 366 |
def has_verovio_renderer() -> bool:
|
| 367 |
if shutil.which("verovio") is not None:
|
| 368 |
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
try:
|
| 370 |
import verovio # noqa: F401
|
| 371 |
except ImportError:
|
|
@@ -379,7 +387,10 @@ def render_musicxml_with_python_verovio(musicxml_path: Path, svg_path: Path) ->
|
|
| 379 |
|
| 380 |
toolkit = verovio.toolkit()
|
| 381 |
toolkit.setOptions({"scale": 42})
|
| 382 |
-
|
|
|
|
|
|
|
|
|
|
| 383 |
return None
|
| 384 |
svg_path.write_text(toolkit.renderToSVG(1), encoding="utf-8")
|
| 385 |
except Exception:
|
|
@@ -389,9 +400,12 @@ def render_musicxml_with_python_verovio(musicxml_path: Path, svg_path: Path) ->
|
|
| 389 |
|
| 390 |
def render_musicxml_to_svg(musicxml_path: Path) -> Path | None:
|
| 391 |
svg_path = musicxml_path.with_suffix(".svg")
|
|
|
|
|
|
|
|
|
|
| 392 |
verovio = shutil.which("verovio")
|
| 393 |
if verovio is None:
|
| 394 |
-
return
|
| 395 |
try:
|
| 396 |
subprocess.run(
|
| 397 |
[verovio, "-s", "42", "-o", str(svg_path), str(musicxml_path)],
|
|
@@ -403,8 +417,8 @@ def render_musicxml_to_svg(musicxml_path: Path) -> Path | None:
|
|
| 403 |
timeout=20,
|
| 404 |
)
|
| 405 |
except (subprocess.SubprocessError, OSError):
|
| 406 |
-
return
|
| 407 |
-
return svg_path if svg_path.exists() else
|
| 408 |
|
| 409 |
|
| 410 |
def markov_cache_key(request: GenerateRequest) -> tuple[object, ...]:
|
|
|
|
| 116 |
|
| 117 |
@app.get("/api/health")
|
| 118 |
def health() -> dict[str, object]:
|
| 119 |
+
verovio_cli = shutil.which("verovio") is not None
|
| 120 |
+
verovio_python = has_python_verovio_renderer()
|
| 121 |
return {
|
| 122 |
"ok": True,
|
| 123 |
"database": DB_PATH.exists(),
|
| 124 |
+
"verovio": verovio_cli or verovio_python,
|
| 125 |
+
"verovio_cli": verovio_cli,
|
| 126 |
+
"verovio_python": verovio_python,
|
| 127 |
"markov_cache_entries": len(_markov_cache),
|
| 128 |
"default_markov_cache": DEFAULT_MARKOV_CACHE.exists(),
|
| 129 |
"transformer_checkpoint": DEFAULT_TRANSFORMER_CHECKPOINT.exists(),
|
|
|
|
| 370 |
def has_verovio_renderer() -> bool:
|
| 371 |
if shutil.which("verovio") is not None:
|
| 372 |
return True
|
| 373 |
+
return has_python_verovio_renderer()
|
| 374 |
+
|
| 375 |
+
|
| 376 |
+
def has_python_verovio_renderer() -> bool:
|
| 377 |
try:
|
| 378 |
import verovio # noqa: F401
|
| 379 |
except ImportError:
|
|
|
|
| 387 |
|
| 388 |
toolkit = verovio.toolkit()
|
| 389 |
toolkit.setOptions({"scale": 42})
|
| 390 |
+
loaded = toolkit.loadFile(str(musicxml_path))
|
| 391 |
+
if not loaded and hasattr(toolkit, "loadData"):
|
| 392 |
+
loaded = toolkit.loadData(musicxml_path.read_text(encoding="utf-8"))
|
| 393 |
+
if not loaded:
|
| 394 |
return None
|
| 395 |
svg_path.write_text(toolkit.renderToSVG(1), encoding="utf-8")
|
| 396 |
except Exception:
|
|
|
|
| 400 |
|
| 401 |
def render_musicxml_to_svg(musicxml_path: Path) -> Path | None:
|
| 402 |
svg_path = musicxml_path.with_suffix(".svg")
|
| 403 |
+
python_svg = render_musicxml_with_python_verovio(musicxml_path, svg_path)
|
| 404 |
+
if python_svg is not None:
|
| 405 |
+
return python_svg
|
| 406 |
verovio = shutil.which("verovio")
|
| 407 |
if verovio is None:
|
| 408 |
+
return None
|
| 409 |
try:
|
| 410 |
subprocess.run(
|
| 411 |
[verovio, "-s", "42", "-o", str(svg_path), str(musicxml_path)],
|
|
|
|
| 417 |
timeout=20,
|
| 418 |
)
|
| 419 |
except (subprocess.SubprocessError, OSError):
|
| 420 |
+
return None
|
| 421 |
+
return svg_path if svg_path.exists() else None
|
| 422 |
|
| 423 |
|
| 424 |
def markov_cache_key(request: GenerateRequest) -> tuple[object, ...]:
|