From 6865eb6e47b82c26ed088483bc3add2bccb1fc2a Mon Sep 17 00:00:00 2001 From: Sleeps Date: Wed, 7 Oct 2026 11:42:30 +0100 Subject: [PATCH] Expose resumable caption jobs over API --- scene_search/api.py | 68 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 7 deletions(-) diff --git a/scene_search/api.py b/scene_search/api.py index 06659bf..c6a8d28 100644 --- a/scene_search/api.py +++ b/scene_search/api.py @@ -1,15 +1,18 @@ from __future__ import annotations import hmac +import threading from pathlib import Path from fastapi import Depends, FastAPI, Header, HTTPException, Query from fastapi.responses import FileResponse from pydantic import BaseModel, Field +from .caption_jobs import CaptionJobManager from .config import settings from .database import connect, init_db from .index_jobs import IndexJobManager +from .paths import resolve_thumb_path from .search_service import SearchService WEB_INDEX_PATH = Path(__file__).parent / "web" / "index.html" @@ -21,6 +24,8 @@ app = FastAPI( _service: SearchService | None = None _index_jobs: IndexJobManager | None = None +_caption_jobs: CaptionJobManager | None = None +_job_start_lock = threading.Lock() class IndexRequest(BaseModel): @@ -28,6 +33,11 @@ class IndexRequest(BaseModel): force: bool = False +class CaptionRequest(BaseModel): + library_ids: list[str] = Field(min_length=1) + force: bool = False + + def get_search_service() -> SearchService: global _service if _service is None: @@ -50,6 +60,13 @@ def get_index_jobs() -> IndexJobManager: return _index_jobs +def get_caption_jobs() -> CaptionJobManager: + global _caption_jobs + if _caption_jobs is None: + _caption_jobs = CaptionJobManager(get_search_service()) + return _caption_jobs + + def require_api_key( authorization: str | None = Header(default=None), x_scene_search_key: str | None = Header(default=None), @@ -82,10 +99,10 @@ def search( scene_limit: int = Query(default=50, ge=1, le=100), item_limit: int = Query(default=20, ge=1, le=50), ) -> dict: - if get_index_jobs().running: + if get_index_jobs().running or get_caption_jobs().gpu_busy: raise HTTPException( status_code=503, - detail="Jellyfin indexing is currently using the embedding model", + detail="A background media job is currently using the local GPU", ) try: @@ -119,10 +136,15 @@ def jellyfin_libraries() -> dict: ) def jellyfin_index(request: IndexRequest) -> dict: try: - return get_index_jobs().start( - request.library_ids, - force=request.force, - ) + with _job_start_lock: + if get_caption_jobs().running: + raise RuntimeError( + "Captioning is currently running. Wait for it to finish before indexing." + ) + return get_index_jobs().start( + request.library_ids, + force=request.force, + ) except RuntimeError as exc: message = str(exc) status = 409 if "already running" in message else 400 @@ -139,6 +161,38 @@ def jellyfin_index_status() -> dict: return get_index_jobs().status() +@app.post( + "/api/caption", + dependencies=[Depends(require_api_key)], + status_code=202, +) +def caption_start(request: CaptionRequest) -> dict: + try: + with _job_start_lock: + if get_index_jobs().running: + raise RuntimeError( + "Jellyfin indexing is currently running. Wait for it to finish before captioning." + ) + return get_caption_jobs().start( + request.library_ids, + force=request.force, + ) + except RuntimeError as exc: + message = str(exc) + status = 409 if "running" in message.lower() else 400 + raise HTTPException(status_code=status, detail=message) from exc + except Exception as exc: + raise HTTPException(status_code=503, detail=str(exc)) from exc + + +@app.get( + "/api/caption/status", + dependencies=[Depends(require_api_key)], +) +def caption_status() -> dict: + return get_caption_jobs().status() + + @app.get( "/api/frames/{frame_id}/thumbnail", dependencies=[Depends(require_api_key)], @@ -159,7 +213,7 @@ def frame_thumbnail(frame_id: str) -> FileResponse: if row is None: raise HTTPException(status_code=404, detail="Frame not found") - path = Path(str(row["thumb_path"])) + path = resolve_thumb_path(str(row["thumb_path"])) if not path.is_file(): raise HTTPException(status_code=404, detail="Thumbnail file not found")