File size: 3,135 Bytes
30411f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Copyright 2024 RobotsMali AI4D Lab.

Licensed under the Creative Commons Attribution 4.0 International License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://creativecommons.org/licenses/by/4.0/

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from datasets import load_dataset
from huggingface_hub import login
import os
import json
import soundfile as sf
import numpy as np

# Log in to Hugging Face
login()

# Load the clean subset of the dataset
oza_bam_asr_clean = load_dataset("oza75/bambara-asr", name="clean")

# Define root directory for the Nemo version
root_dir = "oza-bam-asr-clean"
os.makedirs(root_dir, exist_ok=True)
os.makedirs(f"{root_dir}/audios", exist_ok=True)
os.makedirs(f"{root_dir}/manifests", exist_ok=True)
os.makedirs(f"{root_dir}/french-manifests", exist_ok=True)

def save_audio_and_create_manifest(datasets, manifest_path, french_manifest_path):
    total_duration = 0
    with open(manifest_path, "w", encoding="utf-8") as manifest_file, open(french_manifest_path, "w", encoding="utf-8") as french_file:
        idx = 0
        for dataset in datasets:
            for example in dataset:
                # Save audio to .wav file
                audio_path = f"{root_dir}/audios/oza75-bam-asr-{idx}.wav"
                sf.write(audio_path, np.array(example['audio']['array']), example['audio']['sampling_rate'])

                # Calculate duration and accumulate
                duration = example['duration']
                total_duration += duration

                # Create manifest entries
                manifest_entry = {
                    "audio_filepath": os.path.relpath(audio_path),
                    "duration": duration,
                    "text": example['bambara'].lower()
                }
                french_manifest_entry = {
                    "audio_filepath": os.path.relpath(audio_path),
                    "duration": duration,
                    "text": example['french'].lower()
                }

                # Write to manifest files
                manifest_file.write(json.dumps(manifest_entry, ensure_ascii=False) + "\n")
                french_file.write(json.dumps(french_manifest_entry, ensure_ascii=False) + "\n")
                
                idx += 1

    return total_duration

if __name__ == "__main__":
    # Combine train and test sets into one
    total_duration = save_audio_and_create_manifest(
        [oza_bam_asr_clean['train'], oza_bam_asr_clean['test']],
        f"{root_dir}/manifests/train_manifest.json",
        f"{root_dir}/french-manifests/train_french_manifest.json"
    )

    # Convert duration to hours
    total_hours = total_duration / 3600

    # Print the result
    print(f"Created Nemo manifest for 'oza75/bambara-asr' totalling {total_hours:.2f} hours of audio.")