cdminix commited on
Commit
a2c2ee7
·
1 Parent(s): 5dcf0b4
Files changed (1) hide show
  1. libriheavy.py +122 -0
libriheavy.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import gzip
3
+
4
+ import datasets
5
+ import numpy as np
6
+
7
+ logger = datasets.logging.get_logger(__name__)
8
+
9
+ _DESCRIPTION = """\
10
+ Libriheavy is a labeled version of Librilight.
11
+ This (unofficial) huggingface dataset contains the medium (4500 hours) split of the Libriheavy dataset with alignments and mel spectrograms.
12
+ """
13
+
14
+ _URL = """\
15
+ https://github.com/k2-fsa/libriheavy
16
+ """
17
+
18
+ _CITATION = """\
19
+ @article{kang2023libriheavy,
20
+ title={Libriheavy: a 50,000 hours asr corpus with punctuation casing and context},
21
+ author={Kang, Wei and Yang, Xiaoyu and Yao, Zengwei and Kuang, Fangjun and Yang, Yifan and Guo, Liyong and Lin, Long and Povey, Daniel},
22
+ journal={arXiv preprint arXiv:2309.08105},
23
+ year={2023}
24
+ }
25
+ """
26
+
27
+ class LibriheavyConfig(datasets.BuilderConfig):
28
+ """BuilderConfig for Libriheavy."""
29
+
30
+ def __init__(self, **kwargs):
31
+ """BuilderConfig for Libriheavy.
32
+ Args:
33
+ **kwargs: keyword arguments forwarded to super.
34
+ """
35
+ super(LibriheavyConfig, self).__init__(**kwargs)
36
+
37
+
38
+ class Libriheavy(datasets.GeneratorBasedBuilder):
39
+ """Libriheavy dataset."""
40
+
41
+ BUILDER_CONFIGS = [
42
+ LibriheavyConfig(name="libriheavy", version=datasets.Version("1.0.0"), description="Libriheavy dataset."),
43
+ ]
44
+
45
+ def _info(self):
46
+ return datasets.DatasetInfo(
47
+ description=_DESCRIPTION,
48
+ features=datasets.Features(
49
+ {
50
+ "id": datasets.Value("string"),
51
+ "speaker_id": datasets.Value("string"),
52
+ "audio": datasets.Value("string"),
53
+ "text": datasets.Value("string"),
54
+ "word_segments": datasets.Sequence(
55
+ {
56
+ "start": datasets.Value("int32"),
57
+ "end": datasets.Value("int32"),
58
+ "word": datasets.Value("string"),
59
+ }
60
+ ),
61
+ "mel_spectrogram": datasets.Sequence(datasets.Sequence(datasets.Value("float32"))),
62
+ }
63
+ ),
64
+ supervised_keys=None,
65
+ homepage=_URL,
66
+ citation=_CITATION,
67
+ )
68
+
69
+ def _split_generators(self, dl_manager):
70
+ """Returns SplitGenerators."""
71
+ # first, we load speaker_list.json
72
+ speaker_list = "medium_data/speaker_list.json"
73
+ speaker_list = dl_manager.download_and_extract(speaker_list)
74
+ with open(speaker_list, "r") as f:
75
+ speaker_list = json.load(f)
76
+ # now we load the individual speaker metadata
77
+ speaker_metadata = {}
78
+ for speaker_id, metadata_path in speaker_list.items():
79
+ metadata_path = f"medium_data/{metadata_path}"
80
+ metadata_path = dl_manager.download_and_extract(metadata_path)
81
+ with open(metadata_path, "r") as f:
82
+ speaker_metadata[speaker_id] = json.load(f)
83
+ speaker_chunks = []
84
+ for speaker_id, metadata in speaker_metadata.items():
85
+ for chunk_id, chunk in metadata["chunks"].items():
86
+ speaker_chunks.append(
87
+ {
88
+ "speaker_id": speaker_id,
89
+ "id": f"{speaker_id}_{chunk_id}",
90
+ "audio": dl_manager.download(f"medium_data/{chunk['npz'].replace('.gz', '')}"),
91
+ "text": dl_manager.download(f"medium_data/{chunk['json']}"),
92
+ }
93
+ )
94
+ # shuffle the chunks
95
+ np.random.seed(42)
96
+ np.random.shuffle(speaker_chunks)
97
+ return [
98
+ datasets.SplitGenerator(
99
+ name=datasets.Split.TRAIN,
100
+ gen_kwargs={"speaker_chunks": speaker_chunks},
101
+ )
102
+ ]
103
+
104
+ def _generate_examples(self, speaker_chunks):
105
+ """Yields examples."""
106
+ for chunk in speaker_chunks:
107
+ npz = dict(np.load(chunk["audio"], allow_pickle=True))
108
+ utterances = npz.keys()
109
+ with gzip.open(chunk["text"], "rt") as f:
110
+ text = json.load(f)
111
+ for utterance_id, utterance in text.items():
112
+ result = {
113
+ "id": chunk["speaker_id"] + "_" + utterance_id,
114
+ "speaker_id": chunk["speaker_id"],
115
+ "audio": chunk["audio"],
116
+ "text": chunk["text"],
117
+ "word_segments": [
118
+ {"start": segment[0], "end": segment[1], "word": segment[2]} for segment in utterance["word_segments"]
119
+ ],
120
+ "mel_spectrogram": npz[str(utterance_id)].item()["mel"][0][0],
121
+ }
122
+ yield chunk["speaker_id"] + "_" + utterance_id, result