"""Flora-TempSeg demo: fine-grained video temporal segmentation on Qwen3.5-9B. Reproduces the released model's native inference path (nvidia/Flora-TempSeg): 64 ordered temporal slots @ 4 Hz per 16-second window (12 s emit core, 2 s guards), 20 Hz local ticks, trained pool prompt with the canonical window envelope, thinking disabled, greedy decoding, <=1024 new tokens, FP32 Gated-DeltaNet parameters and FP32 LoRA adapter on a BF16 base. """ import spaces # ZeroGPU: must be imported before torch import json import math import re import time from bisect import bisect_left from dataclasses import dataclass from fractions import Fraction from pathlib import Path import av import gradio as gr import torch from PIL import Image # --------------------------------------------------------------------------- # Model identity (pinned revisions from the release manifest models.json) # --------------------------------------------------------------------------- TEMPSEG_REPO = "nvidia/Flora-TempSeg" TEMPSEG_REVISION = "114a1076f70eb53f4a58ac045505db3087e79347" BASE_REPO = "Qwen/Qwen3.5-9B" BASE_REVISION = "c202236235762e1c871ad0ccb60c8ee5ba337b9a" TICK_US = 50_000 # 20 Hz local ticks SLOT_SPACING_US = 250_000 # 4 Hz slots SLOT_COUNT = 64 # 16 s view SLOT_FPS = 4 LEFT_GUARD_US = RIGHT_GUARD_US = 2_000_000 CORE_US = 12_000_000 VIEW_US = LEFT_GUARD_US + CORE_US + RIGHT_GUARD_US MAX_NEW_TOKENS = 1024 CONTEXT_BUDGET = 32_768 MAX_SEGMENTS = 32 MAX_DURATION_S = 120 MAX_UPLOAD_BYTES = 256 * 1024 * 1024 MAX_DIMENSION = 4096 MAX_FRAME_COUNT = 7200 # Trained pool prompts (verbatim from the release TEMPSEG_PROMPTS.json). POOL_PROMPTS = { "human": ( "