File size: 3,447 Bytes
97bc446
 
 
dcd1ccf
97bc446
 
 
 
5d4d1a8
d11a360
97bc446
 
 
5d4d1a8
97bc446
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f94502
 
dae32d3
8f94502
97bc446
983cbca
b431334
983cbca
5d4d1a8
8f94502
5d4d1a8
983cbca
b389bfd
d11a360
 
 
 
 
 
 
 
97bc446
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9786c09
97bc446
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import csv
import datasets
from datasets import BuilderConfig, GeneratorBasedBuilder, DatasetInfo, SplitGenerator, Split
import tarfile



_PROMPTS_URLS = {
    "train": "segmented_audios.csv",
    "validation": "validation.csv",
}

_ARCHIVES = {
    "train": "nurc-sp_corpus_minimo.tar.gz", 
}


class NurcSPConfig(BuilderConfig):
    def __init__(self, prompts_type="original", **kwargs):
        super().__init__(**kwargs)
        self.prompts_type = prompts_type


class NurcSPDataset(GeneratorBasedBuilder):
    def _info(self):
        return DatasetInfo(
            features=datasets.Features(
                {
                    "path": datasets.Value("string"),
                    "name": datasets.Value("string"),
                    "speaker": datasets.Value("string"),
                    "start_time": datasets.Value("string"),
                    "end_time": datasets.Value("string"),
                    "normalized_text": datasets.Value("string"),
                    "text": datasets.Value("string"),
                    "audio": datasets.Audio(sampling_rate=16_000),
                }
            )
        )

    def _split_generators(self, dl_manager):
        prompts_path = dl_manager.download(_PROMPTS_URLS)
        archive = dl_manager.download(_ARCHIVES)
    
        # Define the path_to_clips variable, pointing to the directory where the audio clips are stored
        path_to_clips = "segmented_audios"  # Update this with the actual path
    
        return [
            SplitGenerator(
                name=Split.TRAIN,  # Single split
                gen_kwargs={
                    "prompts_path": prompts_path["train"],
                    "path_to_clips": path_to_clips,
                    "audio_files": dl_manager.iter_archive(archive["train"]),
                }
            ),
            SplitGenerator(
                name=Split.VALIDATION,  # Single split
                gen_kwargs={
                    "prompts_path": prompts_path["validation"],
                    "path_to_clips": path_to_clips,
                    "audio_files": dl_manager.iter_archive(archive["train"]),
                }
            ),
        ]

    def _generate_examples(self, prompts_path, path_to_clips, audio_files):
        examples = {}
        with open(prompts_path, "r") as f:
            csv_reader = csv.DictReader(f)
            for row in csv_reader:
                path = row['path']
                name = row['name']
                speaker = row['speaker']
                start_time = row['start_time']
                end_time = row['end_time']
                normalized_text = row['normalized_text']
                text = row['text']
                examples[path] = {
                    "path": path,
                    "name": name,
                    "speaker": speaker,
                    "start_time": start_time,
                    "end_time": end_time,
                    "normalized_text": normalized_text,
                    "text": text,
                }
        inside_clips_dir = False
        id_ = 0
        for path, f in audio_files:
            if path.startswith(path_to_clips):
                inside_clips_dir = True
                if path in examples:
                    audio = {"path": path, "bytes": f.read()}
                    yield id_, {**examples[path], "audio": audio}
                    id_ += 1
            elif inside_clips_dir:
                break