File size: 3,061 Bytes
c193ec6
 
0e839d1
c193ec6
 
 
 
0e839d1
 
 
c193ec6
0e839d1
 
 
 
 
 
 
 
c193ec6
 
 
 
0e839d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c193ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e839d1
c193ec6
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
import shutil
import argparse

import datasets


NUM_SONGS = 79


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--move-audio",
        action="store_true",
        help="Move and link audio files (default: only create metadata.jsonl files)",
    )
    args = parser.parse_args()

    datasets.disable_caching()

    subsets_dir = Path("subsets")

    if args.move_audio:
        # Divide the MP3 files by language. Hugging Face requires each subset and its metadata to be in
        # a separate directory. However, for backwards compatibility, we also want to keep the top-level
        # "audio" directory and replace the MP3 files with symlinks into the subsets.
        if subsets_dir.exists():
            shutil.rmtree(subsets_dir)
        subsets_dir.mkdir()

        # Back up the directory with the original MP3 files
        if not Path("audio_orig").exists():
            Path("audio").rename("audio_orig")
        elif Path("audio").exists():
            shutil.rmtree("audio")
        Path("audio").mkdir(exist_ok=True)
    else:
        num_audio_files = sum(1 for _ in subsets_dir.glob("*/audio/*.mp3"))
        if num_audio_files != NUM_SONGS:
            raise RuntimeError(
                f"Expected '{subsets_dir}' to exist and contain all {NUM_SONGS} audio files in "
                f"subdirectories by language. Found {num_audio_files} files matching the pattern. "
                "Use --move-audio if the audio files are found in 'audio' instead."
            )

    # Create language subsets and:
    # - hard link the files from mp3_orig to subsets
    # - add symlinks from mp3 into subsets
    for config_name in ["all", "en", "es", "de", "fr"]:
        subset_dir = Path(".") if config_name == "all" else subsets_dir / config_name
        subset_dir.mkdir(exist_ok=True)

        dataset = datasets.load_dataset(
            "./loader.py",
            config_name,
            trust_remote_code=True,
            split="test",
        )
        if config_name == "all":
            dataset = dataset.add_column(
                "file_name",
                [
                    str(Path("subsets") / lg / "audio" / f"{n}.mp3")
                    for lg, n in zip(dataset["language"], dataset["name"])
                ],
            )
        else:
            dataset = dataset.add_column(
                "file_name", [str(Path("audio") / f"{n}.mp3") for n in dataset["name"]]
            )
        dataset = dataset.remove_columns("audio")

        dataset.to_json(subset_dir / "metadata.jsonl")

        if args.move_audio and config_name != "all":
            (subset_dir / "audio").mkdir()
            for name in dataset["name"]:
                (subset_dir / "audio" / f"{name}.mp3").hardlink_to(
                    Path("audio_orig") / f"{name}.mp3"
                )
                (Path("audio") / f"{name}.mp3").symlink_to(
                    Path("..") / subset_dir / "audio" / f"{name}.mp3"
                )


if __name__ == "__main__":
    main()