OMR: Python 3.11 para el segundo lector y registro de su preparación
Browse files
README.md
CHANGED
|
@@ -2,4 +2,5 @@
|
|
| 2 |
title: Encore OMR
|
| 3 |
sdk: gradio
|
| 4 |
app_file: app.py
|
| 5 |
-
|
|
|
|
|
|
| 2 |
title: Encore OMR
|
| 3 |
sdk: gradio
|
| 4 |
app_file: app.py
|
| 5 |
+
python_version: "3.11"
|
| 6 |
+
---
|
app.py
CHANGED
|
@@ -149,6 +149,7 @@ def health():
|
|
| 149 |
"building": _build_state["building"],
|
| 150 |
"error": _build_state["error"] or None,
|
| 151 |
"second_reader": _second_state["ready"],
|
|
|
|
| 152 |
}
|
| 153 |
|
| 154 |
|
|
@@ -344,7 +345,7 @@ def job_status(job_id: str, authorization: str | None = Header(default=None)):
|
|
| 344 |
SECOND_MAX_IMAGES = int(os.environ.get("OMR_SECOND_MAX_IMAGES", "16"))
|
| 345 |
SECOND_MAX_MB = int(os.environ.get("OMR_SECOND_MAX_MB", "4"))
|
| 346 |
SECOND_TIMEOUT_SECONDS = int(os.environ.get("OMR_SECOND_TIMEOUT_SECONDS", "240"))
|
| 347 |
-
_second_state = {"ready": False, "failed": False}
|
| 348 |
|
| 349 |
|
| 350 |
def _prepare_second():
|
|
@@ -354,13 +355,30 @@ def _prepare_second():
|
|
| 354 |
OMR_SECOND_READER=0 lo apaga."""
|
| 355 |
import sys
|
| 356 |
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
_second_state["
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
_second_state["failed"] = not _second_state["ready"]
|
| 365 |
|
| 366 |
|
|
@@ -377,7 +395,7 @@ def second(files: list[UploadFile] = File(...), authorization: str | None = Head
|
|
| 377 |
compás y marca dónde no coinciden."""
|
| 378 |
_check_auth(authorization)
|
| 379 |
if _second_state["failed"]:
|
| 380 |
-
raise HTTPException(status_code=501, detail="Este servicio no tiene el segundo lector.")
|
| 381 |
if not _second_state["ready"]:
|
| 382 |
raise HTTPException(status_code=503, detail="El segundo lector se está preparando; probá de nuevo en un minuto.")
|
| 383 |
if len(files) > SECOND_MAX_IMAGES:
|
|
|
|
| 149 |
"building": _build_state["building"],
|
| 150 |
"error": _build_state["error"] or None,
|
| 151 |
"second_reader": _second_state["ready"],
|
| 152 |
+
"second_reader_error": _second_state["error"],
|
| 153 |
}
|
| 154 |
|
| 155 |
|
|
|
|
| 345 |
SECOND_MAX_IMAGES = int(os.environ.get("OMR_SECOND_MAX_IMAGES", "16"))
|
| 346 |
SECOND_MAX_MB = int(os.environ.get("OMR_SECOND_MAX_MB", "4"))
|
| 347 |
SECOND_TIMEOUT_SECONDS = int(os.environ.get("OMR_SECOND_TIMEOUT_SECONDS", "240"))
|
| 348 |
+
_second_state = {"ready": False, "failed": False, "error": None}
|
| 349 |
|
| 350 |
|
| 351 |
def _prepare_second():
|
|
|
|
| 355 |
OMR_SECOND_READER=0 lo apaga."""
|
| 356 |
import sys
|
| 357 |
|
| 358 |
+
def say(message: str):
|
| 359 |
+
# Al registro del Space: sin esto, un fallo de la instalación no deja rastro (pasó con Python 3.10, que
|
| 360 |
+
# homr 0.7 no acepta).
|
| 361 |
+
print(f"[segundo lector] {message}", flush=True)
|
| 362 |
+
|
| 363 |
+
if sys.version_info < (3, 11):
|
| 364 |
+
_second_state["error"] = f"homr necesita Python 3.11 o más y este servicio corre con {sys.version.split()[0]}."
|
| 365 |
+
say(_second_state["error"])
|
| 366 |
+
else:
|
| 367 |
+
try:
|
| 368 |
+
if not second_reading_available():
|
| 369 |
+
say("instalando homr…")
|
| 370 |
+
done = subprocess.run([sys.executable, "-m", "pip", "install", "--quiet", "homr>=0.7", "onnxruntime>=1.20"],
|
| 371 |
+
capture_output=True, text=True, timeout=900)
|
| 372 |
+
if done.returncode != 0:
|
| 373 |
+
_second_state["error"] = "No se pudo instalar homr: " + "\n".join((done.stdout + done.stderr).splitlines()[-6:])
|
| 374 |
+
if second_reading_available():
|
| 375 |
+
say("bajando sus modelos…")
|
| 376 |
+
_second_state["ready"] = prepare_second_reader()
|
| 377 |
+
if not _second_state["ready"]:
|
| 378 |
+
_second_state["error"] = "homr se instaló pero no pudo bajar sus modelos."
|
| 379 |
+
except Exception as exc:
|
| 380 |
+
_second_state["error"] = f"Falló la preparación de homr: {exc}"
|
| 381 |
+
say("listo" if _second_state["ready"] else (_second_state["error"] or "no quedó disponible"))
|
| 382 |
_second_state["failed"] = not _second_state["ready"]
|
| 383 |
|
| 384 |
|
|
|
|
| 395 |
compás y marca dónde no coinciden."""
|
| 396 |
_check_auth(authorization)
|
| 397 |
if _second_state["failed"]:
|
| 398 |
+
raise HTTPException(status_code=501, detail=f"Este servicio no tiene el segundo lector. {_second_state['error'] or ''}".strip())
|
| 399 |
if not _second_state["ready"]:
|
| 400 |
raise HTTPException(status_code=503, detail="El segundo lector se está preparando; probá de nuevo en un minuto.")
|
| 401 |
if len(files) > SECOND_MAX_IMAGES:
|