"""The model-provider contract. Every model (dev sample, v2, v3, anything later) is an adapter implementing `ModelProvider`. The pipeline only ever talks to this interface. Input (ScoreRequest) segments consecutive transcript chunks of the video, ~30 s each: index, start, end (seconds), text media facts about the source: duration (s), has_audio, language, and the file path for models that listen Output (ScoreResponse) scores one SegmentScore per input segment, same order: index + score in [0, 1] (higher = more likely worth clipping). A ranking signal, not a probability of going viral. model_id, model_version, kind ("real" | "development"), latency_ms Adapters declare what they can use in `capabilities` ({"text"}, later e.g. {"text", "audio"}); the runner refuses a request the adapter can't serve (UnsupportedCapability), enforces a timeout (ModelTimeout) and validates the output (ModelError) so a bad model never writes bad data. """ from __future__ import annotations from abc import ABC, abstractmethod from dataclasses import dataclass, field class ModelError(Exception): """The model ran but returned something unusable, or failed.""" class ModelUnavailable(ModelError): """The model can't be loaded (missing files, bad config).""" class ModelTimeout(ModelError): """The model took longer than the configured timeout.""" class UnsupportedCapability(ModelError): """The request needs an input this model doesn't support.""" @dataclass class Segment: index: int start: float end: float text: str @dataclass class MediaInfo: duration: float has_audio: bool language: str = "en" path: str | None = None # the media file, for models that listen (capability "audio") live: bool = False # scoring a stream as it happens: only the past may be used @dataclass class ScoreRequest: segments: list[Segment] media: MediaInfo needs: set[str] = field(default_factory=lambda: {"text"}) @dataclass class SegmentScore: index: int score: float @dataclass class ModelInfo: id: str version: str display_name: str kind: str # "real" | "development" capabilities: set[str] # e.g. {"text"} min_score: float = 0.0 # segments below this are never shortlisted ranks_moments: bool = False # scores already combine every signal; the shortlist uses them as the ranking notes: str = "" @dataclass class ScoreResponse: scores: list[SegmentScore] model_id: str model_version: str kind: str latency_ms: int = 0 class ModelProvider(ABC): @abstractmethod def info(self) -> ModelInfo: ... @abstractmethod def score(self, req: ScoreRequest) -> list[SegmentScore]: """Return one score per request segment. Raise ModelError on failure."""