Reuse saved routing decisions and allow routing pauses

This commit is contained in:
Sleeps
2026-10-07 12:29:28 +01:00
parent 7c96a6e717
commit a9c58b9608
+46 -8
View File
@@ -231,7 +231,8 @@ def _select_caption_rows(
f""" f"""
SELECT f.id, f.media_id, f.frame_number, f.timestamp_ms, SELECT f.id, f.media_id, f.frame_number, f.timestamp_ms,
f.thumb_path, f.caption, f.caption_model, f.caption_route, f.thumb_path, f.caption, f.caption_model, f.caption_route,
f.route_score, f.route_policy, f.caption_route_reason, f.route_score, f.route_model,
f.route_policy,
m.title, m.source_kind, m.jellyfin_item_id, m.title, m.source_kind, m.jellyfin_item_id,
m.jellyfin_media_source_id, m.jellyfin_tags, m.jellyfin_media_source_id, m.jellyfin_tags,
m.trickplay_width m.trickplay_width
@@ -251,6 +252,7 @@ def _route_rows(
verbose: bool = False, verbose: bool = False,
progress: CaptionProgress | None = None, progress: CaptionProgress | None = None,
frame_source: FrameSource | None = None, frame_source: FrameSource | None = None,
before_batch: Callable[[], None] | None = None,
) -> tuple[list[tuple], int]: ) -> tuple[list[tuple], int]:
own_source = frame_source is None own_source = frame_source is None
source = frame_source or FrameSource() source = frame_source or FrameSource()
@@ -258,9 +260,43 @@ def _route_rows(
try: try:
policy = _route_policy() policy = _route_policy()
reusable = [
(
row,
str(row["caption_route"]),
float(row["route_score"] or 0.0),
str(row["caption_route_reason"] or "saved"),
)
for row in rows
if str(row["route_policy"] or "") == policy
and str(row["caption_route"] or "") in {"general", "specialist"}
]
reusable_ids = {str(item[0]["id"]) for item in reusable}
pending_rows = [
row for row in rows if str(row["id"]) not in reusable_ids
]
if reusable:
print(
f"Reusing {len(reusable)} saved routing decision(s); "
f"{len(pending_rows)} frame(s) need routing."
)
if not pending_rows:
if progress is not None:
progress(
{
"stage": "routed",
"frames_total": len(reusable),
"frames_completed": 0,
"failed": 0,
}
)
return reusable, 0
if settings.caption_router_enabled: if settings.caption_router_enabled:
print( print(
f"Routing {len(rows)} frame(s) with " f"Routing {len(pending_rows)} frame(s) with "
f"{settings.caption_router_model} on {settings.caption_router_device}..." f"{settings.caption_router_model} on {settings.caption_router_device}..."
) )
router = ExplicitContentRouter( router = ExplicitContentRouter(
@@ -271,8 +307,10 @@ def _route_rows(
valid_rows = [] valid_rows = []
score_values: list[float] = [] score_values: list[float] = []
for start in range(0, len(rows), settings.caption_router_batch_size): for start in range(0, len(pending_rows), settings.caption_router_batch_size):
batch_rows = rows[start:start + settings.caption_router_batch_size] if before_batch is not None:
before_batch()
batch_rows = pending_rows[start:start + settings.caption_router_batch_size]
images: list[Image.Image] = [] images: list[Image.Image] = []
image_rows = [] image_rows = []
try: try:
@@ -299,10 +337,10 @@ def _route_rows(
progress( progress(
{ {
"stage": "routing", "stage": "routing",
"frames_total": len(rows), "frames_total": len(pending_rows),
"frames_completed": min( "frames_completed": min(
start + len(batch_rows), start + len(batch_rows),
len(rows), len(pending_rows),
), ),
"failed": missing, "failed": missing,
} }
@@ -331,7 +369,7 @@ def _route_rows(
) )
route_model = settings.caption_router_model route_model = settings.caption_router_model
else: else:
valid_rows = list(rows) valid_rows = list(pending_rows)
scores = {str(row["id"]): 0.0 for row in valid_rows} scores = {str(row["id"]): 0.0 for row in valid_rows}
decisions = { decisions = {
str(row["id"]): ("general", "disabled") str(row["id"]): ("general", "disabled")
@@ -350,7 +388,7 @@ def _route_rows(
if override is not None: if override is not None:
decisions[str(row["id"])] = override decisions[str(row["id"])] = override
routed: list[tuple] = [] routed: list[tuple] = list(reusable)
with connect(settings.db_path) as con: with connect(settings.db_path) as con:
for row in valid_rows: for row in valid_rows:
frame_id = str(row["id"]) frame_id = str(row["id"])