File size: 3,024 Bytes
ec26866
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

"""
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()