Skip to content

Commit 110c068

Browse files
authored
Catch exception from whisper (k2-fsa#408)
1 parent fee3a5b commit 110c068

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

sherpa-onnx/csrc/offline-recognizer-whisper-impl.h

+15-7
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ class OfflineRecognizerWhisperImpl : public OfflineRecognizerImpl {
107107
int32_t num_frames = f.size() / feat_dim;
108108

109109
if (num_frames > max_num_frames) {
110-
SHERPA_ONNX_LOGE("Only waves less than 30 seconds are supported.");
111-
exit(-1);
110+
SHERPA_ONNX_LOGE(
111+
"Only waves less than 30 seconds are supported. We process only the "
112+
"first 30 seconds and discard the remaining data");
113+
num_frames = max_num_frames;
112114
}
113115

114116
NormalizeFeatures(f.data(), num_frames, feat_dim);
@@ -124,13 +126,19 @@ class OfflineRecognizerWhisperImpl : public OfflineRecognizerImpl {
124126
(max_num_frames - num_frames) * feat_dim * sizeof(float));
125127
mel = Transpose12(model_->Allocator(), &mel);
126128

127-
auto cross_kv = model_->ForwardEncoder(std::move(mel));
129+
try {
130+
auto cross_kv = model_->ForwardEncoder(std::move(mel));
128131

129-
auto results =
130-
decoder_->Decode(std::move(cross_kv.first), std::move(cross_kv.second));
132+
auto results = decoder_->Decode(std::move(cross_kv.first),
133+
std::move(cross_kv.second));
131134

132-
auto r = Convert(results[0], symbol_table_);
133-
s->SetResult(r);
135+
auto r = Convert(results[0], symbol_table_);
136+
s->SetResult(r);
137+
} catch (const Ort::Exception &ex) {
138+
SHERPA_ONNX_LOGE("\n\nCaught exception:\n\n%s\n\nReturn an empty result",
139+
ex.what());
140+
return;
141+
}
134142
}
135143

136144
private:

0 commit comments

Comments
 (0)