File size: 4,739 Bytes
2e83e61 |
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 103 104 105 106 107 |
import os
from datasets import DatasetDict, Audio
import pandas as pd
from datasets.table import embed_table_storage
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("main_folder_path", type=str, help="Path of the base mls folder")
parser.add_argument("configuration", type=str, help="Dataset configuration to use, if necessary. Here corresponds to the language name.")
parser.add_argument("output_dir", type=str, help="Save the dataset on disk with this path.")
parser.add_argument("--cpu_num_workers", default=1, type=int, help="Number of CPU workers.")
parser.add_argument("--csv_folder_path", default=None, type=str, help="Path where to save intermediate csv, by default will be main_foldr_path")
parser.add_argument("--repo_id", default="facebook/multilingual_librispeech", type=str, help="Push the dataset to the hub.")
args = parser.parse_args()
main_folder_path = args.main_folder_path
csv_folder_path = args.csv_folder_path if args.csv_folder_path is not None else main_folder_path
if not os.path.exists(csv_folder_path):
os.makedirs(csv_folder_path)
splits = ["dev", "test", "train"]
# total_length_per_split = 10_000 * 60 * 60 # in sec -> 10k hours
csv_dict = {}
for split in splits:
segment_path = os.path.join(main_folder_path, split, "segments.txt")
transcript_path = os.path.join(main_folder_path, split, "transcripts.txt")
segments = pd.read_csv(segment_path, sep='\t', names=["audio", "original_path", "begin_time", "end_time"],
index_col="audio")
transcripts = pd.read_csv(transcript_path, sep='\t', names=["audio", "transcript"], index_col="audio")
df = pd.concat([segments, transcripts], axis=1, join="inner")
print(
f"Segments and transcripts of {split} has been joined: new length {len(df)}, old lengths {(len(segments), len(transcripts))}")
# add audio duration
df["audio_duration"] = df["end_time"] - df["begin_time"]
df["split"] = split
print(f"len df {len(df)}")
df.to_csv(os.path.join(csv_folder_path, f"{split}.csv"))
csv_dict[split] = os.path.join(csv_folder_path, f"{split}.csv")
# take care of /limited_supervision
if split == "train":
nine_hours_segment_path = os.path.join(main_folder_path, "train/limited_supervision/9hr/handles.txt")
nine_hours_segment = pd.read_csv(nine_hours_segment_path, sep='\t', names=["audio"], index_col="audio").index
nine_hours_df = df.filter(items=nine_hours_segment, axis=0)
nine_hours_df.to_csv(os.path.join(csv_folder_path, f"9_hours.csv"))
csv_dict["9_hours"] = os.path.join(csv_folder_path, f"9_hours.csv")
one_hours_segments = [ os.path.join(f.path, "handles.txt") for f in os.scandir( os.path.join(main_folder_path, "train/limited_supervision/1hr")) if f.is_dir()]
one_hours_segments = pd.concat([pd.read_csv(one, sep='\t', names=["audio"], index_col="audio") for one in one_hours_segments], axis=0).index
one_hours_df = df.filter(items=one_hours_segments, axis=0)
one_hours_df.to_csv(os.path.join(csv_folder_path, f"1_hours.csv"))
csv_dict["1_hours"] = os.path.join(csv_folder_path, f"1_hours.csv")
dataset = DatasetDict.from_csv(csv_dict)
def extract_speaker_id_and_format_path(audio, split):
speaker_id = audio.split("_")[0]
chapter_id = audio.split("_")[1]
file = f"{audio}.opus"
path = os.path.join(main_folder_path, split, "audio", speaker_id, chapter_id, file)
return {"audio": path, "speaker_id": speaker_id, "chapter_id": chapter_id, "file": file, "id": audio}
# correct audio path
dataset = dataset.map(extract_speaker_id_and_format_path, input_columns=["audio", "split"], num_proc=args.cpu_num_workers, remove_columns=["split"])
dataset = dataset.cast_column("audio", Audio())
print(dataset)
print(dataset["dev"][0])
print("Embed table storage")
# load_dataset(...)
format = dataset["train"].format
dataset = dataset.with_format("arrow")
dataset = dataset.map(embed_table_storage, batched=True, num_proc=args.cpu_num_workers)
dataset = dataset.with_format(**format)
dataset.save_to_disk(args.output_dir, num_proc=args.cpu_num_workers)
if args.repo_id:
pushed = False
while not pushed:
try:
dataset.push_to_hub(args.repo_id, args.configuration, revision="refs/pr/15")
pushed = True
except:
pass
|