forked from MahmoudAshraf97/whisper-diarization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diarize.py
178 lines (144 loc) · 5.2 KB
/
diarize.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import argparse
import os
from helpers import *
from faster_whisper import WhisperModel
import whisperx
import torch
import librosa
import soundfile
from nemo.collections.asr.models.msdd_models import NeuralDiarizer
from deepmultilingualpunctuation import PunctuationModel
import re
import logging
import json
mtypes = {'cpu': 'int8', 'cuda': 'float16'}
# Initialize parser
parser = argparse.ArgumentParser()
parser.add_argument(
"-a", "--audio", help="name of the target audio file", required=True
)
parser.add_argument(
"--no-stem",
action="store_false",
dest="stemming",
default=True,
help="Disables source separation."
"This helps with long files that don't contain a lot of music.",
)
parser.add_argument(
"--whisper-model",
dest="model_name",
default="medium.en",
help="name of the Whisper model to use",
)
parser.add_argument(
"--device",
dest="device",
default="cuda" if torch.cuda.is_available() else "cpu",
help="if you have a GPU use 'cuda', otherwise 'cpu'",
)
args = parser.parse_args()
if args.stemming:
# Isolate vocals from the rest of the audio
return_code = os.system(
f'python3 -m demucs.separate -n htdemucs --two-stems=vocals "{args.audio}" -o "temp_outputs"'
)
if return_code != 0:
logging.warning(
"Source splitting failed, using original audio file. Use --no-stem argument to disable it."
)
vocal_target = args.audio
else:
vocal_target = os.path.join(
"temp_outputs", "htdemucs", os.path.basename(args.audio[:-4]), "vocals.wav"
)
else:
vocal_target = args.audio
# convert audio to mono for NeMo combatibility
signal, sample_rate = librosa.load(vocal_target, sr=None)
ROOT = os.getcwd()
temp_path = os.path.join(ROOT, "temp_outputs")
os.makedirs(temp_path, exist_ok=True)
wav_file = os.path.join(temp_path, "mono_file.wav")
soundfile.write(wav_file, signal, sample_rate, "PCM_24")
# Run on GPU with FP16
whisper_model = WhisperModel(
args.model_name, device=args.device, compute_type=mtypes[args.device])
# or run on GPU with INT8
# model = WhisperModel(model_size, device="cuda", compute_type="int8_float16")
# or run on CPU with INT8
# model = WhisperModel(model_size, device="cpu", compute_type="int8")
segments, info = whisper_model.transcribe(
vocal_target, beam_size=1, word_timestamps=True
)
whisper_results = []
for segment in segments:
whisper_results.append(segment._asdict())
# clear gpu vram
del whisper_model
torch.cuda.empty_cache()
if info.language in wav2vec2_langs:
alignment_model, metadata = whisperx.load_align_model(
language_code=info.language, device=args.device
)
result_aligned = whisperx.align(
whisper_results, alignment_model, metadata, wav_file, args.device
)
word_timestamps = result_aligned["word_segments"]
# clear gpu vram
del alignment_model
torch.cuda.empty_cache()
else:
word_timestamps = []
for segment in whisper_results:
for word in segment["words"]:
word_timestamps.append({"text": word[2], "start": word[0], "end": word[1]})
# Initialize NeMo MSDD diarization model
msdd_model = NeuralDiarizer(cfg=create_config(temp_path)).to(args.device)
msdd_model.diarize()
del msdd_model
torch.cuda.empty_cache()
# Reading timestamps <> Speaker Labels mapping
speaker_ts = []
with open(os.path.join(temp_path, "pred_rttms", "mono_file.rttm"), "r") as f:
lines = f.readlines()
for line in lines:
line_list = line.split(" ")
s = int(float(line_list[5]) * 1000)
e = s + int(float(line_list[8]) * 1000)
speaker_ts.append([s, e, int(line_list[11].split("_")[-1])])
wsm = get_words_speaker_mapping(word_timestamps, speaker_ts, "start")
if info.language in punct_model_langs:
# restoring punctuation in the transcript to help realign the sentences
punct_model = PunctuationModel(model="kredor/punctuate-all")
words_list = list(map(lambda x: x["word"], wsm))
labled_words = punct_model.predict(words_list)
ending_puncts = ".?!"
model_puncts = ".,;:!?"
# We don't want to punctuate U.S.A. with a period. Right?
is_acronym = lambda x: re.fullmatch(r"\b(?:[a-zA-Z]\.){2,}", x)
for word_dict, labeled_tuple in zip(wsm, labled_words):
word = word_dict["word"]
if (
word
and labeled_tuple[1] in ending_puncts
and (word[-1] not in model_puncts or is_acronym(word))
):
word += labeled_tuple[1]
if word.endswith(".."):
word = word.rstrip(".")
word_dict["word"] = word
wsm = get_realigned_ws_mapping_with_punctuation(wsm)
else:
logging.warning(
f'Punctuation restoration is not available for {info.language} language.'
)
ssm = get_sentences_speaker_mapping(wsm, speaker_ts)
with open(f"{args.audio[:-4]}.txt", "w", encoding="utf-8-sig") as f:
get_speaker_aware_transcript(ssm, f)
with open(f"{args.audio[:-4]}.srt", "w", encoding="utf-8-sig") as srt:
write_srt(ssm, srt)
# Write results of Whisper to file to be able to access per-phrase timestamps
with open(f"{args.audio[:-4]}.json", "w", encoding="utf-8-sig") as json_file:
json.dump(whisper_results, json_file, indent=4)
cleanup(temp_path)