import base64 import io import logging import os import re import shutil import subprocess import sys import tempfile import time import uuid from pathlib import Path logging.basicConfig( level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s", datefmt="%H:%M:%S", ) log = logging.getLogger(__name__) try: import chatterbox # noqa: F401 except ImportError: log.info("[boot] Installing chatterbox-tts --no-deps …") subprocess.run( [sys.executable, "-m", "pip", "install", "--no-deps", "--quiet", "chatterbox-tts"], check=True, ) log.info("[boot] chatterbox-tts installed.") from gradio import Server from fastapi import Request, BackgroundTasks from fastapi.responses import HTMLResponse, JSONResponse, FileResponse from commentary import STYLES, VIBE_TO_STYLE, generate_commentary from events import events_to_frames, pick_key_events from faces import annotate_event_frame, frame_at, scan_video, write_names_to_jsonl from ov_models import download_models from tts import generate_script_audio from video import get_duration, normalize_video MAX_SECONDS = 60.0 HERE = Path(__file__).parent SESSIONS: dict[str, dict] = {} TIERS = ["HEADLINER", "RISING", "DIVA", "WILDCARD", "BREAKOUT", "CAMEO"] SUPPORTED_VIDEO = {".mp4", ".mov", ".webm", ".mkv", ".avi", ".m4v"} log.info("[boot] Downloading OpenVINO models if needed…") try: download_models() log.info("[boot] OpenVINO models ready.") except Exception as e: log.warning(f"[boot] OV model download failed: {e}") def _crop_b64(pil, size=160): if pil is None: return None img = pil.copy(); img.thumbnail((size, size)) buf = io.BytesIO(); img.save(buf, format="PNG") return "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode() def _build_ui() -> str: doc = (HERE / "Marquee.html").read_text() css = (HERE / "marquee.css").read_text() js = (HERE / "marquee.js").read_text() link_re = re.compile(r']*\bhref="(?!https?:)[^"]*\.css"[^>]*>', re.I) script_re = re.compile(r']*\bsrc="(?!https?:)[^"]*\.js"[^>]*>\s*', re.I) doc, n_css = link_re.subn(lambda _m: f"", doc, count=1) doc, n_js = script_re.subn(lambda _m: f'', doc, count=1) if n_css == 0: log.warning("[boot] No local stylesheet matched — CSS not inlined.") if n_js == 0: log.warning("[boot] No local