Skip to content

Commit 1643898

Browse files
eustlbzaristei
authored andcommitted
[ASR pipline] fix with datasets 4.0 (huggingface#39504)
* fix * handle edge case * make
1 parent e1ed988 commit 1643898

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/transformers/pipelines/automatic_speech_recognition.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,11 @@ def preprocess(self, inputs, chunk_length_s=0, stride_length_s=None):
380380

381381
if isinstance(inputs, torchcodec.decoders.AudioDecoder):
382382
_audio_samples = inputs.get_all_samples()
383+
384+
# torchcodec always returns (num_channels, num_samples)
385+
# while before (datasets < 4.0) we had (2, num_samples) if stereo, (num_samples,) if mono
383386
_array = _audio_samples.data
387+
_array = _array[0] if _array.ndim == 2 and _array.shape[0] == 1 else _array
384388
inputs = {"array": _array, "sampling_rate": _audio_samples.sample_rate}
385389

386390
if isinstance(inputs, dict):
@@ -429,10 +433,13 @@ def preprocess(self, inputs, chunk_length_s=0, stride_length_s=None):
429433
# can add extra data in the inputs, so we need to keep track
430434
# of the original length in the stride so we can cut properly.
431435
stride = (inputs.shape[0], int(round(stride[0] * ratio)), int(round(stride[1] * ratio)))
432-
if not isinstance(inputs, np.ndarray):
436+
if not isinstance(inputs, (np.ndarray, torch.Tensor)):
433437
raise TypeError(f"We expect a numpy ndarray or torch tensor as input, got `{type(inputs)}`")
434-
if len(inputs.shape) != 1:
435-
raise ValueError("We expect a single channel audio input for AutomaticSpeechRecognitionPipeline")
438+
if inputs.ndim != 1:
439+
logger.warning(
440+
f"We expect a single channel audio input for AutomaticSpeechRecognitionPipeline, got {inputs.ndim}. Taking the mean of the channels for mono conversion."
441+
)
442+
inputs = inputs.mean(axis=0)
436443

437444
if chunk_length_s:
438445
if stride_length_s is None:

0 commit comments

Comments
 (0)