Skip to content

Commit

Permalink
feat: add support for WeSpeaker models on Huggingface (#1466)
Browse files Browse the repository at this point in the history
  • Loading branch information
hbredin authored Sep 22, 2023
1 parent 71f012b commit 9297c0c
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions pyannote/audio/pipelines/speaker_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@

import warnings
from functools import cached_property
from pathlib import Path
from typing import Text, Union

import numpy as np
import torch
import torch.nn.functional as F
import torchaudio
import torchaudio.compliance.kaldi as kaldi
from huggingface_hub import hf_hub_download
from huggingface_hub.utils import RepositoryNotFoundError
from torch.nn.utils.rnn import pad_sequence

from pyannote.audio import Inference, Model, Pipeline
Expand Down Expand Up @@ -395,7 +398,7 @@ class WeSpeakerPretrainedSpeakerEmbedding(BaseInference):
Usage
-----
>>> get_embedding = WeSpeakerPretrainedSpeakerEmbedding("wespeaker.xxxx.onnx")
>>> get_embedding = WeSpeakerPretrainedSpeakerEmbedding("hbredin/wespeaker-voxceleb-resnet34-LM")
>>> assert waveforms.ndim == 3
>>> batch_size, num_channels, num_samples = waveforms.shape
>>> assert num_channels == 1
Expand All @@ -410,7 +413,7 @@ class WeSpeakerPretrainedSpeakerEmbedding(BaseInference):

def __init__(
self,
embedding: Text = "speechbrain/spkrec-ecapa-voxceleb",
embedding: Text = "hbredin/wespeaker-voxceleb-resnet34-LM",
device: torch.device = None,
):
if not ONNX_IS_AVAILABLE:
Expand All @@ -420,6 +423,17 @@ def __init__(

super().__init__()

if not Path(embedding).exists():
try:
embedding = hf_hub_download(
repo_id=embedding,
filename="speaker-embedding.onnx",
)
except RepositoryNotFoundError:
raise ValueError(
f"Could not find '{embedding}' on huggingface.co nor on local disk."
)

self.embedding = embedding

self.to(device or torch.device("cpu"))
Expand Down

0 comments on commit 9297c0c

Please sign in to comment.