| import datasets |
| import tarfile |
| import io |
| import json |
| import soundfile as sf |
| import os |
| from pathlib import Path |
|
|
| class TestDS1(datasets.GeneratorBasedBuilder): |
| VERSION = datasets.Version("1.0.0") |
|
|
| def _info(self): |
| return datasets.DatasetInfo( |
| description="Dataset of audiobook chunks with audio and metadata from multiple books.", |
| features=datasets.Features( |
| { |
| "audio": datasets.Audio(sampling_rate=16000), |
| "audio_path": datasets.Value("string"), |
| "start_seconds": datasets.Value("float32"), |
| "end_seconds": datasets.Value("float32"), |
| "duration_seconds": datasets.Value("float32"), |
| "text": datasets.Value("string"), |
| "book_id": datasets.Value("string"), |
| } |
| ), |
| supervised_keys=None, |
| homepage="https://huggingface.co/datasets/jtarjanyi/testds1", |
| citation="", |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| base_url = "https://huggingface.co/datasets/jtarjanyi/testds1/resolve/main" |
| book_folders = ["Buszkeseg_1"] |
| tar_paths = {} |
|
|
| for book in book_folders: |
| tar_url = f"{base_url}/{book}/chunks.tar" |
| print(f"Downloading tar file: {tar_url}") |
| tar_paths[book] = dl_manager.download(tar_url) |
|
|
| return [ |
| datasets.SplitGenerator( |
| name=datasets.Split.TRAIN, |
| gen_kwargs={"tar_paths": tar_paths}, |
| ), |
| ] |
|
|
| def _generate_examples(self, tar_paths): |
| idx = 0 |
| for book_id, tar_path in tar_paths.items(): |
| print(f"Processing tar file for book: {book_id} at {tar_path}") |
| try: |
| with tarfile.open(tar_path, "r") as tar: |
| json_files = sorted([f.name for f in tar.getmembers() if f.name.endswith(".json")]) |
| print(f"Found {len(json_files)} JSON files in {book_id}") |
|
|
| for json_filename in json_files: |
| try: |
| json_file = tar.extractfile(json_filename) |
| if json_file is None: |
| print(f"Skipping {json_filename}: Could not extract from {tar_path}") |
| continue |
| json_data = json.load(json_file) |
| print(f"Loaded JSON: {json_filename}") |
|
|
| wav_filename = json_data["audio_path"] |
| if not wav_filename.endswith(".wav"): |
| print(f"Skipping {wav_filename}: Invalid audio path in {json_filename}") |
| continue |
|
|
| try: |
| wav_member = tar.getmember(wav_filename) |
| except KeyError: |
| print(f"Skipping {wav_filename}: Not found in {tar_path}") |
| continue |
|
|
| wav_file = tar.extractfile(wav_member) |
| if wav_file is None: |
| print(f"Skipping {wav_filename}: Could not extract from {tar_path}") |
| continue |
|
|
| wav_bytes = wav_file.read() |
| print(f"Extracted {wav_filename}: {len(wav_bytes)} bytes") |
|
|
| wav_io = io.BytesIO(wav_bytes) |
| _, sampling_rate = sf.read(wav_io) |
| print(f"Sampling rate for {wav_filename}: {sampling_rate}") |
|
|
| yield idx, { |
| "audio": {"bytes": wav_bytes, "sampling_rate": sampling_rate}, |
| "audio_path": wav_filename, |
| "start_seconds": json_data["start_seconds"], |
| "end_seconds": json_data["end_seconds"], |
| "duration_seconds": json_data["duration_seconds"], |
| "text": json_data["text"], |
| "book_id": book_id, |
| } |
| idx += 1 |
|
|
| except Exception as e: |
| print(f"Error processing {json_filename} in {tar_path}: {str(e)}") |
| continue |
| except Exception as e: |
| print(f"Error opening tar file {tar_path}: {str(e)}") |
| continue |