File size: 2,891 Bytes
72fb739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
041cbb7
72fb739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import csv
import datasets
from datasets import DownloadManager
import os

class JPDataset(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")
    dl_manager = DownloadManager()

    def _info(self): 
        return datasets.DatasetInfo(
            features=datasets.Features(
                {
                    "file_name": datasets.Value("string"),
                    "audio": datasets.Audio(sampling_rate=16_000),
                    "sentence": datasets.Value("string"),
                }
            ),
            supervised_keys=("audio", "sentence"),
        )
    def _split_generators(self, dl_manager):
        train_csv_path = dl_manager.download_and_extract("train.csv")
        valid_csv_path = dl_manager.download_and_extract("valid.csv")
        train_archive_path = dl_manager.download_and_extract("data/train.tar.gz")
        valid_archive_path = dl_manager.download_and_extract("https://storage.googleapis.com/hf-ds/valid.tar.gz")
        print(train_archive_path)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "csv_path": train_csv_path,
                    "archive_path": train_archive_path
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "csv_path": valid_csv_path,
                    "archive_path": valid_archive_path
                },
            ),
        ]



    def _generate_examples(self, csv_path, archive_path):
        examples = {}
        with open(csv_path, encoding="utf-8") as f:
            reader = csv.DictReader(f)
            for row in reader:
                # Replace backslashes with forward slashes in file paths
                file_name_with_prefix = row['file_name'].replace('\\', '/')
                examples[file_name_with_prefix] = {
                    "file_name": file_name_with_prefix,
                    "sentence": row['transcription'],
                }
        print("Keys in examples:", examples.keys())
        for root, dirs, files in os.walk(archive_path):
            for file in files:
                relative_file_path = os.path.relpath(os.path.join(root, file), archive_path).replace('\\', '/')
                print("Processing path:", relative_file_path)  
                if relative_file_path in examples:
                    # Open and read the file
                    with open(os.path.join(root, file), 'rb') as f:
                        audio_data = f.read()

                    example = examples[relative_file_path]
                    example['audio'] = {"bytes": audio_data, "sampling_rate": 16_000}
                    yield relative_file_path, example
                else:
                    print("File path not in examples:", relative_file_path)