Fix scoring of very short clips
Browse filesFiles changed (2)
1234567891011121314151617181920212223242 unmodified lines27282930313218 unmodified lines51525354555616 unmodified lines737475767778"""Adapter for Acliptic v5 (acliptic/ranker5.py): sound fingerprints + speech meaning + loudness + talk cues, judgedin context. Needs no PyTorch and no separate text model (only ONNX Runtime and scikit-learn).models.json: {"type": "moment_ranker5", "path": "<dir with ranker5.pkl>", "version": "5.0"}Without audio, the sound and loudness inputs are missing and the ranker works from the transcript alone."""from __future__ import annotationsimport numpy as npimport pandas as pdfrom .base import ModelInfo, ModelProvider, ModelUnavailable, ScoreRequest, SegmentScoreCHUNK_SEGS = 20 # decode 10 minutes at a timeclass MomentRanker5Provider(ModelProvider):def __init__(self, id_: str, path: str, version: str, display_name: str = "", **_):self.id, self.path, self.version = id_, path, versionself.display_name = display_name or f"Acliptic v{version}"self._ranker = Nonedef info(self) -> ModelInfo:return ModelInfo(id=self.id, version=self.version, display_name=self.display_name, kind="real",2 unmodified lines"from real streams.")def _load(self):if self._ranker is None:try:from ..ranker5 import Ranker518 unmodified linesreturn pd.concat(louds, ignore_index=True), np.concatenate(embs)def signals(self, req: ScoreRequest):from .. import config, soundfrom ..ranker5 import text_embeddingstexts = [s.text for s in req.segments]16 unmodified lines# ---- live: one chunk at a time, ranked against the past only ----def live_score(self, f, dur, text, has_audio, state: dict) -> float:from .. import config, soundfrom ..ranker import SR, loudness_onefrom ..ranker5 import text_embeddings123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354552 unmodified lines5859606162636418 unmodified lines8384858687888916 unmodified lines106107108109110111112"""Adapter for Acliptic v5 (acliptic/ranker5.py): sound fingerprints + speech meaning + loudness + talk cues, judgedin context. Needs no PyTorch and no separate text model (only ONNX Runtime and scikit-learn).models.json: {"type": "moment_ranker5", "path": "<dir with ranker5.pkl>", "version": "5.0","hf_repo": "<owner/name>", "hf_revision": "<commit>"}With hf_repo the ranker, bge, CLAP and Whisper come from that Hugging Face repo, and `path` is only a fallback.Without audio, the sound and loudness inputs are missing and the ranker works from the transcript alone."""from __future__ import annotationsimport loggingfrom pathlib import Pathimport numpy as npimport pandas as pdfrom .base import ModelInfo, ModelProvider, ModelUnavailable, ScoreRequest, SegmentScoreCHUNK_SEGS = 20 # decode 10 minutes at a timelog = logging.getLogger("acliptic.v5")class MomentRanker5Provider(ModelProvider):def __init__(self, id_: str, path: str, version: str, display_name: str = "", hf_repo: str | None = None,hf_revision: str | None = None, **_):self.id, self.path, self.version = id_, path, versionself.display_name = display_name or f"Acliptic v{version}"self.hf_repo, self.hf_revision = hf_repo, hf_revisionself.source = "local files" if not hf_repo else "not loaded yet"self._ranker = Noneself._ready = not hf_repodef prepare(self):"""Fetch the Hugging Face bundle and point the app's sound, text and speech models at it."""if self._ready:returnfrom .. import config, hubtry:d = hub.fetch(self.hf_repo, self.hf_revision)except Exception as e:if not (Path(self.path) / "ranker5.pkl").exists():self.source = "unavailable"raise ModelUnavailable(f"{self.display_name}: couldn't load {self.hf_repo} from Hugging Face ({type(e).__name__})") from elog.warning("Couldn't reach %s (%s); using the local copy", self.hf_repo, type(e).__name__)self.source = "local copy (Hugging Face unavailable)"else:self.path = str(d)config.ASR_MODEL = d / "faster-whisper-base.en"config.CLAP_ONNX = d / "clap-onnx"config.TEXT_EMB_ONNX = d / "bge-onnx"self.source = f"Hugging Face: {self.hf_repo}"self._ready = Truedef info(self) -> ModelInfo:return ModelInfo(id=self.id, version=self.version, display_name=self.display_name, kind="real",2 unmodified lines"from real streams.")def _load(self):self.prepare()if self._ranker is None:try:from ..ranker5 import Ranker518 unmodified linesreturn pd.concat(louds, ignore_index=True), np.concatenate(embs)def signals(self, req: ScoreRequest):self.prepare()from .. import config, soundfrom ..ranker5 import text_embeddingstexts = [s.text for s in req.segments]16 unmodified lines# ---- live: one chunk at a time, ranked against the past only ----def live_score(self, f, dur, text, has_audio, state: dict) -> float:self.prepare()from .. import config, soundfrom ..ranker import SR, loudness_onefrom ..ranker5 import text_embeddings
67 unmodified lines68697071727374757667 unmodified lines"""x moved k segments (k > 0: the value k segments earlier), edges filled with the nearest value."""if k == 0:return xif k > 0:return np.r_[np.repeat(x[:1], k), x[:-k]]return np.r_[x[-k:], np.repeat(x[-1:], -k)]def featurize5(raw: pd.DataFrame, clap: np.ndarray | None, temb: np.ndarray | None, proj: dict, causal: bool = False) -> pd.DataFrame:67 unmodified lines6869707172737475767767 unmodified lines"""x moved k segments (k > 0: the value k segments earlier), edges filled with the nearest value."""if k == 0:return xn = len(x) # a clip shorter than the shift is all edgeif k > 0:return np.r_[np.repeat(x[:1], min(k, n)), x[:-k]]return np.r_[x[-k:], np.repeat(x[-1:], min(-k, n))]def featurize5(raw: pd.DataFrame, clap: np.ndarray | None, temb: np.ndarray | None, proj: dict, causal: bool = False) -> pd.DataFrame:
acliptic/models/moment_ranker5.py+36-2
acliptic/ranker5.py+3-2