Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/transformers/pipelines/automatic_speech_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ def preprocess(self, inputs, chunk_length_s=0, stride_length_s=None):
raise ValueError("We expect a single channel audio input for AutomaticSpeechRecognitionPipeline")

if chunk_length_s:
if self.type not in {"ctc", "ctc_with_lm"}:
raise ValueError(
"`chunk_length_s` is only valid for CTC models, use other chunking options for other models"
)
if stride_length_s is None:
stride_length_s = chunk_length_s / 6

Expand All @@ -264,10 +268,6 @@ def preprocess(self, inputs, chunk_length_s=0, stride_length_s=None):
stride_left = int(round(stride_length_s[0] * self.feature_extractor.sampling_rate / align_to) * align_to)
stride_right = int(round(stride_length_s[1] * self.feature_extractor.sampling_rate / align_to) * align_to)

if self.type not in {"ctc", "ctc_with_lm"}:
raise ValueError(
"`chunk_length_s` is only valid for CTC models, use other chunking options for other models"
)
if chunk_len < stride_left + stride_right:
raise ValueError("Chunk length must be superior to stride length")

Expand Down
19 changes: 18 additions & 1 deletion tests/pipelines/test_pipelines_automatic_speech_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,15 @@ def run_pipeline_test(self, speech_recognizer, examples):
},
)
else:
# Non CTC models cannot use chunk_length
with self.assertRaises(ValueError) as v:
outputs = speech_recognizer(audio, chunk_length_s=10)
self.assertEqual(v.exception, "")

# Non CTC models cannot use return_timestamps
with self.assertRaises(ValueError):
with self.assertRaises(ValueError) as v:
outputs = speech_recognizer(audio, return_timestamps="char")
self.assertEqual(v.exception, "")

@require_torch
@slow
Expand All @@ -138,6 +144,17 @@ def test_small_model_pt(self):
waveform = np.tile(np.arange(1000, dtype=np.float32), 34)
output = speech_recognizer(waveform)
self.assertEqual(output, {"text": "(Applaudissements)"})
with self.assertRaises(ValueError) as v:
_ = speech_recognizer(waveform, chunk_length_s=10)
self.assertEqual(
str(v.exception),
"`chunk_length_s` is only valid for CTC models, use other chunking options for other models",
)

# Non CTC models cannot use return_timestamps
with self.assertRaises(ValueError) as v:
_ = speech_recognizer(waveform, return_timestamps="char")
self.assertEqual(str(v.exception), "We cannot return_timestamps yet on non-ctc models !")
Comment on lines +147 to +157

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIce test 😉


@require_torch
def test_small_model_pt_seq2seq(self):
Expand Down