Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
94 lines
2.9 KiB
Python
94 lines
2.9 KiB
Python
"""Pure highlight-selection policy (no DB, no LLM, no IO).
|
|
|
|
Given scored candidate windows (from the ranker), select the best short-form
|
|
clips: within the duration band, above a score floor, non-overlapping, highest
|
|
score first, capped at N. Kept side-effect free so the core product logic is
|
|
exhaustively unit-testable and deterministic.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
# Sensible defaults for vertical short-form; overridable per request.
|
|
DEFAULT_MIN_LEN_S = 15
|
|
DEFAULT_MAX_LEN_S = 60
|
|
DEFAULT_MAX_CLIPS = 5
|
|
DEFAULT_MIN_SCORE = 50
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Candidate:
|
|
"""A scored transcript window proposed by the ranker."""
|
|
|
|
start_s: int
|
|
end_s: int
|
|
score: int
|
|
title: str = ""
|
|
hook: str = ""
|
|
transcript: str = ""
|
|
reason: str = ""
|
|
|
|
@property
|
|
def length_s(self) -> int:
|
|
return max(0, int(self.end_s) - int(self.start_s))
|
|
|
|
|
|
def _overlaps(a: Candidate, b: Candidate) -> bool:
|
|
return int(a.start_s) < int(b.end_s) and int(b.start_s) < int(a.end_s)
|
|
|
|
|
|
def _bounded(value: int, minimum: int, maximum: int) -> int:
|
|
return max(minimum, min(int(value), maximum))
|
|
|
|
|
|
def select_highlights(
|
|
candidates: list[Candidate],
|
|
*,
|
|
max_clips: int = DEFAULT_MAX_CLIPS,
|
|
min_len_s: int = DEFAULT_MIN_LEN_S,
|
|
max_len_s: int = DEFAULT_MAX_LEN_S,
|
|
min_score: int = DEFAULT_MIN_SCORE,
|
|
) -> list[Candidate]:
|
|
"""Return the selected clips, best score first, non-overlapping.
|
|
|
|
A candidate qualifies when its length is within ``[min_len_s, max_len_s]``
|
|
and its score is at least ``min_score``. Qualified candidates are considered
|
|
highest-score-first (ties broken by earlier start, then shorter length) and
|
|
greedily selected while they do not overlap an already-selected clip, up to
|
|
``max_clips``.
|
|
"""
|
|
max_clips = _bounded(max_clips, 1, 50)
|
|
min_len_s = max(1, int(min_len_s))
|
|
max_len_s = max(min_len_s, int(max_len_s))
|
|
min_score = max(0, int(min_score))
|
|
|
|
qualified = [
|
|
c
|
|
for c in candidates
|
|
if min_len_s <= c.length_s <= max_len_s and int(c.score) >= min_score
|
|
]
|
|
qualified.sort(key=lambda c: (-int(c.score), int(c.start_s), c.length_s))
|
|
|
|
selected: list[Candidate] = []
|
|
for cand in qualified:
|
|
if len(selected) >= max_clips:
|
|
break
|
|
if any(_overlaps(cand, s) for s in selected):
|
|
continue
|
|
selected.append(cand)
|
|
return selected
|
|
|
|
|
|
def candidate_from_dict(row: dict) -> Candidate:
|
|
"""Coerce a ranker/transcript dict into a :class:`Candidate` (defensive)."""
|
|
return Candidate(
|
|
start_s=int(row.get("start_s", 0) or 0),
|
|
end_s=int(row.get("end_s", 0) or 0),
|
|
score=_bounded(row.get("score", 0) or 0, 0, 100),
|
|
title=str(row.get("title", "") or ""),
|
|
hook=str(row.get("hook", "") or ""),
|
|
transcript=str(row.get("transcript", row.get("text", "")) or ""),
|
|
reason=str(row.get("reason", "") or ""),
|
|
)
|