| from datasets import DatasetInfo, GeneratorBasedBuilder, Split, SplitGenerator, Value, Audio, Features |
| import os |
| import csv |
|
|
| class MyDataset(GeneratorBasedBuilder): |
| def _info(self): |
| return DatasetInfo( |
| features=Features({ |
| "audio": Audio(sampling_rate=16_000), |
| "text": Value("string") |
| }), |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| audio_dir = os.path.join(self.config.data_dir, "audio") |
| metadata_path = os.path.join(self.config.data_dir, "metadata.csv") |
|
|
| return [ |
| SplitGenerator( |
| name=Split.TRAIN, |
| gen_kwargs={ |
| "metadata_path": metadata_path, |
| "audio_dir": audio_dir |
| }, |
| ) |
| ] |
|
|
| def _generate_examples(self, metadata_path, audio_dir): |
| with open(metadata_path, encoding="utf-8") as f: |
| reader = csv.DictReader(f) |
| for idx, row in enumerate(reader): |
| yield idx, { |
| "audio": os.path.join(audio_dir, row["file"]), |
| "text": row["text"] |
| } |