jeli-asr / scripts /create_manifest_oza_bam_asr.py
diarray's picture
Publish Jeli-ASR version 1.0.0
30411f8
raw
history blame
3.14 kB
"""
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.")