clipwright/layout_picker.py
juancarlos.cavero 3f7ac4da18 feat(reframe): pick the layout from 12 frames instead of uploading the video
Gemini bills video at ~300 tokens per second. An hour of source is ~1.08M
tokens (past a 1M context window) and a 1-2GB upload to get one word back. This
pipeline ingests hour-long podcasts, so uploading the file failed in exactly the
normal case.

Measured over the same 48 clips against the hand-checked labels:

  whole video          94% / 92% / 96%   18,17,18 of 20 found
  12 frames @ 640px    90% / 90%         15,16 of 20
  12 frames @ 1024px   92%               17 of 20
  24 frames @ 1024px   90%               16 of 20

Resolution was the gap, not frame count: at 640px a spreadsheet is unreadable,
and doubling the frames made it slightly worse. At 1024px the difference from
sending the whole video sits inside the run-to-run variance the whole-video mode
already has (92-96%), at 2.2s per clip instead of ~15s, with a cost that no
longer depends on the source duration.

It also removes the Files API dance: no upload, no waiting for ACTIVE.
2026-07-31 13:28:45 +02:00

180 lines
6.9 KiB
Python

"""Ask Gemini which layout a video needs, once per video.
The four previous attempts at this problem asked the model (or a pixel
heuristic) to MEASURE something — edge density, MSER text density, what fraction
of the duration had content on screen, what fraction of the width it spanned —
and let thresholds turn that number into a routing decision. All four failed the
same way: they could not separate a spreadsheet from a corner scoreboard.
This asks for the decision itself. Measured over the 48-clip corpus against
hand-checked labels (`labels-layout.json` in the reframe-testing skill), three
runs:
run 1 45/48 94% 18/20 content found 1 false positive of 28
run 2 44/48 92% 17/20 1 of 28
run 3 46/48 96% 18/20 0 of 28
Two clips out of 48 changed answer between runs. That matters because the
earlier note in this repo said Gemini was too non-deterministic to build on —
the same video scored 1% and 97% coverage on consecutive runs. The variance was
in asking for a continuous measurement, not in the model: a categorical choice
between closed options is stable.
It samples FRAMES instead of uploading the video. Gemini bills video at ~300
tokens per second, so an hour of source is ~1.08M tokens — past a 1M context
window before it starts — and means pushing a 1-2GB upload to get back one word.
This pipeline ingests hour-long podcasts, so that scales badly in exactly the
normal case. Twelve stills cost ~3k tokens whatever the source runs to.
Measured on the same 48 clips (whole video vs sampled frames):
whole video 94% / 92% / 96% 18,17,18 of 20 found
12 frames @ 640px 90% / 90% 15,16 of 20
12 frames @ 1024px 92% 17 of 20
24 frames @ 1024px 90% 16 of 20
Resolution was the gap, not frame count: at 640px a spreadsheet is unreadable,
and doubling the frames made it slightly worse rather than better. At 1024px the
difference from sending the whole video sits inside the run-to-run variance the
whole-video mode already has, at 2.2s per clip instead of ~15s.
Off by default (``AUTO_LAYOUT=1``). A caller that already switched layouts on
by hand wins: this only ever ADDS, so an explicit choice is never overridden.
"""
import json
import os
ENABLED = os.environ.get("AUTO_LAYOUT", "0") == "1"
# 12 frames at 1024px wide. Both numbers are measured, not guessed: see above.
SAMPLE_FRAMES = int(os.environ.get("LAYOUT_SAMPLE_FRAMES", "12"))
SAMPLE_WIDTH = int(os.environ.get("LAYOUT_SAMPLE_WIDTH", "1024"))
# What each decision turns on. Keys match the layout names in the prompt.
DECISION_FLAGS = {
"none": [],
"screencast": ["screencast_layout"],
"split": ["split_layout", "active_speaker"],
}
VALID = set(DECISION_FLAGS)
def _module_flags(decision):
"""Modules to enable for a decision, ignoring anything unrecognised."""
return DECISION_FLAGS.get(str(decision or "none").strip().lower(), [])
def apply(decision):
"""Switch on the modules a decision needs. Returns the modules touched.
Deliberately additive: an operator who set SPLIT_LAYOUT=1 for a job wants
stacking regardless of what the model thinks, and a model that says "none"
must not quietly undo that.
"""
import active_speaker
import screencast_layout
import split_layout
modules = {"split_layout": split_layout,
"screencast_layout": screencast_layout,
"active_speaker": active_speaker}
touched = []
for name in _module_flags(decision):
module = modules.get(name)
if module is not None and not getattr(module, "ENABLED", False):
module.ENABLED = True
touched.append(name)
return touched
def sample_frames(video_path, n=None, width=None):
"""JPEG bytes for ``n`` frames spread evenly across the video."""
import cv2
n = n or SAMPLE_FRAMES
width = width or SAMPLE_WIDTH
cap = cv2.VideoCapture(video_path)
total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
out = []
try:
if total <= 0:
return out
for i in range(n):
cap.set(cv2.CAP_PROP_POS_FRAMES, int(i * total / n))
ok, frame = cap.read()
if not ok:
continue
h, w = frame.shape[:2]
scaled = cv2.resize(frame, (width, max(2, int(h * width / w))),
interpolation=cv2.INTER_AREA)
ok, buf = cv2.imencode(".jpg", scaled,
[cv2.IMWRITE_JPEG_QUALITY, 80])
if ok:
out.append(buf.tobytes())
finally:
cap.release()
return out
def pick(video_path, video_duration):
"""The layout Gemini picks for this video, or "none" on any failure.
Never raises: a missing answer has to degrade to today's routing rather
than break the job.
"""
if not ENABLED:
return "none"
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
return "none"
model_name = os.environ.get("GEMINI_MODEL") or 'gemini-3.1-flash-lite'
print("🎛️ Choosing a layout for this video…")
try:
# Inside the try on purpose: the contract above is that this never
# raises, and an unimportable SDK is just one more reason to fall back.
from google import genai
from google.genai import types as genai_types
import gemini_worker
frames = sample_frames(video_path)
if not frames:
print(" ⚠️ No readable frames — keeping the default layout.")
return "none"
client = genai.Client(api_key=api_key)
parts = [genai_types.Part.from_bytes(data=b, mime_type="image/jpeg")
for b in frames]
response = client.models.generate_content(
model=model_name,
contents=parts + [gemini_worker.LAYOUT_CHOICE_PROMPT],
config=genai_types.GenerateContentConfig(
response_mime_type="application/json",
response_schema=gemini_worker.LayoutChoice,
))
gemini_worker.raise_if_blocked(response)
answer = json.loads(response.text) or {}
except Exception as e:
print(f" ⚠️ Layout choice failed ({e}) — keeping the default layout.")
return "none"
decision = str(answer.get("layout", "none")).strip().lower()
if decision not in VALID:
print(f" ⚠️ Unknown layout '{decision}' — keeping the default layout.")
return "none"
why = str(answer.get("why", ""))[:80]
confidence = answer.get("confidence")
print(f" 🎬 Layout: {decision} (confianza {confidence}) — {why}")
return decision
def pick_and_apply(video_path, video_duration):
"""Convenience for the pipeline: decide, switch on, report what changed."""
decision = pick(video_path, video_duration)
touched = apply(decision)
if touched:
print(f" ✅ Enabled: {', '.join(touched)}")
return decision