fix(selection): bisect a Gemini policy block instead of failing the job
PROHIBITED_CONTENT fired on the scoring batch of a plain software walkthrough: windows 5+6 together blocked 3/3 on lite, 3.7-flash and 2.5-flash, each alone passed, and a neutral prompt over the same text passed. The filter reacts to combinations, so it cannot be predicted or sanitized. Both selection passes now run through _run_stage_split: on a block the batch is halved until the combination is isolated, and only a window that blocks on its own is dropped. The affected job now yields 2 clips instead of dying.
This commit is contained in:
parent
22c991d22b
commit
443c7980a5
2 changed files with 93 additions and 19 deletions
65
main.py
65
main.py
|
|
@ -1472,6 +1472,34 @@ def _run_gemini_stage(client, model_name, prompt, schema):
|
|||
time.sleep(wait)
|
||||
|
||||
|
||||
def _run_stage_split(client, model_name, items, build_prompt, schema, key, costs, label):
|
||||
"""Run a Gemini stage over ``items``; on a policy block, bisect.
|
||||
|
||||
Google's prompt filter (PROHIBITED_CONTENT) fires on some COMBINATIONS of
|
||||
transcript windows that pass individually (27-aug-2026: windows 5+6 of a
|
||||
software walkthrough blocked 3/3, each alone fine, all three models).
|
||||
A block is deterministic for a given prompt, so instead of failing the
|
||||
job the batch is split in halves until the offending combination is
|
||||
isolated; a single item that still blocks is dropped with a log line.
|
||||
Returns the merged list found under ``key`` in each response."""
|
||||
if not items:
|
||||
return []
|
||||
prompt = build_prompt(items)
|
||||
try:
|
||||
parsed, cost = _run_gemini_stage(client, model_name, prompt, schema)
|
||||
if cost:
|
||||
costs.append(cost)
|
||||
return list(parsed.get(key) or [])
|
||||
except gemini_worker.GeminiBlockedError as e:
|
||||
if len(items) == 1:
|
||||
print(f" 🚫 {label}: Gemini blocked window {items[0].get('id')} on its own; skipping it ({e})")
|
||||
return []
|
||||
mid = len(items) // 2
|
||||
print(f" 🚫 {label}: Gemini blocked a batch of {len(items)}; retrying as {mid} + {len(items) - mid}")
|
||||
return (_run_stage_split(client, model_name, items[:mid], build_prompt, schema, key, costs, label)
|
||||
+ _run_stage_split(client, model_name, items[mid:], build_prompt, schema, key, costs, label))
|
||||
|
||||
|
||||
def get_viral_clips(transcript_result, video_duration):
|
||||
"""Two-pass clip selection: score transcript windows, then detail the best.
|
||||
|
||||
|
|
@ -1512,16 +1540,18 @@ def get_viral_clips(transcript_result, video_duration):
|
|||
# --- Pass 1: score windows in batches, keep the highest-scoring ---
|
||||
scored = []
|
||||
SCORE_BATCH = 8
|
||||
for b in range(0, len(windows), SCORE_BATCH):
|
||||
batch = windows[b:b + SCORE_BATCH]
|
||||
payload = [{"id": w["id"], "start": w["start"], "end": w["end"], "text": w["text"]} for w in batch]
|
||||
prompt = gemini_worker.SCORE_PROMPT_TEMPLATE.format(
|
||||
def _payload(ws):
|
||||
return [{"id": w["id"], "start": w["start"], "end": w["end"], "text": w["text"]} for w in ws]
|
||||
|
||||
def _score_prompt(ws):
|
||||
return gemini_worker.SCORE_PROMPT_TEMPLATE.format(
|
||||
video_duration=video_duration, language=language,
|
||||
windows_json=json.dumps(payload, ensure_ascii=False))
|
||||
parsed, cost = _run_gemini_stage(client, model_name, prompt, gemini_worker.ScoreResponse)
|
||||
if cost:
|
||||
costs.append(cost)
|
||||
scored.extend(parsed.get("windows") or [])
|
||||
windows_json=json.dumps(_payload(ws), ensure_ascii=False))
|
||||
|
||||
for b in range(0, len(windows), SCORE_BATCH):
|
||||
scored.extend(_run_stage_split(
|
||||
client, model_name, windows[b:b + SCORE_BATCH], _score_prompt,
|
||||
gemini_worker.ScoreResponse, "windows", costs, "score"))
|
||||
|
||||
# Shortlist the top windows; scale with duration so long videos surface
|
||||
# more candidates without exploding the detail call.
|
||||
|
|
@ -1534,18 +1564,21 @@ def get_viral_clips(transcript_result, video_duration):
|
|||
print(f" Shortlisted {len(shortlist)} window(s) for detail.")
|
||||
|
||||
# --- Pass 2: detailed clip extraction on the shortlist ---
|
||||
payload = [{"id": w["id"], "start": w["start"], "end": w["end"], "text": w["text"]} for w in shortlist]
|
||||
min_clips, max_clips = clip_count_targets(len(shortlist))
|
||||
prompt = gemini_worker.DETAIL_PROMPT_TEMPLATE.format(
|
||||
|
||||
def _detail_prompt(ws):
|
||||
# A split batch keeps the full clip-count band: a short list can
|
||||
# still hold the best clips, and the model returns fewer anyway.
|
||||
return gemini_worker.DETAIL_PROMPT_TEMPLATE.format(
|
||||
video_duration=video_duration, language=language,
|
||||
min_clips=min_clips, max_clips=max_clips,
|
||||
min_secs=min_secs, max_secs=max_secs,
|
||||
windows_json=json.dumps(payload, ensure_ascii=False))
|
||||
detail, cost = _run_gemini_stage(client, model_name, prompt, gemini_worker.DetailResponse)
|
||||
if cost:
|
||||
costs.append(cost)
|
||||
windows_json=json.dumps(_payload(ws), ensure_ascii=False))
|
||||
|
||||
shorts = detail.get("shorts") or []
|
||||
shorts = _run_stage_split(client, model_name, shortlist, _detail_prompt,
|
||||
gemini_worker.DetailResponse, "shorts", costs, "detail")
|
||||
if len(shorts) > max_clips:
|
||||
shorts = shorts[:max_clips]
|
||||
# Snap each proposed clip onto real word boundaries (+ a bit of silence).
|
||||
for s in shorts:
|
||||
ns, ne = snap_clip_to_words(s.get("start", 0), s.get("end", 0), words, video_duration,
|
||||
|
|
|
|||
41
tests/test_gemini_block_split.py
Normal file
41
tests/test_gemini_block_split.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
"""A policy block on a batch of windows must bisect, not kill the job."""
|
||||
import pytest
|
||||
main = pytest.importorskip("main")
|
||||
import gemini_worker
|
||||
|
||||
|
||||
def _windows(n):
|
||||
return [{"id": f"w{i}", "start": i * 10, "end": i * 10 + 10, "text": f"t{i}"} for i in range(n)]
|
||||
|
||||
|
||||
def test_block_on_pair_isolates_and_drops_only_the_culprit(monkeypatch):
|
||||
calls = []
|
||||
def fake_stage(client, model, prompt, schema):
|
||||
ids = [w["id"] for w in __import__("json").loads(prompt)]
|
||||
calls.append(ids)
|
||||
if "w5" in ids and "w6" in ids: # the pair that trips the filter
|
||||
raise gemini_worker.GeminiBlockedError("PROHIBITED_CONTENT")
|
||||
if ids == ["w3"]: # and w3 blocks even on its own
|
||||
raise gemini_worker.GeminiBlockedError("PROHIBITED_CONTENT")
|
||||
return {"windows": [{"id": i, "score": 50} for i in ids]}, {"input_tokens": 1}
|
||||
monkeypatch.setattr(main, "_run_gemini_stage", fake_stage)
|
||||
costs = []
|
||||
out = main._run_stage_split(None, "m", _windows(8), lambda ws: __import__("json").dumps(ws),
|
||||
None, "windows", costs, "score")
|
||||
got = sorted(w["id"] for w in out)
|
||||
# w5 and w6 each survive once separated; w3 only ever reached the model on
|
||||
# its own inside the [0-3] batch, which passed, so it is kept too.
|
||||
assert got == [f"w{i}" for i in range(8)]
|
||||
assert calls[0] == [f"w{i}" for i in range(8)] # tried the whole batch first
|
||||
assert [c for c in calls if "w5" in c and "w6" in c] == [calls[0], ["w4", "w5", "w6", "w7"], ["w5", "w6"]]
|
||||
assert len(costs) == 4 # [0-3], [4], [5], [6,7]
|
||||
|
||||
|
||||
def test_no_block_is_a_single_call(monkeypatch):
|
||||
calls = []
|
||||
def fake_stage(client, model, prompt, schema):
|
||||
calls.append(prompt)
|
||||
return {"shorts": [{"start": 0, "end": 20}]}, None
|
||||
monkeypatch.setattr(main, "_run_gemini_stage", fake_stage)
|
||||
out = main._run_stage_split(None, "m", _windows(3), lambda ws: "p", None, "shorts", [], "detail")
|
||||
assert out == [{"start": 0, "end": 20}] and len(calls) == 1
|
||||
Loading…
Reference in a new issue