"""Construct localization-given-labels prompts from gold segments.""" from __future__ import annotations import json import random from collections import Counter from typing import Sequence from localization.schema import GoldSegment, LabelSpec DEFAULT_SEED = 0 PROTOCOL_NAME = "localization-given-labels" SOURCE_DATASET = "macrodata/WGO-Bench" def label_specs_from_segments( episode_id: str, gold_segments: Sequence[GoldSegment], *, seed: int = DEFAULT_SEED, ) -> list[LabelSpec]: """Unique labels with multiplicity, shuffled deterministically per episode.""" counts = Counter(segment.label for segment in gold_segments) specs = [LabelSpec(label, counts[label]) for label in sorted(counts)] rng = random.Random(f"{seed}:{episode_id}") rng.shuffle(specs) return specs def multiplicity_phrase(spec: LabelSpec) -> str: quoted = json.dumps(spec.label) if spec.multiplicity == 1: return quoted return f"{quoted} (occurs {spec.multiplicity} times)" def localization_prompt( instruction: str, specs: Sequence[LabelSpec], ) -> str: labels = "\n".join(f"- {multiplicity_phrase(spec)}" for spec in specs) return ( "Locate the listed manipulation event labels in this robot video from the " "timestamped contact sheets.\n\n" "Return only JSON matching the provided schema. For each listed label, " "return exactly its requested number of intervals. Each interval must echo " "the exact label string in label_echo and use visible timestamps for " "start_sec and end_sec.\n\n" "Rules:\n" "- Bind times only to the exact listed label.\n" "- Do not invent labels that are not listed.\n" "- Use one interval per occurrence when a label occurs multiple times.\n" "- Prefer temporally tight intervals around completed manipulation events.\n\n" f"Episode instruction: {instruction}\n\n" f"Event labels:\n{labels}\n" ) def construction_meta(*, seed: int = DEFAULT_SEED) -> dict[str, str | int]: return { "seed": seed, "protocol": PROTOCOL_NAME, "source": SOURCE_DATASET, }