File size: 3,791 Bytes
84bf161
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import tarfile
import datasets

class LEMASDataset(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")

    def _info(self):
        return datasets.DatasetInfo(
            description="LEMAS multilingual training dataset with split audio archives",
            features=datasets.Features(
                {
                    "key": datasets.Value("string"),
                    "audio": datasets.Audio(),
                    "dur": datasets.Value("float32"),
                    "txt": datasets.Value("string"),
                    "align": datasets.features.Sequence({
                        "txt": datasets.Value("string"),
                        "words": datasets.features.Sequence({
                            "word": datasets.Value("string"),
                            "start": datasets.Value("float32"),
                            "end": datasets.Value("float32"),
                            "score": datasets.Value("float32"),
                        }),
                    }),
                }
            ),
            supervised_keys=("audio", "txt"),
            homepage="https://your-project-homepage",
            license="Apache-2.0",
        )

    def _split_generators(self, dl_manager):
        # 假设你上传的文件结构已经是这样的解压路径
        # LEMAS-Dataset/train/de/de000.jsonl, de000.tar.gz ...
        data_dir = dl_manager.extract("LEMAS-Dataset/train")  # 或者目录路径,也可以改成dl_manager.manual_dir等

        # 遍历所有语言目录
        languages = [d for d in os.listdir(data_dir) if os.path.isdir(os.path.join(data_dir, d))]
        all_generators = []
        for lang in languages:
            lang_dir = os.path.join(data_dir, lang)
            jsonl_files = sorted([f for f in os.listdir(lang_dir) if f.endswith(".jsonl")])
            tar_files = sorted([f for f in os.listdir(lang_dir) if f.endswith(".tar.gz")])

            # 匹配jsonl和tar.gz
            for jsonl_file in jsonl_files:
                prefix = jsonl_file[:-6]
                tar_file = prefix + ".tar.gz"
                if tar_file not in tar_files:
                    continue
                all_generators.append(
                    datasets.SplitGenerator(
                        name=datasets.Split.TRAIN,
                        gen_kwargs={
                            "jsonl_path": os.path.join(lang_dir, jsonl_file),
                            "audio_archive_path": os.path.join(lang_dir, tar_file),
                        },
                    )
                )
        return all_generators

    def _generate_examples(self, jsonl_path, audio_archive_path):
        # 打开tar.gz文件
        archive = tarfile.open(audio_archive_path, "r:gz")
        members = {m.name: m for m in archive.getmembers()}

        def get_audio_bytes(path_in_archive):
            if path_in_archive not in members:
                raise FileNotFoundError(f"Audio {path_in_archive} not found in archive {audio_archive_path}")
            f = archive.extractfile(members[path_in_archive])
            if f is None:
                raise FileNotFoundError(f"Cannot extract {path_in_archive} from {audio_archive_path}")
            return f.read()

        with open(jsonl_path, "r", encoding="utf-8") as f:
            for line in f:
                example = json.loads(line)
                audio_bytes = get_audio_bytes(example["audio"])
                yield example["key"], {
                    "key": example["key"],
                    "audio": {"bytes": audio_bytes, "path": example["audio"]},
                    "dur": example["dur"],
                    "txt": example["txt"],
                    "align": example["align"],
                }
        archive.close()