TEXEDO / prepare_texedo_dataset.py
JianuoCao's picture
Add files using upload-large-folder tool
cf85cff verified
Raw
History Blame
4.79 kB
#!/usr/bin/env python3
"""Prepare the TEXEDO dataset layout for Hugging Face upload."""
from __future__ import annotations
import json
import os
import shutil
from pathlib import Path
SRC_ROOT = Path("/home/jianuo/projects/MotionGPT/datasets/CustomCombined")
OUT_ROOT = Path("/home/jianuo/projects/MotionGPT/datasets/TEXEDO_dataset")
SPLITS = {
"train": "train",
"val": "validation",
"test": "test",
}
def source_name(raw: str) -> str:
if raw == "textseedo":
return "claw"
return raw
def bucket_for(sample_id: str) -> str:
return sample_id[:3]
def link_or_copy(src: Path, dst: Path) -> None:
dst.parent.mkdir(parents=True, exist_ok=True)
if dst.exists():
return
try:
os.link(src, dst)
except OSError:
shutil.copy2(src, dst)
def parse_text_file(path: Path) -> list[dict[str, object]]:
captions = []
for line in path.read_text(encoding="utf-8").splitlines():
line = line.strip()
if not line:
continue
parts = line.split("#")
caption = parts[0]
tokens = parts[1].split() if len(parts) > 1 and parts[1] else []
start = float(parts[2]) if len(parts) > 2 and parts[2] else 0.0
end = float(parts[3]) if len(parts) > 3 and parts[3] else 0.0
captions.append(
{
"caption": caption,
"tokens": tokens,
"start_time": start,
"end_time": end,
}
)
return captions
def read_ids(split: str) -> list[str]:
return (SRC_ROOT / f"{split}.txt").read_text(encoding="utf-8").split()
def main() -> None:
match_info = json.loads((SRC_ROOT / "match_info.json").read_text(encoding="utf-8"))
info_by_id = {item["new_id"]: item for item in match_info}
(OUT_ROOT / "data").mkdir(parents=True, exist_ok=True)
(OUT_ROOT / "metadata").mkdir(parents=True, exist_ok=True)
summary = {
"dataset": "TEXEDO_dataset",
"source_root": str(SRC_ROOT),
"num_samples": 0,
"splits": {},
"sources": {},
"fields": [
"id",
"split",
"source",
"motion_path",
"text_path",
"num_frames",
"motion_dim",
"num_texts",
"captions",
],
}
all_rows = []
for original_split, hf_split in SPLITS.items():
ids = read_ids(original_split)
(OUT_ROOT / f"{original_split}.txt").write_text(
"\n".join(ids) + "\n", encoding="utf-8"
)
rows = []
for sample_id in ids:
if sample_id not in info_by_id:
raise KeyError(f"{sample_id} is missing from match_info.json")
item = info_by_id[sample_id]
src = source_name(item["source"])
bucket = bucket_for(sample_id)
motion_src = SRC_ROOT / "new_joint_vecs" / f"{sample_id}.npy"
text_src = SRC_ROOT / "texts" / f"{sample_id}.txt"
if not motion_src.exists():
raise FileNotFoundError(motion_src)
if not text_src.exists():
raise FileNotFoundError(text_src)
motion_rel = Path("motions") / src / bucket / f"{sample_id}.npy"
text_rel = Path("texts") / src / bucket / f"{sample_id}.txt"
link_or_copy(motion_src, OUT_ROOT / motion_rel)
link_or_copy(text_src, OUT_ROOT / text_rel)
row = {
"id": sample_id,
"split": hf_split,
"source": src,
"motion_path": motion_rel.as_posix(),
"text_path": text_rel.as_posix(),
"num_frames": int(item["num_frames"]),
"motion_dim": 36,
"num_texts": int(item["num_texts"]),
"captions": parse_text_file(text_src),
}
rows.append(row)
all_rows.append(row)
summary["sources"][src] = summary["sources"].get(src, 0) + 1
out_jsonl = OUT_ROOT / "data" / f"{hf_split}.jsonl"
with out_jsonl.open("w", encoding="utf-8") as f:
for row in rows:
f.write(json.dumps(row, ensure_ascii=False) + "\n")
summary["splits"][hf_split] = {
"num_samples": len(rows),
"file": f"data/{hf_split}.jsonl",
}
with (OUT_ROOT / "data" / "all.jsonl").open("w", encoding="utf-8") as f:
for row in all_rows:
f.write(json.dumps(row, ensure_ascii=False) + "\n")
summary["num_samples"] = len(all_rows)
(OUT_ROOT / "metadata" / "dataset_summary.json").write_text(
json.dumps(summary, indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
)
if __name__ == "__main__":
main()