|
""" |
|
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 |
|
|
|
|
|
login() |
|
|
|
|
|
oza_bam_asr_clean = load_dataset("oza75/bambara-asr", name="clean") |
|
|
|
|
|
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: |
|
|
|
audio_path = f"{root_dir}/audios/oza75-bam-asr-{idx}.wav" |
|
sf.write(audio_path, np.array(example['audio']['array']), example['audio']['sampling_rate']) |
|
|
|
|
|
duration = example['duration'] |
|
total_duration += duration |
|
|
|
|
|
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() |
|
} |
|
|
|
|
|
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__": |
|
|
|
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" |
|
) |
|
|
|
|
|
total_hours = total_duration / 3600 |
|
|
|
|
|
print(f"Created Nemo manifest for 'oza75/bambara-asr' totalling {total_hours:.2f} hours of audio.") |
|
|