File size: 2,183 Bytes
d37698e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | """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,
}
|