Add resumable background caption jobs

This commit is contained in:
Sleeps
2026-10-07 11:42:09 +01:00
parent 6a5b260873
commit c25f986bda
+299
View File
@@ -0,0 +1,299 @@
from __future__ import annotations
import threading
from copy import deepcopy
from datetime import datetime, timezone
from types import SimpleNamespace
from typing import Any
from .captioner import ensure_caption_prompt_file, ensure_specialist_prompt_file
from .cli import (
_caption_queue,
_route_rows,
_select_caption_rows,
_unload_ollama_model,
)
from .config import settings
from .database import connect, init_db
from .search_service import SearchService
from .sync import rebuild_meili
class CaptionJobManager:
"""Runs one resumable caption job at a time for indexed Jellyfin frames."""
def __init__(self, search_service: SearchService) -> None:
self.search_service = search_service
self._state_lock = threading.Lock()
self._thread: threading.Thread | None = None
self._gpu_busy = False
self._state: dict[str, Any] = {
"running": False,
"stage": "idle",
"started_at": None,
"finished_at": None,
"error": None,
"frames_total": 0,
"frames_completed": 0,
"failed": 0,
"retained": 0,
"percent": 0.0,
}
@property
def running(self) -> bool:
with self._state_lock:
return bool(self._state.get("running"))
@property
def gpu_busy(self) -> bool:
with self._state_lock:
return self._gpu_busy
def status(self) -> dict[str, Any]:
with self._state_lock:
return deepcopy(self._state)
def start(self, library_ids: list[str], *, force: bool = False) -> dict[str, Any]:
library_ids = [str(value).strip() for value in library_ids if str(value).strip()]
if not library_ids:
raise RuntimeError("Select at least one Jellyfin library.")
with self._state_lock:
if self._state.get("running"):
raise RuntimeError("A captioning job is already running.")
init_db(settings.db_path)
args = SimpleNamespace(
force=force,
media_id=None,
title=None,
limit=None,
library_ids=library_ids,
)
rows = _select_caption_rows(args, only_needing_caption=True)
with connect(settings.db_path) as con:
placeholders = ",".join("?" for _ in library_ids)
totals = con.execute(
f"""
SELECT COUNT(*) AS total,
SUM(CASE WHEN f.caption IS NOT NULL THEN 1 ELSE 0 END) AS captioned
FROM frames f
JOIN media m ON m.id = f.media_id
WHERE m.source_kind = 'jellyfin'
AND m.jellyfin_library_id IN ({placeholders})
""",
library_ids,
).fetchone()
indexed_frames = int(totals["total"] or 0)
already_captioned = int(totals["captioned"] or 0)
with self._state_lock:
self._state = {
"running": bool(rows),
"stage": "starting" if rows else "complete",
"started_at": datetime.now(timezone.utc).isoformat(),
"finished_at": None if rows else datetime.now(timezone.utc).isoformat(),
"error": None,
"library_ids": library_ids,
"force": force,
"indexed_frames": indexed_frames,
"already_captioned": already_captioned,
"frames_selected": len(rows),
"frames_total": len(rows),
"frames_completed": 0,
"failed": 0,
"retained": 0,
"percent": 0.0 if rows else 100.0,
}
if not rows:
return self.status()
self._thread = threading.Thread(
target=self._run,
args=(args, rows),
name="scene-search-caption",
daemon=True,
)
self._thread.start()
return self.status()
def _progress(self, update: dict[str, Any]) -> None:
with self._state_lock:
self._state.update(update)
stage = str(self._state.get("stage") or "")
total = int(self._state.get("frames_total") or 0)
completed = int(self._state.get("frames_completed") or 0)
if stage == "complete":
self._state["percent"] = 100.0
elif stage in {"sync", "syncing", "synced"}:
self._state["percent"] = 99.0
elif total > 0:
self._state["percent"] = round(
min(max(completed / total, 0.0), 1.0) * 99.0,
1,
)
else:
self._state["percent"] = 0.0
def _set_gpu_busy(self, value: bool) -> None:
with self._state_lock:
self._gpu_busy = value
def _run(self, args: SimpleNamespace, rows: list) -> None:
missing = 0
try:
self._progress({"stage": "routing"})
# The router uses the local GPU. Block SigLIP searches/indexing only
# for this stage; the actual VLM caption requests run on Ollama.
self._set_gpu_busy(True)
try:
with self.search_service.model_lock:
routed, missing = _route_rows(
rows,
progress=self._progress,
)
finally:
self._set_gpu_busy(False)
if not routed:
self._progress(
{
"running": False,
"stage": "complete",
"finished_at": datetime.now(timezone.utc).isoformat(),
"frames_total": 0,
"frames_completed": 0,
"failed": missing,
}
)
return
general_prompt = ensure_caption_prompt_file(settings.caption_prompt_file)
specialist_prompt = ensure_specialist_prompt_file(
settings.caption_specialist_prompt_file
)
general_rows = [
item
for item in routed
if item[1] == "general"
and (
args.force
or item[0]["caption"] is None
or item[0]["caption_model"] != settings.caption_general_model
)
]
specialist_rows = [
item
for item in routed
if item[1] == "specialist"
and (
args.force
or item[0]["caption"] is None
or item[0]["caption_model"] != settings.caption_specialist_model
)
]
retained = len(routed) - len(general_rows) - len(specialist_rows)
frames_total = len(general_rows) + len(specialist_rows)
self._progress(
{
"stage": "captioning",
"frames_total": frames_total,
"frames_completed": 0,
"failed": missing,
"retained": retained,
"general_total": len(general_rows),
"specialist_total": len(specialist_rows),
}
)
if general_rows:
_unload_ollama_model(settings.caption_specialist_model)
def general_progress(update: dict) -> None:
adjusted = dict(update)
adjusted["failed"] = missing + int(update.get("failed") or 0)
self._progress(adjusted)
general_done, general_failed = _caption_queue(
"General queue",
general_rows,
settings.caption_general_model,
general_prompt,
progress=general_progress,
total_frames=frames_total,
)
if specialist_rows:
_unload_ollama_model(settings.caption_general_model)
def specialist_progress(update: dict) -> None:
adjusted = dict(update)
adjusted["failed"] = (
missing
+ general_failed
+ int(update.get("failed") or 0)
)
self._progress(adjusted)
specialist_done, specialist_failed = _caption_queue(
"Specialist queue",
specialist_rows,
settings.caption_specialist_model,
specialist_prompt,
progress=specialist_progress,
completed_offset=general_done,
total_frames=frames_total,
)
completed = general_done + specialist_done
failed = missing + general_failed + specialist_failed
self._progress(
{
"stage": "syncing",
"frames_completed": completed,
"failed": failed,
}
)
def sync_progress(update: dict) -> None:
self._progress(
{
**update,
"stage": "syncing",
"frames_completed": completed,
"failed": failed,
}
)
documents = rebuild_meili(progress=sync_progress)
self._progress(
{
"running": False,
"stage": "complete",
"finished_at": datetime.now(timezone.utc).isoformat(),
"frames_completed": completed,
"failed": failed,
"documents_processed": documents,
}
)
except Exception as exc:
self._set_gpu_busy(False)
self._progress(
{
"running": False,
"stage": "failed",
"finished_at": datetime.now(timezone.utc).isoformat(),
"error": str(exc),
"failed": max(int(self._state.get("failed") or 0), missing),
}
)