Spaces:
Running
Running
Configure Verovio resources in Theme Lab
Browse files- apps/theme_lab/app.py +47 -2
apps/theme_lab/app.py
CHANGED
|
@@ -3,6 +3,7 @@
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import argparse
|
|
|
|
| 6 |
from html import escape
|
| 7 |
import os
|
| 8 |
import pickle
|
|
@@ -58,7 +59,13 @@ _transformer_lock = threading.Lock()
|
|
| 58 |
_transformer_checkpoint = None
|
| 59 |
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
| 63 |
app.mount("/svgs", StaticFiles(directory=ROOT / "svgs"), name="svgs")
|
| 64 |
app.mount("/midis", StaticFiles(directory=ROOT / "midis"), name="midis")
|
|
@@ -122,12 +129,21 @@ def index() -> FileResponse:
|
|
| 122 |
def health() -> dict[str, object]:
|
| 123 |
verovio_cli = shutil.which("verovio") is not None
|
| 124 |
verovio_python = has_python_verovio_renderer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
return {
|
| 126 |
"ok": True,
|
| 127 |
"database": DB_PATH.exists(),
|
| 128 |
"verovio": verovio_cli or verovio_python,
|
| 129 |
"verovio_cli": verovio_cli,
|
| 130 |
"verovio_python": verovio_python,
|
|
|
|
| 131 |
"score_preview_render_version": SCORE_PREVIEW_RENDER_VERSION,
|
| 132 |
"markov_cache_entries": len(_markov_cache),
|
| 133 |
"default_markov_cache": DEFAULT_MARKOV_CACHE.exists(),
|
|
@@ -475,6 +491,34 @@ def has_python_verovio_renderer() -> bool:
|
|
| 475 |
return True
|
| 476 |
|
| 477 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
def musicxml_preview_data(musicxml_data: str, *, strip_ties: bool = False, strip_beams: bool = False) -> str | None:
|
| 479 |
try:
|
| 480 |
root = ET.fromstring(musicxml_data)
|
|
@@ -507,6 +551,7 @@ def render_musicxml_with_python_verovio(
|
|
| 507 |
try:
|
| 508 |
import verovio
|
| 509 |
|
|
|
|
| 510 |
musicxml_data = musicxml_path.read_text(encoding="utf-8")
|
| 511 |
if strip_ties or strip_beams:
|
| 512 |
musicxml_data = (
|
|
@@ -514,6 +559,7 @@ def render_musicxml_with_python_verovio(
|
|
| 514 |
or musicxml_data
|
| 515 |
)
|
| 516 |
toolkit = verovio.toolkit()
|
|
|
|
| 517 |
toolkit.setOptions({"scale": 42})
|
| 518 |
svg = toolkit.renderData(musicxml_data, {}) if hasattr(toolkit, "renderData") else ""
|
| 519 |
if not svg:
|
|
@@ -710,7 +756,6 @@ def load_default_transformer_checkpoint() -> None:
|
|
| 710 |
_transformer_checkpoint = checkpoint
|
| 711 |
|
| 712 |
|
| 713 |
-
@app.on_event("startup")
|
| 714 |
def start_background_warmup() -> None:
|
| 715 |
loaded_precomputed = load_precomputed_markov_backend()
|
| 716 |
if not loaded_precomputed and os.environ.get("THEME_LAB_WARM_MARKOV", "").lower() in {"1", "true", "yes"}:
|
|
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import argparse
|
| 6 |
+
from contextlib import asynccontextmanager
|
| 7 |
from html import escape
|
| 8 |
import os
|
| 9 |
import pickle
|
|
|
|
| 59 |
_transformer_checkpoint = None
|
| 60 |
|
| 61 |
|
| 62 |
+
@asynccontextmanager
|
| 63 |
+
async def lifespan(_: FastAPI):
|
| 64 |
+
start_background_warmup()
|
| 65 |
+
yield
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
app = FastAPI(title="Theme Lab", version="0.1.0", lifespan=lifespan)
|
| 69 |
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
| 70 |
app.mount("/svgs", StaticFiles(directory=ROOT / "svgs"), name="svgs")
|
| 71 |
app.mount("/midis", StaticFiles(directory=ROOT / "midis"), name="midis")
|
|
|
|
| 129 |
def health() -> dict[str, object]:
|
| 130 |
verovio_cli = shutil.which("verovio") is not None
|
| 131 |
verovio_python = has_python_verovio_renderer()
|
| 132 |
+
verovio_resource_path = None
|
| 133 |
+
if verovio_python:
|
| 134 |
+
try:
|
| 135 |
+
import verovio
|
| 136 |
+
|
| 137 |
+
verovio_resource_path = configure_verovio_resources(verovio)
|
| 138 |
+
except Exception:
|
| 139 |
+
verovio_resource_path = None
|
| 140 |
return {
|
| 141 |
"ok": True,
|
| 142 |
"database": DB_PATH.exists(),
|
| 143 |
"verovio": verovio_cli or verovio_python,
|
| 144 |
"verovio_cli": verovio_cli,
|
| 145 |
"verovio_python": verovio_python,
|
| 146 |
+
"verovio_resource_path": verovio_resource_path,
|
| 147 |
"score_preview_render_version": SCORE_PREVIEW_RENDER_VERSION,
|
| 148 |
"markov_cache_entries": len(_markov_cache),
|
| 149 |
"default_markov_cache": DEFAULT_MARKOV_CACHE.exists(),
|
|
|
|
| 491 |
return True
|
| 492 |
|
| 493 |
|
| 494 |
+
def packaged_verovio_resource_path(verovio_module) -> Path | None:
|
| 495 |
+
module_file = getattr(verovio_module, "__file__", None)
|
| 496 |
+
if not module_file:
|
| 497 |
+
return None
|
| 498 |
+
data_path = Path(module_file).resolve().parent / "data"
|
| 499 |
+
if not data_path.exists():
|
| 500 |
+
return None
|
| 501 |
+
required = ["Bravura.xml", "Leipzig.xml"]
|
| 502 |
+
if not all((data_path / name).exists() for name in required):
|
| 503 |
+
return None
|
| 504 |
+
return data_path
|
| 505 |
+
|
| 506 |
+
|
| 507 |
+
def configure_verovio_resources(verovio_module, toolkit=None) -> str | None:
|
| 508 |
+
data_path = packaged_verovio_resource_path(verovio_module)
|
| 509 |
+
if data_path is None:
|
| 510 |
+
return None
|
| 511 |
+
resource_path = str(data_path)
|
| 512 |
+
try:
|
| 513 |
+
if hasattr(verovio_module, "setDefaultResourcePath"):
|
| 514 |
+
verovio_module.setDefaultResourcePath(resource_path)
|
| 515 |
+
if toolkit is not None and hasattr(toolkit, "setResourcePath"):
|
| 516 |
+
toolkit.setResourcePath(resource_path)
|
| 517 |
+
except Exception:
|
| 518 |
+
return None
|
| 519 |
+
return resource_path
|
| 520 |
+
|
| 521 |
+
|
| 522 |
def musicxml_preview_data(musicxml_data: str, *, strip_ties: bool = False, strip_beams: bool = False) -> str | None:
|
| 523 |
try:
|
| 524 |
root = ET.fromstring(musicxml_data)
|
|
|
|
| 551 |
try:
|
| 552 |
import verovio
|
| 553 |
|
| 554 |
+
configure_verovio_resources(verovio)
|
| 555 |
musicxml_data = musicxml_path.read_text(encoding="utf-8")
|
| 556 |
if strip_ties or strip_beams:
|
| 557 |
musicxml_data = (
|
|
|
|
| 559 |
or musicxml_data
|
| 560 |
)
|
| 561 |
toolkit = verovio.toolkit()
|
| 562 |
+
configure_verovio_resources(verovio, toolkit)
|
| 563 |
toolkit.setOptions({"scale": 42})
|
| 564 |
svg = toolkit.renderData(musicxml_data, {}) if hasattr(toolkit, "renderData") else ""
|
| 565 |
if not svg:
|
|
|
|
| 756 |
_transformer_checkpoint = checkpoint
|
| 757 |
|
| 758 |
|
|
|
|
| 759 |
def start_background_warmup() -> None:
|
| 760 |
loaded_precomputed = load_precomputed_markov_backend()
|
| 761 |
if not loaded_precomputed and os.environ.get("THEME_LAB_WARM_MARKOV", "").lower() in {"1", "true", "yes"}:
|