File size: 3,928 Bytes
4a258e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
"""Add the completed ESPnet-format preparation entry to its original data card."""
import argparse
import json
from pathlib import Path

from huggingface_hub import CommitOperationAdd, HfApi, get_token, hf_hub_download

from convert_release import RECIPES


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--dataset", choices=list(RECIPES), required=True)
    parser.add_argument(
        "--root", type=Path, default=Path("/root/bagpiper-sft-conversion")
    )
    args = parser.parse_args()
    recipe = RECIPES[args.dataset]
    api = HfApi(token=get_token())
    target_revision = api.repo_info(recipe["target"], repo_type="dataset").sha
    status = json.loads(
        Path(
            hf_hub_download(
                recipe["target"],
                "STATUS.json",
                repo_type="dataset",
                revision=target_revision,
            )
        ).read_text()
    )
    assert (
        status["state"] == "complete"
        and status.get("validation") == "RELEASE_VALIDATION.json"
    )
    source_revision = api.repo_info(recipe["source"], repo_type="dataset").sha
    card = Path(
        hf_hub_download(
            recipe["source"], "README.md", repo_type="dataset", revision=source_revision
        )
    ).read_text()
    heading = "## Prepare for ESPnet training"
    section = f"""{heading}

The complete [ESPnet-format release](https://huggingface.co/datasets/{recipe['target']})
preserves all {status['expected_rows']:,} source rows, ordered text turns, and original
encoded audio bytes. It converts this Parquet schema into three-element dialogue
messages and portable audio files. Source `row_id` becomes the globally unique
ESPnet `example_id`; repeated training examples remain present.

```bash
python -m pip install huggingface_hub zstandard
hf download {recipe['target']} prepare_espnet.py --local-dir tools
python tools/prepare_espnet.py \\
    --repo-id {recipe['target']} --data-root /path/to/bagpiper-{args.dataset}-sft
```

The helper verifies archive/audio hashes, resolves local paths, and writes
`prepared/<partition>/{{all,train,valid}}.json` plus `dialogues.jsonl`. Add
`--max-shards 2` for a small trial and repeat `--partition NAME` to select
applications. Audio SHA256 determines the suggested 1% validation split;
`all.json` retains the original training pool. The split is not the paper's
official split. Prepare token-length statistics with the checkpoint-compatible
configuration as shown in the converted release's README.

See its [full release validation](https://huggingface.co/datasets/{recipe['target']}/blob/main/RELEASE_VALIDATION.json)
and [public download/ESPnet reader checks](https://huggingface.co/datasets/{recipe['target']}/blob/main/PUBLIC_DOWNLOAD_VALIDATION.json).
This conversion preserves the original source selection and applicable terms.

"""
    if heading in card:
        start = card.index(heading)
        end = card.find("\n## ", start + len(heading))
        card = card[:start] + section + (card[end + 1 :] if end >= 0 else "")
    else:
        marker = "## Loading the data"
        assert marker in card
        card = card.replace(marker, section + marker, 1)
    output = args.root / "releases" / args.dataset / "SOURCE_README.updated.md"
    output.write_text(card)
    commit = api.create_commit(
        recipe["source"],
        repo_type="dataset",
        parent_commit=source_revision,
        operations=[
            CommitOperationAdd(path_in_repo="README.md", path_or_fileobj=output)
        ],
        commit_message="Link completed lossless ESPnet training format and preparation helper",
    )
    print(
        json.dumps(
            dict(
                source=recipe["source"],
                commit=commit.oid,
                converted_revision=target_revision,
            )
        )
    )


if __name__ == "__main__":
    main()