bn_audio / bn_audio.py
emon-j's picture
Update bn_audio.py
041cbb7
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)