"""Visual index building only. No answer-generation provider is required.""" import json from html import escape from pathlib import Path import subprocess import sys ROOT = Path(__file__).resolve().parent if (ROOT.parent / "lecturelens_v2").is_dir(): sys.path.insert(0, str(ROOT.parent)) sys.path.insert(0, str(ROOT.parents[1] / "shared")) from lecturelens_v2.models import configure configure() import gradio as gr from lecturelens_v2.pipeline import build, clean_session from lecturelens_v2.visualize import timeline, hierarchy, tree_html def asset(name): staged = ROOT / "assets" / name return staged if staged.exists() else ROOT.parents[1] / "examples" / name def load_demo(choice): name = "changes" if choice == "Cuts, fade and repeated scene" else "demo" return str(asset(name + ".mp4")), str(asset(name + ".srt")) def build_index(video, subtitles, detector, threshold, minimum, representation, merge, maximum, use_ocr, previous, progress=gr.Progress()): try: state = build(video, subtitles, detector, threshold, minimum, representation, merge, maximum, use_ocr, progress=lambda text: progress(0.4, desc=text)) index = state["index"]; clean_session(previous) choices = [(f'{n["id"]} · {n["start"]:.2f}–{n["end"]:.2f}s', n["id"]) for n in reversed(index["nodes"])] summary = (f'**Index ready** · {index["source"]["frame_count"]:,} frames · {len(index["shots"])} shots' f' · {len(index["groups"])} groups · {len(index["keyframes"])} keyframes · {state["elapsed_seconds"]}s processing') return summary, timeline(index), hierarchy(index), tree_html(index), gr.Dropdown(choices=choices, value=index["root"]), state["bundle"], str(Path(state["workspace"]) / "index.json"), state except (ValueError, RuntimeError) as exc: raise gr.Error(str(exc)[:400]) from None except Exception as exc: print("V2 indexing failed: " + type(exc).__name__, flush=True) raise gr.Error("Indexing could not complete. Try the Adaptive detector with Color baseline, or use a shorter clip.") from None def inspect_node(ident, state): if not state or not ident: return "Build an index to inspect a node.", [], None, None index = state["index"]; node = next((n for n in index["nodes"] if n["id"] == ident), None) if node is None: raise gr.Error("Choose a node from the current index") evidence = [e for e in index["evidence"] if e["id"] in node["evidence_ids"]] frames = [f for f in index["keyframes"] if f["id"] in node["keyframes"]] related = [r for r in index["relations"] if r["source"] == ident or r["target"] == ident] context = {"schema_version": "lecturelens.context.v1", "time_units": "seconds", "source": index["source"], "node": {k: node[k] for k in ("id", "start", "end")}, "evidence": evidence, "keyframes": frames, "limitations": index["limitations"]} root = Path(state["workspace"]); file = root / (ident + "-context.json") file.write_text(json.dumps(context, indent=2) + "\n") preview = root / (ident + "-preview.mp4") if not preview.exists(): subprocess.run(["ffmpeg", "-v", "error", "-nostdin", "-y", "-ss", str(node["start"]), "-i", state["video"], "-t", str(min(20, node["end"] - node["start"])), "-c:v", "libx264", "-preset", "veryfast", "-c:a", "aac", "-movflags", "+faststart", str(preview)], capture_output=True, timeout=60, check=True) details = (f'
{node["start"]:.3f}–{node["end"]:.3f} seconds · ' f'frames [{node["lo"]}, {node["hi"]}) · {len(evidence)} evidence items
' f'{escape(node.get("label", "Chronological hierarchy node"))}
' f'Label source: {escape(node.get("label_source", "structural_identifier"))}. ' f'Merge similarity: {node["merge_similarity"] if node["merge_similarity"] is not None else "shot leaf"}. ' f'{len(related)} repeated-scene links. Scores are not confidence probabilities.
') gallery = [(str(root / f["file"]), f'{f["id"]} · {f["time"]:.3f}s') for f in frames[:24]] return details, gallery, str(preview), str(file) def reset_input(state): clean_session(state) return (None, "Input changed. Build a new index to inspect this video.", None, None, "", gr.Dropdown(choices=[], value=None), None, None, "Build an index to inspect a node.", [], None, None) def create_app(): with gr.Blocks(title="LectureLens V2 · Index explorer", delete_cache=(3600, 3600)) as app: state = gr.State(None, time_to_live=3600, delete_callback=clean_session) gr.HTML('Detect changes. Explore the structure. Export timestamped evidence.