EasyOctopus's picture
Load example inputs and helpers from a standard Hub snapshot
37aba78 verified
Raw History Blame Contribute Delete
2.3 kB
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
"""Generate fictional messages with the release's scenario vocabulary."""
import argparse
from collections import defaultdict
from pathlib import Path
import sys
import json
import random
HERE=Path(__file__).absolute().parent
sys.path.insert(0, str(HERE))
from slot_values import slots,render
def main():
parser=argparse.ArgumentParser()
parser.add_argument("--output",type=Path,required=True)
parser.add_argument("--seed",type=int,help="Defaults to the released study seed")
parser.add_argument("--per-template",type=int,help="Override split-specific sample counts")
args=parser.parse_args()
if args.output.exists() and any(args.output.iterdir()):
parser.error("Choose an empty output directory")
if args.per_template is not None and args.per_template<1:
parser.error("Sample count must be positive")
config=json.loads((HERE/"generation.json").read_text())
rng=random.Random(config["seed"] if args.seed is None else args.seed)
seen=defaultdict(set)
splits=defaultdict(list)
# Traverse the shared study catalog in its original order to replay RNG draws.
for line in (HERE/"templates.tsv").read_text().splitlines():
if line.startswith("#"): continue
task,split,scenario,*alternates=line.split("\t")
for number,template in enumerate(alternates,1):
key=f"{scenario}-{number}"
for i in range(args.per_template or config["generation"][split]):
for _ in range(1000):
row=render(task,template,slots(rng,split))
text_key=" ".join(row["text"].casefold().split())
if text_key not in seen[task]: break
else: raise ValueError("Template value pool exhausted: "+key)
seen[task].add(text_key)
if task==config["task"]:
splits[split].append({"id":f"{task}-{key}-{i:03d}","scenario":scenario,"template":key,**row})
args.output.mkdir(parents=True,exist_ok=True)
for split,rows in splits.items():
(args.output/(split+".jsonl")).write_text("".join(json.dumps(row,ensure_ascii=False)+"\n" for row in rows))
if __name__=="__main__":
main()