youtube-persian-asr / asr_dataset.py
PerSets's picture
fix: trying to pair metadata and tarfile audios
9a10e37
raw
history blame
4.69 kB
import os
import tarfile
import pandas as pd
import datasets
from datasets import Audio, Value, Features
import logging
from typing import Dict, Generator, Tuple
logger = logging.getLogger(__name__)
_DESCRIPTION = """
This dataset consists of various Youtube videos in Persian language.
Note: This dataset contains raw, unvalidated transcriptions. Users are advised to:
1. Perform their own quality assessment
2. Create their own train/validation/test splits based on their specific needs
3. Validate a subset of the data if needed for their use case
"""
_CITATION = """
Use this repo info/link for citation.
"""
_LICENSE = "MIT"
_DATA_URL = [
"clips/clips_001.tar",
"clips/clips_002.tar",
"clips/clips_003.tar",
"clips/clips_004.tar",
"clips/clips_005.tar",
"clips/clips_006.tar",
"clips/clips_007.tar",
"clips/clips_008.tar",
"clips/clips_009.tar",
"clips/clips_010.tar",
"clips/clips_011.tar",
"clips/clips_012.tar",
"clips/clips_013.tar",
"clips/clips_014.tar",
"clips/clips_015.tar",
"clips/clips_016.tar",
"clips/clips_017.tar",
"clips/clips_018.tar",
"clips/clips_019.tar",
"clips/clips_020.tar",
"clips/clips_021.tar",
]
#_DATA_URL = ["ytDataset/" + x for x in _DATA_URL][:2]
_PROMPTS_URLS = {
#"train": "ytDataset/clips/metadata.csv"
"train": "clips/metadata.csv"
}
class ASRDataset(datasets.GeneratorBasedBuilder):
"""ASR dataset with audio files stored in tar archives."""
VERSION = datasets.Version("1.0.0")
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features({
"file_name": datasets.Value("string"),
"audio": datasets.Audio(sampling_rate=16000),
"sentence": datasets.Value("string"),
#"tar_file": datasets.Value("string"),
}),
supervised_keys=None,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators with added error handling."""
prompts_paths = dl_manager.download_and_extract(_PROMPTS_URLS)
archive = dl_manager.download(_DATA_URL)
train_dir = "clips"
try:
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
#"split": "train",
#"data_dir": self.config.data_dir,
"prompts_path": prompts_paths["train"],
"path_to_clips": train_dir,
"audio_files": dl_manager.iter_archive(archive)
},
),
]
except Exception as e:
logger.error(f"Error in _split_generators: {e}")
logger.error(traceback.format_exc())
raise
def _generate_examples(self, prompts_path, path_to_clips, audio_files):
"""Yields examples as (key, example) tuples."""
logger.info('hey there!')
examples = {}
with open(prompts_path, encoding="utf-8") as f:
for row in f:
data = row.strip().split("\t", 1)
file_name = data[0].strip()
audio_path = "/".join([path_to_clips, file_name])
examples[audio_path] = {
"path": audio_path,
"sentence": data[1],
}
inside_clips_dir = False
id_ = 0
tar_files = [
"clips/clips_001.tar",
"clips/clips_002.tar",
"clips/clips_003.tar",
"clips/clips_004.tar",
"clips/clips_005.tar",
"clips/clips_006.tar",
"clips/clips_007.tar",
"clips/clips_008.tar",
"clips/clips_009.tar",
"clips/clips_010.tar",
"clips/clips_011.tar",
"clips/clips_012.tar",
"clips/clips_013.tar",
"clips/clips_014.tar",
"clips/clips_015.tar",
"clips/clips_016.tar",
"clips/clips_017.tar",
"clips/clips_018.tar",
"clips/clips_019.tar",
"clips/clips_020.tar",
"clips/clips_021.tar",
]
for tar_file in tar_files:
with tarfile.open(tar_file, 'r') as tar:
for member in tar.getmembers():
path = "/".join([path_to_clips, member.name])
audio_file =tar.extractfile(member)
audio = {"path": path, "bytes": audio_file.read()}
yield id_, {**examples[path], "audio": audio}