From 04bac3fcdf733e2a37d861525aec91f397ed6b3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20BREDIN?= Date: Tue, 9 Sep 2025 13:59:07 +0200 Subject: [PATCH] feat: add Pipeline-like wrapper around pyannoteAI products (#1913) Co-authored-by: Juan Coria --- pyproject.toml | 2 +- .../{premium => pyannoteai}/__init__.py | 3 - .../audio/pipelines/pyannoteai/local.py | 126 ++++++++++++++++++ .../sdk.py} | 54 ++++---- .../audio/pipelines/speaker_diarization.py | 6 +- 5 files changed, 158 insertions(+), 33 deletions(-) rename src/pyannote/audio/pipelines/{premium => pyannoteai}/__init__.py (91%) create mode 100644 src/pyannote/audio/pipelines/pyannoteai/local.py rename src/pyannote/audio/pipelines/{premium/speaker_diarization.py => pyannoteai/sdk.py} (79%) diff --git a/pyproject.toml b/pyproject.toml index 7e02fb29..a00d401f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,7 @@ dependencies = [ "torchmetrics>=1.6.1", "soundfile>=0.13.1", "matplotlib>=3.10.0", - "pyannoteai.sdk>=0.1.0", + "pyannoteai-sdk>=0.2.0", ] [project.scripts] diff --git a/src/pyannote/audio/pipelines/premium/__init__.py b/src/pyannote/audio/pipelines/pyannoteai/__init__.py similarity index 91% rename from src/pyannote/audio/pipelines/premium/__init__.py rename to src/pyannote/audio/pipelines/pyannoteai/__init__.py index c505efad..d598a73a 100644 --- a/src/pyannote/audio/pipelines/premium/__init__.py +++ b/src/pyannote/audio/pipelines/pyannoteai/__init__.py @@ -20,6 +20,3 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -from .speaker_diarization import PremiumSpeakerDiarization - -__all__ = ["PremiumSpeakerDiarization"] diff --git a/src/pyannote/audio/pipelines/pyannoteai/local.py b/src/pyannote/audio/pipelines/pyannoteai/local.py new file mode 100644 index 00000000..08c73676 --- /dev/null +++ b/src/pyannote/audio/pipelines/pyannoteai/local.py @@ -0,0 +1,126 @@ +# The MIT License (MIT) +# +# Copyright (c) 2025- pyannoteAI +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: + +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. + +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import os +from pathlib import Path + +from pyannote.audio import Pipeline +from pyannote.core import Annotation, Segment + + +class Local(Pipeline): + """Wrapper around official pyannoteAI on-premise package + + Parameters + ---------- + token : str, optional + pyannoteAI API key created from https://dashboard.pyannote.ai. + Defaults to using `PYANNOTEAI_API_TOKEN` environment variable. + + Usage + ----- + >>> from pyannote.audio.pipelines.pyannoteai.local import Local + >>> pipeline = Local(token="{PYANNOTEAI_API_KEY}") + >>> speaker_diarization = pipeline("/path/to/your/audio.wav") + """ + + def __init__(self, token: str | None = None, **kwargs): + super().__init__() + + from pyannoteai.local import Pipeline as _LocalPipeline + + self.token = token or os.environ.get("PYANNOTEAI_API_KEY", None) + self._pipeline = _LocalPipeline(self.token) + + def _to_annotation(self, completed_job: dict) -> Annotation: + """Deserialize job output into pyannote.core Annotation""" + + output = completed_job["output"]["diarization"] + job_id = completed_job["jobId"] + + annotation = Annotation(uri=job_id) + for t, turn in enumerate(output): + segment = Segment(start=turn["start"], end=turn["end"]) + speaker = turn["speaker"] + annotation[segment, t] = speaker + + return annotation.rename_tracks("string") + + def apply( + self, + file: Path, + num_speakers: int | None = None, + min_speakers: int | None = None, + max_speakers: int | None = None, + exclusive: bool = False, + ) -> Annotation: + """Speaker diarization using pyannoteAI on-premise package + + Parameters + ---------- + file : AudioFile + Processed file. + num_speakers : int, optional + Force number of speakers to diarize. If not provided, the + number of speakers will be determined automatically. + min_speakers : int, optional + Not supported yet. Minimum number of speakers. Has no effect when `num_speakers` is provided. + max_speakers : int, optional + Not supported yet. Maximum number of speakers. Has no effect when `num_speakers` is provided. + exclusive : bool, optional + Enable exclusive diarization. + + Returns + ------- + speaker_diarization : Annotation + Speaker diarization result (when successful) + + Raises + ------ + PyannoteAIFailedJob + If the job failed + PyannoteAICanceledJob + If the job was canceled + HTTPError + If something else went wrong + """ + + predictions = self._pipeline.diarize( + file["audio"], + num_speakers=num_speakers, + min_speakers=min_speakers, + max_speakers=max_speakers, + ) + + # use exclusive diarization whenever requested + if exclusive: + diarization = predictions["exclusive_diarization"] + else: + diarization = predictions["diarization"] + + # deserialize the output into a good-old Annotation instance + annotation = Annotation() + for t, turn in enumerate(diarization): + segment = Segment(start=turn["start"], end=turn["end"]) + speaker = turn["speaker"] + annotation[segment, t] = speaker + return annotation.rename_tracks("string") diff --git a/src/pyannote/audio/pipelines/premium/speaker_diarization.py b/src/pyannote/audio/pipelines/pyannoteai/sdk.py similarity index 79% rename from src/pyannote/audio/pipelines/premium/speaker_diarization.py rename to src/pyannote/audio/pipelines/pyannoteai/sdk.py index feb4c5cc..c953e613 100644 --- a/src/pyannote/audio/pipelines/premium/speaker_diarization.py +++ b/src/pyannote/audio/pipelines/pyannoteai/sdk.py @@ -20,9 +20,8 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. - +import os from pathlib import Path -from typing import Optional from pyannote.audio import Pipeline from pyannote.core import Annotation, Segment @@ -30,7 +29,7 @@ from pyannote.core import Annotation, Segment from pyannoteai.sdk import Client -class PremiumSpeakerDiarization(Pipeline): +class SDK(Pipeline): """Wrapper around official pyannoteAI API client Parameters @@ -41,36 +40,25 @@ class PremiumSpeakerDiarization(Pipeline): Usage ----- - >>> from pyannote.audio.pipelines.premium import PremiumSpeakerDiarization - >>> pipeline = PremiumSpeakerDiarization(token="{PYANNOTEAI_API_KEY}") + >>> from pyannote.audio.pipelines.pyannoteai.sdk import SDK + >>> pipeline = SDK(token="{PYANNOTEAI_API_KEY}") >>> speaker_diarization = pipeline("/path/to/your/audio.wav") """ - def __init__(self, token: Optional[str] = None, **kwargs): + def __init__(self, token: str | None = None, **kwargs): super().__init__() - self._client = Client(token) - def _to_annotation(self, completed_job: dict) -> Annotation: - """Deserialize job output into pyannote.core Annotation""" + self.token = token or os.environ.get("PYANNOTEAI_API_KEY", None) + self._client = Client(self.token) - output = completed_job["output"]["diarization"] - job_id = completed_job["jobId"] - - annotation = Annotation(uri=job_id) - for t, turn in enumerate(output): - segment = Segment(start=turn["start"], end=turn["end"]) - speaker = turn["speaker"] - annotation[segment, t] = speaker - - return annotation.rename_tracks("string") - - def __call__( + def apply( self, file: Path, - num_speakers: Optional[int] = None, - min_speakers: Optional[int] = None, - max_speakers: Optional[int] = None, + num_speakers: int | None = None, + min_speakers: int | None = None, + max_speakers: int | None = None, model: str = "precision-2", + exclusive: bool = False, ) -> Annotation: """Speaker diarization using pyannoteAI web API @@ -91,7 +79,9 @@ class PremiumSpeakerDiarization(Pipeline): Not supported yet. Maximum number of speakers. Has no effect when `num_speakers` is provided. model : str, optional pyannoteAI diarization model to use. Defaults to "precision-2". - + exclusive : bool, optional + Enable exclusive diarization. + Returns ------- speaker_diarization : Annotation @@ -118,10 +108,22 @@ class PremiumSpeakerDiarization(Pipeline): max_speakers=max_speakers, confidence=False, model=model, + exclusive=exclusive, ) # retrieve job output (once completed) job_output = self._client.retrieve(job_id) + # use exclusive diarization whenever requested + if exclusive: + diarization: list[dict] = job_output["output"]["exclusiveDiarization"] + else: + diarization: list[dict] = job_output["output"]["diarization"] + # deserialize the output into a good-old Annotation instance - return self._to_annotation(job_output) + annotation = Annotation() + for t, turn in enumerate(diarization): + segment = Segment(start=turn["start"], end=turn["end"]) + speaker = turn["speaker"] + annotation[segment, t] = speaker + return annotation.rename_tracks("string") diff --git a/src/pyannote/audio/pipelines/speaker_diarization.py b/src/pyannote/audio/pipelines/speaker_diarization.py index 13cd9e98..a3930f18 100644 --- a/src/pyannote/audio/pipelines/speaker_diarization.py +++ b/src/pyannote/audio/pipelines/speaker_diarization.py @@ -450,7 +450,7 @@ class SpeakerDiarization(SpeakerDiarizationMixin, Pipeline): num_speakers: Optional[int] = None, min_speakers: Optional[int] = None, max_speakers: Optional[int] = None, - exclusive_diarization: bool = False, + exclusive: bool = False, return_embeddings: bool = False, hook: Optional[Callable] = None, ) -> Annotation: @@ -466,7 +466,7 @@ class SpeakerDiarization(SpeakerDiarizationMixin, Pipeline): Minimum number of speakers. Has no effect when `num_speakers` is provided. max_speakers : int, optional Maximum number of speakers. Has no effect when `num_speakers` is provided. - exclusive_diarization : bool, optional + exclusive : bool, optional Enforce exclusive diarization, i.e. only one speaker can be active at a time. Defaults to False, i.e. overlapping speech is allowed. return_embeddings : bool, optional @@ -537,7 +537,7 @@ class SpeakerDiarization(SpeakerDiarizationMixin, Pipeline): # shape: (num_frames, 1) # dtype: int - if exclusive_diarization: + if exclusive: count.data = np.minimum(count.data, 1).astype(np.int8) # exit early when no speaker is ever active