File size: 6,467 Bytes
efb2aa0
 
 
 
 
 
 
 
 
 
 
 
 
 
35f7906
efb2aa0
 
 
 
 
 
 
694387f
efb2aa0
 
 
 
 
 
 
 
694387f
efb2aa0
694387f
efb2aa0
 
 
 
 
 
 
 
 
 
 
694387f
 
efb2aa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

import datasets
import os


_DESCRIPTION = "MGB2 speech recognition dataset AR"
_HOMEPAGE = "https://arabicspeech.org/mgb2/"
_LICENSE = "MGB-2 License agreement"





_DATA_ARCHIVE_ROOT = "https://huggingface.co/datasets/taqwa92/mg.trial4/tree/main/audio/ar/test"
_DATA_ARCHIVE_ROOT1 ="https://huggingface.co/datasets/taqwa92/mg.trial4/tree/main/sentence/ar/test"

_DATA_URL = {
    "test": _DATA_ARCHIVE_ROOT + "test.zip",
    
}

_TEXT_URL = {
    "test": _DATA_ARCHIVE_ROOT1 + "text.tsv",
    

class MGDB2Dataset(datasets.GeneratorBasedBuilder):
    def _info(self):
        return datasets.DatasetInfo(
        description=_DESCRIPTION,
        features=datasets.Features(
            {
                
                "audio": datasets.Audio(sampling_rate=16_000),
                "sentence": datasets.Value("string"),
            }
        ),
        supervised_keys=None,
        homepage=_HOMEPAGE,
        license=_LICENSE,
       
    )

    def _split_generators(self, dl_manager):
        wav_archive = dl_manager.download(_DATA_URL)
        txt_archive = dl_manager.download(_TEXT_URL)
        test_dir = "audio/ar/test"
        


        print("Starting write datasets.........................................................")
        
        
        if dl_manager.is_streaming:
            print("from streaming.........................................................")



            
            return [
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "path_to_txt": test_dir + "/txt",
                    "path_to_wav": test_dir + "/wav",
                    "wav_files": dl_manager.iter_archive(wav_archive['test']),
                    "txt_files": dl_manager.iter_archive(txt_archive['test']),
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "path_to_txt": dev_dir + "/txt",
                    "path_to_wav": dev_dir + "/wav",
                    "wav_files": dl_manager.iter_archive(wav_archive['dev']),
                    "txt_files": dl_manager.iter_archive(txt_archive['dev']),
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "path_to_txt": train_dir + "/txt",
                    "path_to_wav": train_dir + "/wav",
                    "wav_files": dl_manager.iter_archive(wav_archive['train']),
                    "txt_files": dl_manager.iter_archive(txt_archive['train']),
                },
            ),
        ]
        else:
            print("from non streaming.........................................................")


            test_txt_files=dl_manager.extract(txt_archive['test']);
            print("txt file list .....................................",txt_archive['test'])


            print("txt file names .....................................",test_txt_files)

            
            return [
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "path_to_txt": test_dir + "/txt",
                    "path_to_wav": test_dir + "/wav",
                    "wav_files": dl_manager.extract(wav_archive['test']),
                    "txt_files": test_txt_files,
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "path_to_txt": dev_dir + "/txt",
                    "path_to_wav": dev_dir + "/wav",
                    "wav_files": dl_manager.extract(wav_archive['dev']),
                    "txt_files": dl_manager.extract(txt_archive['dev']),
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "path_to_txt": train_dir + "/txt",
                    "path_to_wav": train_dir + "/wav",
                    "wav_files": dl_manager.extract(wav_archive['train']),
                    "txt_files": dl_manager.extract(txt_archive['train']),
                },
            ),
        ]
    print("end of generation.........................................................")
      


    
    def _generate_examples(self, path_to_txt, path_to_wav, wav_files, txt_files):
        """ 
        This assumes that the text directory alphabetically precedes the wav dir
        The file names for wav and text seem to match and are unique
        We can use them for the dictionary matching them
        """

        print("start of generate examples.........................................................")

        print("txt file names............................",txt_files)
        print("wav_files names....................................",wav_files)

        examples = {}
        id_ = 0
        # need to prepare the transcript - wave map
        for item in txt_files:


            print("copying txt file...............",item)
      
            if type(item) is tuple:
                # iter_archive will return path and file
                path, f = item
                txt = f.read().decode(encoding="utf-8").strip()
            else:
                # extract will return path only
                path = item
                with open(path, encoding="utf-8") as f:
                    txt = f.read().strip()

            if path.find(path_to_txt) > -1:
                # construct the wav path
                # which is used as an identifier
                wav_path = os.path.split(path)[1].replace("_utf8", "").replace(".txt", ".wav").strip()
                
                examples[wav_path] = {
                    "text": txt,
                    "path": wav_path,
                }

        for wf in wav_files:
            for item in wf:
                if type(item) is tuple:
                    path, f = item
                    wav_data = f.read()
                else:
                    path = item
                    with open(path, "rb") as f:
                        wav_data = f.read()
                if path.find(path_to_wav) > -1:
                    wav_path = os.path.split(path)[1].strip()
                    audio = {"path": path, "bytes": wav_data}
                    yield id_, {**examples[wav_path], "audio": audio}
                    id_ += 1