File size: 657 Bytes
dd5bcef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from pathlib import Path
from typing import Tuple

import torch
from pyannote.audio import Pipeline
from pydub import AudioSegment


def get_pipeline(filename: str | Path, hf_api_key: str, pyannote_model: str, tmp_dir: Path) -> Tuple[AudioSegment, Pipeline]:
    pipeline = Pipeline.from_pretrained(
        pyannote_model,
        token=hf_api_key,
    )
    pipeline.to(torch.device("cuda"))

    audio_segment = AudioSegment.from_mp3(filename)
    wav_audio = tmp_dir.joinpath(Path(filename).name).with_suffix(".wav")

    with open(wav_audio, "wb"):
        audio_segment.export(wav_audio, format="wav")

    return (audio_segment, pipeline(wav_audio))