From 4293180568574b978f96e2b50551b9fd7f7bbf05 Mon Sep 17 00:00:00 2001 From: Kral <118106297+dashitongzhi@users.noreply.github.com> Date: Mon, 29 Jun 2026 21:57:50 +0800 Subject: [PATCH 1/3] fix: handle `torchcodec` unavailability with clear RuntimeError messages (#2016) --- src/pyannote/audio/core/io.py | 29 +++++++++++++++++++++++++++++ src/pyannote/audio/utils/signal.py | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/pyannote/audio/core/io.py b/src/pyannote/audio/core/io.py index 38f3de75..935580fd 100644 --- a/src/pyannote/audio/core/io.py +++ b/src/pyannote/audio/core/io.py @@ -43,6 +43,7 @@ try: import torchcodec from torchcodec import AudioSamples from torchcodec.decoders import AudioDecoder, AudioStreamMetadata + TORCHCODEC_AVAILABLE = True except Exception as e: warnings.warn( "\ntorchcodec is not installed correctly so built-in audio decoding will fail. Solutions are:\n" @@ -50,6 +51,9 @@ except Exception as e: "* fix torchcodec installation. Error message was:\n\n" f"{e}" ) + TORCHCODEC_AVAILABLE = False + AudioDecoder = None + AudioStreamMetadata = None AudioFile = str | Path | IOBase | Mapping @@ -81,8 +85,19 @@ def get_audio_metadata(file: AudioFile) -> "AudioStreamMetadata": ------- metadata : AudioStreamMetadata Audio file metadata + + Raises + ------ + RuntimeError if torchcodec is not available. """ + if not TORCHCODEC_AVAILABLE: + raise RuntimeError( + "torchcodec is not available. Cannot read audio metadata. " + "Please install torchcodec or provide audio as a waveform dictionary: " + "{'waveform': (channel, time) torch.Tensor, 'sample_rate': int}" + ) + metadata = AudioDecoder(file["audio"]).metadata # rewind if needed @@ -316,6 +331,13 @@ class Audio: return self.downmix_and_resample(waveform, sample_rate, channel=channel) + if not TORCHCODEC_AVAILABLE: + raise RuntimeError( + "torchcodec is not available. Cannot read audio file. " + "Please install torchcodec or provide audio as a waveform dictionary: " + "{'waveform': (channel, time) torch.Tensor, 'sample_rate': int}" + ) + decoder = AudioDecoder(file["audio"]) samples: AudioSamples = decoder.get_all_samples() @@ -391,6 +413,13 @@ class Audio: data = F.pad(data, (pad_start, pad_end)) return self.downmix_and_resample(data, sample_rate, channel=channel) + if not TORCHCODEC_AVAILABLE: + raise RuntimeError( + "torchcodec is not available. Cannot crop audio file. " + "Please install torchcodec or provide audio as a waveform dictionary: " + "{'waveform': (channel, time) torch.Tensor, 'sample_rate': int}" + ) + decoder = AudioDecoder(file["audio"]) metadata: AudioStreamMetadata = decoder.metadata diff --git a/src/pyannote/audio/utils/signal.py b/src/pyannote/audio/utils/signal.py index 1b372ce2..2808f0af 100644 --- a/src/pyannote/audio/utils/signal.py +++ b/src/pyannote/audio/utils/signal.py @@ -334,7 +334,7 @@ class Peak: alpha: float = 0.5, min_duration: float = 1.0, ): - super(Peak, self).__init__() + super().__init__() self.alpha = alpha self.min_duration = min_duration From a4280d7188e9f8670e2d9571e6ac3c6b662c5b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20BREDIN?= Date: Tue, 30 Jun 2026 13:27:57 +0200 Subject: [PATCH 2/3] fix: fix #2032 --- CHANGELOG.md | 5 +-- src/pyannote/audio/core/pipeline.py | 55 ++++++++++++++--------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d66e010..f579d7dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ # CHANGELOG -## next +## Version 4.0.6 (2026-06-29) -- improve(pipeline): add support for `subfolder` in `Pipeline.from_pretrained` +- fix(pipeline): add missing support for `subfolder` in `Pipeline.from_pretrained` +- feat(pipeline): add basic support for (sequential) batch inference ## Version 4.0.5 (2026-06-22) diff --git a/src/pyannote/audio/core/pipeline.py b/src/pyannote/audio/core/pipeline.py index 4ce5885b..fcada57a 100644 --- a/src/pyannote/audio/core/pipeline.py +++ b/src/pyannote/audio/core/pipeline.py @@ -486,12 +486,33 @@ class Pipeline(_Pipeline): return file + def _apply_batch( + self, + files: list, + **kwargs, + ) -> Iterator[tuple[Any, Any]]: + """Process a list of prepared files, yielding (file, prediction) pairs""" + + # if pipeline natively supports batch processing, use it + if hasattr(self, "apply_batch"): + # TODO: warn user that pipeline_kwargs might not be taken into account. + for f, prediction in self.apply_batch(files, **kwargs): + track_pipeline_apply(self, f, **kwargs) + yield f, prediction + + # otherwise process files sequentially + else: + for f in files: + prediction = self.apply(f, **f.get("pipeline_kwargs", {}), **kwargs) + track_pipeline_apply(self, f, **kwargs) + yield f, prediction + def __call__( self, file: AudioFile | list[AudioFile], preload: bool = False, **kwargs, - ) -> Any | Iterator[tuple[str, Any]]: + ) -> Any | Iterator[tuple[Any, Any]]: """Validate file(s), (optionally) load it/them in memory, then process it/them Parameters @@ -511,13 +532,12 @@ class Pipeline(_Pipeline): Returns (for single file) ------------------------- output : Any - Whatever `self.apply(...)` returns for a single file, or a dict - mapping each URI to its output for batch input. + Whatever `self.apply(...)` returns for a single file. - Yields (for multiple files) + Returns (for list of files) --------------------------- - file : AudioFile - output : Any + predictions : Iterator[tuple[AudioFile, Any]] + Iterator of (file, prediction) pairs. """ fix_reproducibility(getattr(self, "device", torch.device("cpu"))) @@ -557,28 +577,7 @@ class Pipeline(_Pipeline): f"Duplicate URIs: {duplicates}" ) - # if pipeline natively supports batch processing, use it - if hasattr(self, "apply_batch"): - # TODO: warn user that pipeline_kwargs might not be taken into account. - for f, prediction in self.apply_batch(files, **kwargs): - # send file duration to telemetry as well as - # requested number of speakers in case of diarization - track_pipeline_apply(self, f, **kwargs) - - yield f, prediction - - # otherwise process files sequentially - else: - for f in files: - prediction = self.apply(f, **f.get("pipeline_kwargs", {}), **kwargs) - - # send file duration to telemetry as well as - # requested number of speakers in case of diarization - track_pipeline_apply(self, f, **kwargs) - - yield f, prediction - - return + return self._apply_batch(files, **kwargs) file = self.prepare_one(file, preload=preload) prediction = self.apply(file, **file.get("pipeline_kwargs", {}), **kwargs) From 9a18f3a8002f86e49397666181215694bbfed108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20BREDIN?= Date: Tue, 30 Jun 2026 16:27:29 +0200 Subject: [PATCH 3/3] doc: update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f579d7dd..faba74db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # CHANGELOG -## Version 4.0.6 (2026-06-29) +## Version 4.0.7 (2026-06-30) - fix(pipeline): add missing support for `subfolder` in `Pipeline.from_pretrained` - feat(pipeline): add basic support for (sequential) batch inference