Files
RRFZ 1b41175cad fix(parsing): keep model-download jobs readable on non-UTF-8 Windows
The Docling and MinerU model downloads read their child's output with
text=True and no encoding, so the backend decoded it with the ANSI code
page. Under `deeptutor start` the backend gets PYTHONIOENCODING=utf-8
but not UTF-8 mode, so on a cp936/cp932 server the downloader writes
UTF-8 tqdm bars that the pump cannot decode. The first bar raised
UnicodeDecodeError, the pump stopped reading, the child blocked on the
full pipe and the job stayed "running" with an empty log.

Follow the MinerU parse subprocess: run the downloader with
PYTHONUTF8=1 and decode its output as UTF-8 with errors="replace".
2026-10-31 21:01:18 -04:00

287 lines
11 KiB
Python

"""Offline installation guidance and background model downloads for parsers.
Optional packages must be installed while DeepTutor is stopped, using the
installation path appropriate to the deployment. Only model downloads run as
background subprocesses, with fixed downloader arguments and cursor-based logs.
"""
from __future__ import annotations
import importlib
import logging
import os
from pathlib import Path
import shutil
import subprocess # nosec B404 - argv from fixed allow-lists, never user input
import sys
import threading
import time
from typing import Any, Callable, Optional
from ._versions import package_version
logger = logging.getLogger(__name__)
# Engine id -> pip requirement specifiers. Mirrors the optional extras in
# pyproject.toml. Engines absent here (text_only built-in, mineru external
# CLI/API) have no optional-package installation guidance.
from deeptutor.modules.catalog import manifests
ENGINE_PIP_SPECS: dict[str, list[str]] = {
row["parser_engine"]: list(row["requirements"])
for row in manifests().values()
if "parser_engine" in row
}
# Engine id -> console-script argv that downloads its model weights. The script
# is resolved next to the server's python first (same env), then on PATH.
ENGINE_MODEL_DOWNLOADERS: dict[str, list[str]] = {
"docling": ["docling-tools", "models", "download"],
}
# Buffered log lines kept in memory; older lines are dropped (the cursor
# protocol keeps clients consistent across trims).
_MAX_LINES = 2000
_LINE_MIN_INTERVAL = 0.2
def installable_engines() -> frozenset[str]:
"""Engine ids with supported optional-package installation guidance."""
return frozenset(ENGINE_PIP_SPECS)
def model_downloadable_engines() -> frozenset[str]:
"""Engine ids that support one-click model-weight download."""
return frozenset(ENGINE_MODEL_DOWNLOADERS)
def resolve_model_downloader(engine: str) -> Optional[list[str]]:
"""Resolve the model-download argv for ``engine``.
Locates the console script next to the server's python first (same env so it
matches the installed engine), then falls back to PATH. Returns ``None`` if
the engine has no downloader or the script isn't found.
"""
parts = ENGINE_MODEL_DOWNLOADERS.get(engine)
if not parts:
return None
name = parts[0]
# Let the platform resolver search the active Python environment too. On
# Windows pip installs console scripts as ``<name>.exe`` and ``which``
# applies PATHEXT; probing the extensionless Path directly misses it.
sibling = shutil.which(name, path=str(Path(sys.executable).parent))
if sibling:
return [sibling, *parts[1:]]
found = shutil.which(name)
if found:
return [found, *parts[1:]]
return None
def _invalidate_import_caches() -> None:
"""Make a freshly installed package importable in this process and bust the
cached "" version so readiness flips without a server restart."""
try:
importlib.invalidate_caches()
package_version.cache_clear()
from .markitdown.formats import markitdown_supported_formats
# Docling format discovery is intentionally static and has no cache to
# invalidate: importing its runtime would pull PyTorch's OpenMP runtime
# into the FAISS backend process on macOS.
markitdown_supported_formats.cache_clear()
except Exception: # noqa: BLE001 - best effort
logger.exception("Post-install cache invalidation failed")
class BackgroundJobManager:
"""At most one background subprocess (install or model download), with a
cursor-based line log.
States: ``idle`` → ``running`` → ``done`` / ``failed`` / ``cancelled``.
``status(cursor)`` returns lines after ``cursor`` plus ``next_cursor`` so the
UI can poll incrementally; ``kind`` (``install`` | ``models``) and ``engine``
tell the UI which job/card the log belongs to.
"""
def __init__(self) -> None:
self._lock = threading.Lock()
self._state = "idle"
self._kind = ""
self._engine = ""
self._lines: list[str] = []
self._base = 0
self._message = ""
self._process: subprocess.Popen | None = None
self._cancel_requested = False
def start_install(self, *, engine: str, specs: list[str]) -> dict[str, Any]:
# Host-package changes cannot be safe while turns/API workers run.
# Model downloads remain background jobs because they do not mutate code.
if engine not in ENGINE_PIP_SPECS or specs != ENGINE_PIP_SPECS[engine]:
return {"ok": False, "message": "Unknown parser installation request."}
return {
"ok": False,
"message": (
"Live package installation is disabled. Docker: add parse-"
+ engine
+ " to DEEPTUTOR_EXTRAS in your existing deployment configuration, "
"preserving other extras; recreate the same service with its existing "
"configuration and mounts. Startup reapplies extras; inspect startup "
"logs for installation failures, then recheck availability. "
"Source checkout: stop DeepTutor, run `python scripts/install_extras.py parse-"
+ engine
+ "` from the checkout in the server's Python environment, then restart. "
"Wheel installation: stop DeepTutor, run `deeptutor module install parser-"
+ engine
+ "` in the server's Python environment, then restart. "
"Data mounts preserve user data, not installed Python packages."
),
}
def start_model_download(self, *, engine: str, cmd: list[str]) -> dict[str, Any]:
return self._launch(kind="models", engine=engine, cmd=cmd)
def status(self, cursor: int = 0) -> dict[str, Any]:
with self._lock:
start = max(int(cursor) - self._base, 0)
return {
"state": self._state,
"kind": self._kind,
"engine": self._engine,
"lines": list(self._lines[start:]),
"next_cursor": self._base + len(self._lines),
"message": self._message,
}
def cancel(self) -> dict[str, Any]:
with self._lock:
process = self._process
running = self._state == "running"
if running:
self._cancel_requested = True
if not (running and process):
return {"ok": False, "message": "No job is running."}
if process.poll() is None:
try:
process.terminate()
except Exception as exc: # noqa: BLE001
return {"ok": False, "message": f"Failed to cancel: {exc}"}
return {"ok": True, "message": ""}
# ------------------------------------------------------------------
def _launch(
self,
*,
kind: str,
engine: str,
cmd: list[str],
env: Optional[dict[str, str]] = None,
on_success: Optional[Callable[[], None]] = None,
) -> dict[str, Any]:
with self._lock:
if self._state == "running":
return {
"ok": False,
"message": f"A {self._kind or 'job'} is already running ({self._engine}).",
}
try:
# Downloaders are Python console scripts whose tqdm bars use
# block characters. Have them write UTF-8 and read it as UTF-8:
# an ANSI code page such as cp936 cannot decode those bytes, and
# a dead pump leaves the child blocked on a full pipe.
process = subprocess.Popen( # nosec B603 - argv from fixed allow-lists
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
encoding="utf-8",
errors="replace",
shell=False,
env={**os.environ, **(env or {}), "PYTHONUTF8": "1"},
)
except Exception as exc: # noqa: BLE001
self._state = "failed"
self._kind = kind
self._engine = engine
self._message = f"Failed to launch: {exc}"
return {"ok": False, "message": self._message}
self._state = "running"
self._kind = kind
self._engine = engine
self._lines = []
self._base = 0
self._message = ""
self._process = process
self._cancel_requested = False
thread = threading.Thread(
target=self._pump, args=(process, kind, engine, on_success), daemon=True
)
thread.start()
logger.info("Parser %s job started (%s): %s", kind, engine, " ".join(cmd))
return {"ok": True, "message": ""}
def _pump(
self,
process: subprocess.Popen,
kind: str,
engine: str,
on_success: Optional[Callable[[], None]],
) -> None:
last_emit = 0.0
try:
assert process.stdout is not None
for raw_line in process.stdout:
line = raw_line.strip()
if not line:
continue
now = time.monotonic()
if now - last_emit < _LINE_MIN_INTERVAL:
continue
last_emit = now
self._append(line[:300])
except Exception:
logger.exception("Background job output pump failed")
returncode = process.wait()
with self._lock:
if self._cancel_requested:
self._state = "cancelled"
self._message = "Cancelled."
elif returncode == 0:
self._state = "done"
self._message = "Finished."
else:
self._state = "failed"
self._message = f"Exited with code {returncode}."
self._process = None
if returncode == 0 and not self._cancel_requested and on_success is not None:
on_success()
logger.info("Parser %s job finished (%s): %s", kind, engine, self._state)
def _append(self, line: str) -> None:
with self._lock:
self._lines.append(line)
overflow = len(self._lines) - _MAX_LINES
if overflow > 0:
del self._lines[:overflow]
self._base += overflow
_manager = BackgroundJobManager()
def get_background_job_manager() -> BackgroundJobManager:
return _manager
__all__ = [
"ENGINE_MODEL_DOWNLOADERS",
"ENGINE_PIP_SPECS",
"BackgroundJobManager",
"get_background_job_manager",
"installable_engines",
"model_downloadable_engines",
"resolve_model_downloader",
]