2024-04-21 22:43:20 +00:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import torch
|
|
|
|
import torchaudio
|
|
|
|
import whisperx
|
|
|
|
|
|
|
|
from tqdm.auto import tqdm
|
|
|
|
from pathlib import Path
|
|
|
|
|
2024-04-29 03:28:29 +00:00
|
|
|
# should be args
|
2024-04-21 22:43:20 +00:00
|
|
|
batch_size = 16
|
2024-04-29 03:28:29 +00:00
|
|
|
device = "cuda"
|
2024-04-21 22:43:20 +00:00
|
|
|
dtype = "float16"
|
2024-04-29 03:28:29 +00:00
|
|
|
model_name = "large-v3"
|
2024-04-21 22:43:20 +00:00
|
|
|
|
2024-04-29 03:28:29 +00:00
|
|
|
input_audio = "voices"
|
2024-04-21 22:43:20 +00:00
|
|
|
output_dataset = "metadata"
|
|
|
|
|
2024-04-29 03:28:29 +00:00
|
|
|
skip_existing = True
|
|
|
|
diarize = False
|
2024-04-21 22:43:20 +00:00
|
|
|
|
2024-04-29 03:28:29 +00:00
|
|
|
#
|
|
|
|
model = whisperx.load_model(model_name, device, compute_type=dtype)
|
2024-04-21 22:43:20 +00:00
|
|
|
align_model, align_model_metadata, align_model_language = (None, None, None)
|
2024-04-29 03:28:29 +00:00
|
|
|
if diarize:
|
|
|
|
diarize_model = whisperx.DiarizationPipeline(device=device)
|
|
|
|
else:
|
|
|
|
diarize_model = None
|
2024-04-21 22:43:20 +00:00
|
|
|
|
|
|
|
def pad(num, zeroes):
|
|
|
|
return str(num).zfill(zeroes+1)
|
|
|
|
|
|
|
|
for dataset_name in os.listdir(f'./{input_audio}/'):
|
|
|
|
if not os.path.isdir(f'./{input_audio}/{dataset_name}/'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
for speaker_id in tqdm(os.listdir(f'./{input_audio}/{dataset_name}/'), desc="Processing speaker"):
|
|
|
|
if not os.path.isdir(f'./{input_audio}/{dataset_name}/{speaker_id}'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
outpath = Path(f'./{output_dataset}/{dataset_name}/{speaker_id}/whisper.json')
|
|
|
|
|
|
|
|
if outpath.exists():
|
|
|
|
metadata = json.loads(open(outpath, 'r', encoding='utf-8').read())
|
|
|
|
else:
|
|
|
|
os.makedirs(f'./{output_dataset}/{dataset_name}/{speaker_id}/', exist_ok=True)
|
|
|
|
metadata = {}
|
|
|
|
|
|
|
|
for filename in tqdm(os.listdir(f'./{input_audio}/{dataset_name}/{speaker_id}/'), desc=f"Processing speaker: {speaker_id}"):
|
|
|
|
|
|
|
|
if skip_existing and filename in metadata:
|
|
|
|
continue
|
|
|
|
|
2024-04-29 03:28:29 +00:00
|
|
|
if ".json" in filename:
|
|
|
|
continue
|
|
|
|
|
2024-04-21 22:43:20 +00:00
|
|
|
inpath = f'./{input_audio}/{dataset_name}/{speaker_id}/{filename}'
|
2024-04-29 03:28:29 +00:00
|
|
|
|
|
|
|
if os.path.isdir(inpath):
|
|
|
|
continue
|
2024-04-21 22:43:20 +00:00
|
|
|
|
|
|
|
metadata[filename] = {
|
|
|
|
"segments": [],
|
|
|
|
"language": "",
|
2024-04-29 03:28:29 +00:00
|
|
|
"text": "",
|
|
|
|
"start": 0,
|
|
|
|
"end": 0,
|
2024-04-21 22:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
audio = whisperx.load_audio(inpath)
|
|
|
|
result = model.transcribe(audio, batch_size=batch_size)
|
|
|
|
language = result["language"]
|
|
|
|
|
2024-04-29 03:28:29 +00:00
|
|
|
if language[:2] not in ["ja"]:
|
|
|
|
language = "en"
|
|
|
|
|
2024-04-21 22:43:20 +00:00
|
|
|
if align_model_language != language:
|
|
|
|
tqdm.write(f'Loading language: {language}')
|
|
|
|
align_model, align_model_metadata = whisperx.load_align_model(language_code=language, device=device)
|
|
|
|
align_model_language = language
|
|
|
|
|
|
|
|
result = whisperx.align(result["segments"], align_model, align_model_metadata, audio, device, return_char_alignments=False)
|
|
|
|
|
|
|
|
metadata[filename]["segments"] = result["segments"]
|
|
|
|
metadata[filename]["language"] = language
|
|
|
|
|
2024-04-29 03:28:29 +00:00
|
|
|
if diarize_model is not None:
|
|
|
|
diarize_segments = diarize_model(audio)
|
|
|
|
result = whisperx.assign_word_speakers(diarize_segments, result)
|
|
|
|
|
2024-04-21 22:43:20 +00:00
|
|
|
text = []
|
2024-04-29 03:28:29 +00:00
|
|
|
start = 0
|
|
|
|
end = 0
|
2024-04-21 22:43:20 +00:00
|
|
|
for segment in result["segments"]:
|
|
|
|
text.append( segment["text"] )
|
2024-04-29 03:28:29 +00:00
|
|
|
start = min( start, segment["start"] )
|
|
|
|
end = max( end, segment["end"] )
|
2024-04-21 22:43:20 +00:00
|
|
|
|
|
|
|
metadata[filename]["text"] = " ".join(text).strip()
|
2024-04-29 03:28:29 +00:00
|
|
|
metadata[filename]["start"] = start
|
|
|
|
metadata[filename]["end"] = end
|
2024-04-21 22:43:20 +00:00
|
|
|
|
|
|
|
open(outpath, 'w', encoding='utf-8').write(json.dumps(metadata))
|