asr_funs.py 7.57 KB
Newer Older
Mattia Bergagio's avatar
Mattia Bergagio committed
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import os
from pathlib import Path
from typing import Dict, List

import torch
import whisper_timestamped
from whisper_timestamped.make_subtitles import write_srt
import pysrt
from typeguard import typechecked

import trs_class

try:
    from common_utils.times import timeit
except ModuleNotFoundError:
    from common_module.common_utils.times import timeit

try:
    from common_utils.logger import create_logger
except ModuleNotFoundError:
    from common_module.common_utils.logger import create_logger

try:
    from common_utils.saves import save
except ModuleNotFoundError:
    from common_module.common_utils.saves import save

try:
    from common_utils.gpus_torch import pick_best_gpu
except ModuleNotFoundError:
    from common_module.common_utils.gpus_torch import pick_best_gpu

LOGGER = create_logger(__name__)


@typechecked
def whisper_trs(
    aud_path: str,
    diar_segments: List[Dict],
    device: str,
    compute_type: str,
    out_path: str,
    model_dir: str,
    dev_idx: int = 0,
    max_len: int = 200,
) -> Dict[str, list]:
    """
    diar_segments: segments from mmc_aus.
    max_len: max length of a subtitle, in chars.

    Transcribes each segment from mmc_aus.
    """
    my_trs = trs_class.WhisperTranscriber(
        os.path.join(model_dir, "whisper-large-v3"),
        device,
        # dev_idx,
        # compute_type,  # , model_dir
    )

    transcribe_options = {"task": "transcribe"}

    chunks = []
    end_srt_fils = []

    # read WAVs from mmc_aus
    mmc_aus_out_path = os.path.join(Path(out_path).parent, "mmc_aus")

    # loop over segments from mmc_aus
    for cnt_diar_segm, diar_segm in enumerate(diar_segments):
        LOGGER.debug(f"{cnt_diar_segm=}")
        LOGGER.debug(f'{diar_segm["start"]=}')
        LOGGER.debug(f'{diar_segm["end"]=}')

        if diar_segm["end"] - diar_segm["start"] < 1:
            # suppress short utterances (pyannote artifact)
            chunks.append(
                {
                    "rel_start": transcr_d["segments"][0]["start"],
                    "rel_end": transcr_d["segments"][-1]["end"],
                    "text": None,
                    "language": None,
                    "speaker": None,
                }
            )
            continue

        diar_segm_path = os.path.join(mmc_aus_out_path, f"split.{cnt_diar_segm}.wav")
        diar_audio = whisper_timestamped.load_audio(diar_segm_path)

        # get transcription
        transcr_d = my_trs(
            diar_audio,
            transcribe_options,
        )

        chunks.append(
            {
                "rel_start": transcr_d["segments"][0]["start"],
                "rel_end": transcr_d["segments"][-1]["end"],
                "text": transcr_d["text"],
                "language": transcr_d["language"],
                "speaker": diar_segm["label"],
            }
        )

        LOGGER.debug(f"{chunks[-1]=}")
        LOGGER.debug("writing subs...")
        # https://github.com/linto-ai/whisper-timestamped/issues/42#issuecomment-1443866219
        tmp_srt_fil = os.path.join(out_path, f"tmp_subs.{cnt_diar_segm}.srt")
        end_srt_fils += [os.path.join(out_path, f"end_subs.{cnt_diar_segm}.srt")]
        write_srt(
            transcr_d["segments"],
            open(
                tmp_srt_fil,
                "w",
                encoding="utf-8",
            ),
        )

        LOGGER.debug("upd'ing subs...")
        subs = pysrt.open(tmp_srt_fil, encoding="utf-8")

        LOGGER.debug("shifting subs...")
        subs.shift(seconds=diar_segm["start"])

        LOGGER.debug("saving subs...")
        subs.save(end_srt_fils[-1], encoding="utf-8")

    # merge subs
    # https://unix.stackexchange.com/a/728438
    offset = 0

    with open(
        os.path.join(out_path, "end_subs.srt"), "w", encoding="utf-8"
    ) as end_subs_w:
        for subi in end_srt_fils:
            LOGGER.debug(f"reading {subi}...")
            with open(subi, "r", encoding="utf-8") as subir:
                for lin in subir:
                    # check if line contains a single integer
                    if lin.strip().isdigit():
                        # convert lin to int
                        num = int(lin)
                        num += offset

                        # write num to output file
                        end_subs_w.write(f"{num}\n")
                    else:
                        # write line
                        end_subs_w.write(lin)

            # upd offset
            offset = num
            LOGGER.debug(f"{offset=}")

    return {"transcription": chunks}


@timeit
@typechecked
def trs(
    aud_path: str,
    segments: List[Dict],
    out_path: str,
    model_dir: str,
) -> Dict[str, List]:
    """
    aud_path: path of audio file.
    segments: mmc_aus output.
    out_path: path subs are saved to.
    model_dir: dir the model is saved to.

    Transcribes audio.
    Returns annotation.
    """
    try:
        device = "cuda" if torch.cuda.is_available() else "cpu"
        LOGGER.debug("go to whisper_trs")
        compute_type = "float16"

        # pick best GPU
        dev_idx = pick_best_gpu()
        LOGGER.debug(f"using best GPU = {dev_idx}")

        return whisper_trs(
            aud_path, segments, device, compute_type, out_path, model_dir, dev_idx
        )

    except RuntimeError:
        try:
            # TODO
            # try gpu 0?
            device = "cpu"
            compute_type = "int8"

            LOGGER.debug("using CPU")

            return whisper_trs(
                aud_path, segments, device, compute_type, out_path, model_dir
            )

        except Exception as err:
            LOGGER.error(f"{err=}")
            return {"transcription": []}


def trs_save(
    aud_path: str, segments: List[Dict], json_path: str, out_path: str, model_dir: str
):
    """
    Transcribes audio.
    Saves annotation to JSON.
    """
    annotation = trs(aud_path, segments, out_path, model_dir)
    save(annotation, json_path)
    return


@typechecked
def dl_trs_save(
    message_body: dict,
    out_json: str,
    out_path: str,
    base_dir: str,
    model_dir: str,
) -> bool:
    """
    message_body: msg body. Has "FS" & "voices" as keys.
    out_json: JSON the output is saved to.
    out_path: unused input.
    base_dir: base dir.
    model_dir: dir the model is saved to.

    Downloads mmc_aus JSON.
    Transcribes audio.
    Returns 0 if success.
    """
    # copies landmark
    ret_code = -1

    aud_path = os.path.join(
        base_dir,
        message_body["programme"]["uid"],
        f'{message_body["programme"]["external_id"]}.wav',
    )
    diar_out = message_body["programme"]["mmc_aus"]["segments"]

    # check diar_out
    # True if diar_out is a list
    # True if all dicts have keys ["start", "end", "label"]
    if isinstance(diar_out, list) and all(
        [
            # True if all keys in a dict are ["start", "end", "label"]
            all(k in voice for k in ["start", "end", "label"])
            for voice in diar_out
        ]
    ):

        # transcribe aud
        trs_save(aud_path, diar_out, out_json, out_path, model_dir)

        # success
        ret_code = 0

    # copies landmark
    LOGGER.debug(f"return code: {ret_code}")

    if ret_code != 0:
        return False
    else:
        # success if ret_code = 0
        return True


"""
def find_hallucinations(annotation):
    hall = [" Amara.org", "www.mesmerism.info", " QTSS", " ... "]

    for i, tr in enumerate(annotation["segments"]):
        for w in hall:
            if w in tr["text"]:
                LOGGER.debug(f"Hallucination removed: {tr['text']}")
                annotation["segments"].pop(i)
                break
    return annotation
"""