File size: 1,497 Bytes
5f85b2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import os
import datasets

class AudioTextDataset(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")
    
    def _info(self):
        return datasets.DatasetInfo(
            features=datasets.Features({
                "audio": datasets.Audio(sampling_rate=22050),
                "text": datasets.Value("string")
            })
        )
    
    def _split_generators(self, dl_manager):
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"split": "train"}
            )
        ]
    
    def _generate_examples(self, split):
        audio_dir = os.path.join(os.path.dirname(__file__), "audio")
        audio_files = sorted([f for f in os.listdir(audio_dir) if f.endswith(".wav")])
        
        # Load the mapping of audio files to text
        text_mapping = {}
        mapping_path = os.path.join(os.path.dirname(__file__), "mapping.txt")
        with open(mapping_path, "r", encoding="utf-8") as f:
            for line in f:
                parts = line.strip().split("\t")
                if len(parts) == 2:
                    audio_file, text = parts
                    text_mapping[audio_file] = text
        
        for idx, audio_file in enumerate(audio_files):
            if audio_file in text_mapping:
                yield idx, {
                    "audio": os.path.join(audio_dir, audio_file),
                    "text": text_mapping[audio_file]
                }