clipwright/gemini_worker.py
juancarlos.cavero 19531c5b09 feat(selection): require clips to stand alone without sacrificing the payoff
Measured with a blind judge comparing two candidates from the same video,
positions alternated and durations matched. Baseline first: the current
pipeline beats a random window of the same length 82% of the time over 60
comparisons, so selection works and the room is in the remaining 18%.

The judge's stated reasons kept naming something the prompt never asked for:
whether the moment stands on its own. Two versions of that rule were tested.

  v1  "must make sense to someone who has seen nothing else"
      19 vs 20, 49%, p = 1.000  -> nothing
  v2  v1 plus "fix it by moving the START earlier, never by cutting the
      ending short"
      run 1  27 vs 11, 71%, p = 0.014
      run 2  18 vs 13, 58%, p = 0.473   (replication, different seed)
      pooled 45 vs 24, 65%, p = 0.015   -> adopted

Read the replication honestly: it did NOT reproduce 71% on its own. Both runs
point the same way and pooled they clear the bar, but the effect is smaller
than the first run suggested. If the true rate were 71%, seeing 18/31 or worse
has p = 0.085, so this is a weaker effect rather than a contradiction. Adopted
because it is free — no fewer clips, no extra call — and revisit it if another
selection change ever needs the same slot.

The gap between v1 and v2 is the useful finding. Asking for self-containment
alone is inert because the model buys context by delaying the start and pays
for it with the payoff, and the two cancel out; v1's losses were literally "B
ends abruptly in mid-sentence" and "A includes the final call to action".

Median overlap between the two selections was 37-42%, which is what makes the
v1 result meaningful: the prompts really do pick different seconds, so 49%
means "no better", not "no different".

Harnesses kept in the reframe-testing skill (selection_ab.py, prompt_ab.py).
2026-07-31 21:50:07 +02:00

535 lines
20 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import argparse
import json
import os
import sys
from typing import List, Optional
from dotenv import load_dotenv
from google import genai
from google.genai import types as genai_types
from pydantic import BaseModel
from clip_selection import lookup_model_prices
load_dotenv()
# --- Structured output schemas (passed as response_schema so the API
# --- guarantees the format instead of us repairing free-form JSON). ---
class ScoredWindowModel(BaseModel):
id: str
start: float
end: float
score: int
reason: str
class ScoreResponse(BaseModel):
windows: List[ScoredWindowModel]
class DetailClipModel(BaseModel):
start: float
end: float
source_window_id: str
predicted_score: int
video_description_for_tiktok: str
video_description_for_instagram: str
video_title_for_youtube_short: str
viral_hook_text: str
class DetailResponse(BaseModel):
shorts: List[DetailClipModel]
# Visual (no-transcript) clip selection: Gemini watches a silent video and
# picks moments from the imagery. Same output shape as DetailClipModel minus
# the transcript-only source_window_id.
class VisualClipModel(BaseModel):
start: float
end: float
predicted_score: int
video_description_for_tiktok: str
video_description_for_instagram: str
video_title_for_youtube_short: str
viral_hook_text: str
class VisualResponse(BaseModel):
shorts: List[VisualClipModel]
VISUAL_PROMPT_TEMPLATE = """
You are a senior short-form video editor. This video has NO speech/audio — judge
it purely by what you SEE. Watch the whole thing and pick the 3–15 MOST engaging
visual moments for TikTok / Reels / Shorts (action, reveals, transformations,
striking or funny shots, satisfying payoffs, dramatic movement).
TIME CONTRACT — STRICT:
- Timestamps in ABSOLUTE SECONDS from the start (usable with ffmpeg -ss/-to).
- Only numbers with up to 3 decimals (e.g. 0, 12.5, 47.250).
- 0 <= start < end <= {video_duration}.
- Each clip 15 to 60 seconds long. If the whole video is shorter than 15s,
return one clip spanning the full video.
- Cut on visual scene changes, never mid-motion.
For each clip write catchy copy in {language} (a scroll-stopping hook, a TikTok
and an Instagram description, and a YouTube title ≤100 chars). Order clips best
to worst by how likely they are to stop a viewer scrolling.
"""
class LayoutChoice(BaseModel):
layout: str
confidence: float
why: str
# Scored 94/92/96% over the 48-clip corpus against hand-checked labels, with
# 0-1 false positives out of the 28 clips that must not be touched. Do not
# reword casually: the wins come from the explicit "none is usually right"
# instruction and from naming the exact decorations (corner bugs, score
# counters, subtitles) that four earlier attempts kept mistaking for content.
LAYOUT_CHOICE_PROMPT = """
These frames are sampled at regular intervals from a single landscape video.
You are choosing how to re-frame that video into a vertical 9:16 clip.
Pick ONE layout:
- "none": crop to the speaker and fill the frame. This is the RIGHT answer for
ordinary talking heads, interviews shot in close-up, b-roll, sport, action,
music, and any footage whose meaning survives a centre crop. Corner logos,
score bugs, subscriber counters, lower-thirds and burned-in subtitles do NOT
change this: they are decoration, and losing them costs nothing.
- "screencast": keep the screen. ONLY when the video is built around a screen
recording, slides, a spreadsheet, a chart or a map that the viewer must read
to follow it. If you cannot read words or numbers off the screen that matter
to the point being made, it is not this.
(A "camera_inset" option was added here and removed on 31-jul-2026. Whether a
webcam is composited into a corner of that screen is not something the model
can see: on the five clips that have one it answered "screencast" every time,
in both runs, while overall accuracy fell from 92% to 83-85%. camera_inset.py
finds the same five geometrically with no false positives, so that question is
answered downstream instead of being asked here.)
- "split": stack two people. ONLY when two people are visible IN THE SAME SHOT
at the same time in most frames, talking to each other. Frames that alternate
between one-person close-ups are NOT this, however many people appear.
"none" is by far the most common correct answer. Choose anything else only if
you would defend it to an editor. If you are unsure, answer "none".
confidence is 0..1. why is at most 12 words.
"""
class WideContentRangeModel(BaseModel):
start: float
end: float
what: str
width_fraction: float
class WideContentResponse(BaseModel):
ranges: List[WideContentRangeModel]
WIDE_CONTENT_PROMPT_TEMPLATE = """
You are preparing a landscape video to be re-framed to a vertical 9:16 crop.
The crop keeps a tall centre strip and THROWS AWAY the left and right sides.
List every time range where on-screen content would be cut by that, and for each
one report HOW MUCH OF THE FRAME WIDTH the content spans.
width_fraction is the single most important field. Measure the content's own
horizontal extent, from its left edge to its right edge, as a fraction of the
full frame width:
- a spreadsheet, slide, screen recording or map filling the picture: 0.9 - 1.0
- a chart or diagram beside a speaker: 0.4 - 0.7
- a lower-third or headline strip across the bottom: 0.6 - 0.9
- a logo, channel bug, score counter or subscriber count in a corner: 0.1 - 0.2
- subtitles centred at the bottom: 0.3 - 0.5
Report what you actually see. Do NOT inflate the number to make a range seem
worth reporting, and do NOT leave out corner graphics — report them with their
true small width_fraction. A range reported honestly at 0.15 is useful; the same
range reported at 0.9 makes the video worse.
COUNT a range when the frame shows:
- a screen recording, slide, spreadsheet, chart, graph or map
- headlines, labels, statistics or comparison tables burned into the picture
- a side-by-side or split-screen layout
- any diagram or product shot where the edges carry the meaning
DO NOT count an ordinary talking head, even against a busy background, and do
not count b-roll, landscapes, crowds or action footage with no graphics.
TIME CONTRACT — STRICT:
- ABSOLUTE SECONDS from the start, numbers only, up to 3 decimals.
- 0 <= start < end <= {video_duration}.
- Merge ranges that are less than 1 second apart.
- Return an EMPTY list if the video never shows such content. An empty list is
the correct, expected answer for most talking-head and b-roll videos — do not
invent ranges to seem useful.
For "what", name the content in three words or fewer (e.g. "stock chart",
"spreadsheet", "corner ticker").
"""
def _configure_stdio() -> None:
for stream_name in ("stdout", "stderr"):
stream = getattr(sys, stream_name, None)
if not stream or not hasattr(stream, "reconfigure"):
continue
try:
stream.reconfigure(encoding="utf-8", errors="replace")
except Exception:
pass
def _log(message: str) -> None:
stream = sys.stdout
text = str(message)
try:
stream.write(text + "\n")
except UnicodeEncodeError:
encoding = getattr(stream, "encoding", None) or "utf-8"
safe_text = text.encode(encoding, errors="replace").decode(encoding, errors="replace")
stream.write(safe_text + "\n")
stream.flush()
SCORE_PROMPT_TEMPLATE = """
You are a senior short-form video strategist.
Select the MOST viral candidate windows from this batch.
Rules:
- Return only valid JSON.
- Choose up to 3 windows from this batch.
- `score` must be an integer from 0 to 100.
- THE 2-SECOND TEST is the main criterion: would the first 2 seconds of this
moment force a cold viewer (no context) to keep watching? Windows that only
work with prior context score low.
- Prefer windows with strong hooks, conflict, surprise, outrage, emotion,
novelty, big numbers, or a clear payoff.
- Ignore weak filler, housekeeping, outros, rambling transitions, and
low-signal padding unless there is an obvious hook or payoff.
TRANSCRIPT_LANGUAGE: {language}
VIDEO_DURATION_SECONDS: {video_duration}
WINDOWS_JSON:
{windows_json}
Return only:
{{
"windows": [
{{
"id": "<window id>",
"start": <number>,
"end": <number>,
"score": <integer 0-100>,
"reason": "<very short reason>"
}}
]
}}
"""
DETAIL_PROMPT_TEMPLATE = """
You are a senior short-form video editor and viral copywriter.
Choose the BEST short clips from these shortlisted candidate windows.
CLIP RULES:
- Return only valid JSON.
- Each clip must be 15 to 60 seconds long, in absolute seconds from the start of the source video.
- Stay within the candidate window boundaries.
- THE 2-SECOND RULE: the clip MUST open on its strongest moment. If the first
2 seconds would not stop a cold viewer from scrolling, move the start or skip the clip.
- Start slightly before the hook and end slightly after the payoff when possible.
- Do not cut in the middle of a word or phrase.
- No generic intros/outros unless they are the hook.
- STANDS ALONE: the clip must make sense to someone who has seen nothing else.
If it opens on a pronoun, a "that", a "so anyway", or an answer whose question
was asked earlier, move the start back to where the idea begins or skip it.
A brilliant moment that needs the previous five minutes is not a clip.
Fix this by moving the START earlier, never by cutting the ending short: a
clip that loses its payoff to gain context has traded down.
- Prefer one great clip per candidate window. Maximum 2 clips per window only if clearly justified.
- DIVERSITY: never return two clips that make the same point, tell the same
story, or land the same joke — even across different windows. Pick the
stronger one and drop the other.
HOOK PLAYBOOK — pick the strongest fitting pattern for `viral_hook_text` (max 10 words):
- Open question: "Why does everyone get this wrong?"
- Hot take / controversy: "Stop doing this. Seriously."
- Number / fact shock: "97% of people miss this."
- Story loop: "This one email almost ruined me."
- POV / pattern interrupt: "POV: you finally understand it."
(These are English PATTERNS — always write the actual hook in TRANSCRIPT_LANGUAGE.)
COPY RULES — ALL text fields (descriptions, title, hook) MUST be written in TRANSCRIPT_LANGUAGE ({language}):
- Descriptions (TikTok + Instagram): 1-2 punchy sentences that tease the payoff
without spoiling it, then 3-5 topically relevant hashtags. No generic hashtag spam.
- `video_title_for_youtube_short`: max 100 chars, curiosity-driven, no fake claims.
- `predicted_score`: honest 0-100 estimate of viral potential.
TRANSCRIPT_LANGUAGE: {language}
VIDEO_DURATION_SECONDS: {video_duration}
CANDIDATE_WINDOWS_JSON:
{windows_json}
Return only:
{{
"shorts": [
{{
"start": <number>,
"end": <number>,
"source_window_id": "<window id>",
"predicted_score": <integer 0-100>,
"video_description_for_tiktok": "<description + hashtags>",
"video_description_for_instagram": "<description + hashtags>",
"video_title_for_youtube_short": "<title max 100 chars>",
"viral_hook_text": "<short overlay max 10 words>"
}}
]
}}
"""
def _strip_code_fences(text: str) -> str:
text = (text or "").strip()
if text.startswith("```"):
lines = text.splitlines()
if lines:
lines = lines[1:]
if lines and lines[-1].strip() == "```":
lines = lines[:-1]
text = "\n".join(lines).strip()
return text
def _extract_json_candidate(text: str) -> str:
cleaned = _strip_code_fences(text)
start = cleaned.find("{")
end = cleaned.rfind("}")
if start != -1 and end != -1 and end > start:
return cleaned[start:end + 1]
return cleaned
def _escape_invalid_unicode_escapes(text: str) -> str:
chars = []
i = 0
while i < len(text):
if text[i] == "\\" and i + 1 < len(text) and text[i + 1] == "u":
hex_digits = text[i + 2:i + 6]
if len(hex_digits) < 4 or any(ch not in "0123456789abcdefABCDEF" for ch in hex_digits):
chars.append("\\\\u")
i += 2
continue
chars.append(text[i])
i += 1
return "".join(chars)
def _parse_json_response_text(text: str) -> dict:
if not text:
raise ValueError("Gemini returned an empty response body.")
candidate = _extract_json_candidate(text).replace("\x00", "").strip()
if not candidate:
raise ValueError("Gemini response did not contain a JSON object.")
parse_attempts = [candidate]
sanitized_candidate = _escape_invalid_unicode_escapes(candidate)
if sanitized_candidate != candidate:
parse_attempts.append(sanitized_candidate)
last_error: Optional[Exception] = None
for parse_candidate in parse_attempts:
try:
return json.loads(parse_candidate)
except json.JSONDecodeError as e:
last_error = e
raise ValueError(f"Failed to parse Gemini JSON response: {last_error}")
class GeminiBlockedError(ValueError):
"""The API refused the request for content-policy reasons.
Deterministic: the same payload is rejected every time (verified in prod,
23-jul-2026 — a stand-up video came back PROHIBITED_CONTENT in ~300ms on
every attempt), and BLOCK_NONE safety settings do NOT lift it. Retrying is
pointless, so callers must fail fast with a message that tells the user the
video's content is the problem, not the service."""
_BLOCKED_FINISH_REASONS = {"SAFETY", "PROHIBITED_CONTENT", "BLOCKLIST",
"SPII", "IMAGE_SAFETY", "RECITATION"}
def raise_if_blocked(response):
"""Raise GeminiBlockedError when the API refused to answer on policy grounds."""
pf = getattr(response, "prompt_feedback", None)
reason = getattr(pf, "block_reason", None)
if reason:
name = getattr(reason, "name", None) or str(reason)
raise GeminiBlockedError(
f"Gemini blocked this video's content ({name}). The AI provider's "
"usage policies reject this material, so it can't be analyzed.")
for c in (getattr(response, "candidates", None) or []):
fr = getattr(c, "finish_reason", None)
name = (getattr(fr, "name", None) or str(fr or "")).upper()
if name in _BLOCKED_FINISH_REASONS:
raise GeminiBlockedError(
f"Gemini blocked its answer for this video ({name}). The AI "
"provider's usage policies reject this material, so it can't be analyzed.")
def _get_response_text(response) -> str:
try:
text = response.text
if text:
return text
except Exception:
pass
parts = []
for candidate in getattr(response, "candidates", []) or []:
content = getattr(candidate, "content", None)
for part in getattr(content, "parts", []) or []:
part_text = getattr(part, "text", None)
if part_text:
parts.append(part_text)
return "\n".join(parts).strip()
def _calculate_cost_analysis(response, model_name: str) -> Optional[dict]:
usage = getattr(response, "usage_metadata", None)
if not usage:
return None
prices = lookup_model_prices(model_name)
price_estimated = prices is None
if prices is None:
# Unknown model: conservative estimate so the UI shows something sane.
prices = (0.50, 3.00)
input_price_per_million, output_price_per_million = prices
prompt_tokens = usage.prompt_token_count or 0
output_tokens = usage.candidates_token_count or 0
# Thinking tokens bill at the output rate even though they are invisible.
thinking_tokens = getattr(usage, "thoughts_token_count", 0) or 0
input_cost = (prompt_tokens / 1_000_000) * input_price_per_million
output_cost = ((output_tokens + thinking_tokens) / 1_000_000) * output_price_per_million
total_cost = input_cost + output_cost
return {
"input_tokens": prompt_tokens,
"output_tokens": output_tokens,
"thinking_tokens": thinking_tokens,
"input_cost": input_cost,
"output_cost": output_cost,
"total_cost": total_cost,
"model": model_name,
"price_estimated": price_estimated,
}
def _thinking_config_from_env(model_name: str):
"""GEMINI_THINKING_SCORE: off (default) | low | high | <token budget>.
Applied only to the scoring stage. Gemini 3 models take thinking_level,
Gemini 2.5 takes thinking_budget; returns None (= model default) if the
setting is off or the SDK rejects the config."""
raw = (os.getenv("GEMINI_THINKING_SCORE") or "off").strip().lower()
if raw in ("", "off", "0", "none", "false"):
return None
try:
if raw.isdigit():
return genai_types.ThinkingConfig(thinking_budget=int(raw))
if raw in ("low", "high"):
if model_name.startswith("gemini-3"):
return genai_types.ThinkingConfig(thinking_level=raw)
return genai_types.ThinkingConfig(thinking_budget=2048 if raw == "low" else 8192)
except Exception as e:
_log(f"⚠️ Ignoring GEMINI_THINKING_SCORE={raw!r}: {e}")
return None
def _config_for_strategy(strategy: str, mode: str, model_name: str) -> genai_types.GenerateContentConfig:
# The detail stage writes creative copy (hooks/descriptions) — it gets a
# high temperature; timestamps are validated and word-snapped afterwards.
# The score stage stays precise. Fallback strategies get conservative.
creative = mode == "detail"
kwargs = {
"response_mime_type": "application/json",
"candidate_count": 1,
}
if strategy == "strict-json":
kwargs["temperature"] = 0.7 if creative else 0.1
elif strategy == "json-text-recovery":
kwargs["temperature"] = 0.2 if creative else 0.0
else: # structured-schema: schema-enforced output, primary strategy
kwargs["temperature"] = 0.9 if creative else 0.2
kwargs["response_schema"] = DetailResponse if mode == "detail" else ScoreResponse
if mode == "score":
thinking = _thinking_config_from_env(model_name)
if thinking is not None:
kwargs["thinking_config"] = thinking
return genai_types.GenerateContentConfig(**kwargs)
def main() -> int:
_configure_stdio()
parser = argparse.ArgumentParser(description="Run a single Gemini request for clip scoring/detailing.")
parser.add_argument("--mode", choices=["score", "detail"], required=True)
parser.add_argument("--input", dest="input_path", required=True)
parser.add_argument("--output", dest="output_path", required=True)
parser.add_argument("--strategy", default="structured-schema")
parser.add_argument("--model", default="gemini-2.5-flash")
args = parser.parse_args()
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
raise SystemExit("Missing GEMINI_API_KEY.")
with open(args.input_path, "r", encoding="utf-8") as f:
payload = json.load(f)
model_name = args.model
client = genai.Client(api_key=api_key)
config = _config_for_strategy(args.strategy, args.mode, model_name)
language = str(payload.get("language") or "unknown")
template = SCORE_PROMPT_TEMPLATE if args.mode == "score" else DETAIL_PROMPT_TEMPLATE
prompt = template.format(
video_duration=payload["video_duration"],
language=language,
windows_json=json.dumps(payload["windows"], ensure_ascii=False),
)
_log(f"🤖 Gemini worker request: mode={args.mode} strategy={args.strategy} model={model_name} items={len(payload.get('windows', []))}")
response = client.models.generate_content(
model=model_name,
contents=prompt,
config=config,
)
raw_text = _get_response_text(response)
# With response_schema the SDK returns an already-validated object; fall
# back to the text-repair path only when that is unavailable.
parsed_obj = getattr(response, "parsed", None)
if parsed_obj is not None:
parsed = parsed_obj.model_dump() if hasattr(parsed_obj, "model_dump") else parsed_obj
else:
parsed = _parse_json_response_text(raw_text)
result = {
"mode": args.mode,
"payload": parsed,
"cost_analysis": _calculate_cost_analysis(response, model_name),
"raw_text": raw_text,
}
with open(args.output_path, "w", encoding="utf-8") as f:
json.dump(result, f, indent=2, ensure_ascii=False)
_log(f"✅ Gemini worker success: mode={args.mode}")
return 0
if __name__ == "__main__":
raise SystemExit(main())