La alerta del 23-jul (5/12 jobs, "empty response body") no era la API fallando: un vídeo subido (stand-up con contenido explícito) devuelve prompt_feedback.block_reason=PROHIBITED_CONTENT en ~300ms, de forma determinista — verificado con la transcripción real, y BLOCK_NONE en safety_settings NO lo levanta (es el filtro de política de Google, no configurable). Reintentábamos 3 veces algo imposible, el error parecía un fallo nuestro, y el usuario reintegró el mismo vídeo varias veces inflando la tasa de fallos. - gemini_worker: GeminiBlockedError + raise_if_blocked() (block_reason del prompt y finish_reason SAFETY/PROHIBITED_CONTENT/... del candidato). - main: _run_gemini_stage falla rápido sin reintentos ante bloqueo (0.5s vs ~15s) y propaga el motivo real hasta el log del job en ambas vías (transcript y visión); ya no se convierte en el genérico "no clips". - alerts: categoría propia "blocked content (user video)" para que la alerta no se lea como caída del servicio.
432 lines
16 KiB
Python
432 lines
16 KiB
Python
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.
|
||
"""
|
||
|
||
|
||
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.
|
||
- 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())
|