diff --git a/scene_search/cli.py b/scene_search/cli.py index 92a2983..01ab61d 100644 --- a/scene_search/cli.py +++ b/scene_search/cli.py @@ -231,7 +231,8 @@ def _select_caption_rows( f""" SELECT f.id, f.media_id, f.frame_number, f.timestamp_ms, 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.jellyfin_media_source_id, m.jellyfin_tags, m.trickplay_width @@ -251,6 +252,7 @@ def _route_rows( verbose: bool = False, progress: CaptionProgress | None = None, frame_source: FrameSource | None = None, + before_batch: Callable[[], None] | None = None, ) -> tuple[list[tuple], int]: own_source = frame_source is None source = frame_source or FrameSource() @@ -258,9 +260,43 @@ def _route_rows( try: 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: 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}..." ) router = ExplicitContentRouter( @@ -271,8 +307,10 @@ def _route_rows( valid_rows = [] score_values: list[float] = [] - for start in range(0, len(rows), settings.caption_router_batch_size): - batch_rows = rows[start:start + settings.caption_router_batch_size] + for start in range(0, len(pending_rows), 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] = [] image_rows = [] try: @@ -299,10 +337,10 @@ def _route_rows( progress( { "stage": "routing", - "frames_total": len(rows), + "frames_total": len(pending_rows), "frames_completed": min( start + len(batch_rows), - len(rows), + len(pending_rows), ), "failed": missing, } @@ -331,7 +369,7 @@ def _route_rows( ) route_model = settings.caption_router_model else: - valid_rows = list(rows) + valid_rows = list(pending_rows) scores = {str(row["id"]): 0.0 for row in valid_rows} decisions = { str(row["id"]): ("general", "disabled") @@ -350,7 +388,7 @@ def _route_rows( if override is not None: decisions[str(row["id"])] = override - routed: list[tuple] = [] + routed: list[tuple] = list(reusable) with connect(settings.db_path) as con: for row in valid_rows: frame_id = str(row["id"])