Datasets:
Download TurnMaster_processing/extract_shards.py from turnmaster/TurnMaster: direct link, hf CLI and curl.
- Browser
- Download file 3.02 kB
-
https://huggingface.co/datasets/turnmaster/TurnMaster/resolve/main/TurnMaster_processing/extract_shards.py
- Command line
-
hf download hf://datasets/turnmaster/TurnMaster/TurnMaster_processing/extract_shards.py
-
curl -L -o extract_shards.py https://huggingface.co/datasets/turnmaster/TurnMaster/resolve/main/TurnMaster_processing/extract_shards.py
3.02 kB
| """ | |
| Extract tar shards back to original structure | |
| """ | |
| import argparse | |
| import tarfile | |
| import csv | |
| from pathlib import Path | |
| from tqdm import tqdm | |
| from collections import defaultdict | |
| import shutil | |
| def parse_args(): | |
| parser = argparse.ArgumentParser( | |
| description="Extract tar shards to recreate dataset" | |
| ) | |
| parser.add_argument( | |
| "--sharded-dir", | |
| type=Path, | |
| default=".", | |
| help="Directory containing audio/ and metadata/ folders" | |
| ) | |
| parser.add_argument( | |
| "--output-dir", | |
| type=Path, | |
| default="extracted", | |
| help="Output directory for extracted dataset" | |
| ) | |
| return parser.parse_args() | |
| def main(): | |
| args = parse_args() | |
| audio_dir = args.sharded_dir / "audio" | |
| metadata_file = args.sharded_dir / "metadata" / "metadata.csv" | |
| if not audio_dir.exists(): | |
| raise FileNotFoundError(f"Audio directory not found: {audio_dir}") | |
| if not metadata_file.exists(): | |
| raise FileNotFoundError(f"Metadata not found: {metadata_file}") | |
| # Read metadata to understand structure | |
| print("Reading metadata...") | |
| with open(metadata_file, 'r', encoding='utf-8') as f: | |
| reader = csv.DictReader(f) | |
| rows = list(reader) | |
| # Group by subset and shard | |
| shards_by_subset = defaultdict(set) | |
| for row in rows: | |
| subset = row['subset'] | |
| shard = row['shard'] | |
| shards_by_subset[subset].add(shard) | |
| print(f"Found {len(rows):,} samples across {len(shards_by_subset)} subsets") | |
| # Extract all shards | |
| for subset in sorted(shards_by_subset.keys()): | |
| print(f"\nExtracting {subset}...") | |
| subset_output = args.output_dir / subset / "audio" | |
| subset_output.mkdir(parents=True, exist_ok=True) | |
| shards = sorted(shards_by_subset[subset]) | |
| for shard_name in tqdm(shards, desc=f" {subset}"): | |
| shard_path = audio_dir / subset / shard_name | |
| if not shard_path.exists(): | |
| print(f"Warning: {shard_path} not found, skipping") | |
| continue | |
| # Extract tar | |
| with tarfile.open(shard_path, 'r') as tar: | |
| tar.extractall(path=subset_output) | |
| metadata_dir = Path(args.sharded_dir) / "metadata" | |
| output_dir = Path(args.output_dir) | |
| # Create the output directory if it does not exist | |
| output_dir.mkdir(parents=True, exist_ok=True) | |
| # Copy every CSV file | |
| for csv_file in metadata_dir.glob("*.csv"): | |
| destination = output_dir / csv_file.name | |
| shutil.copy2(csv_file, destination) | |
| print("\n" + "="*60) | |
| print("Extraction complete!") | |
| print("="*60) | |
| print(f"Output directory: {args.output_dir}") | |
| print(f"\nTo process the dataset:") | |
| print(f"python process_dataset.py \\") | |
| print(f" --taskmaster-root-dir {args.output_dir} \\") | |
| print(f" --aug-dataset-save-dir ./processed \\") | |
| print(f" --normalize-text") | |
| if __name__ == "__main__": | |
| main() | |