Spaces:
Running
Running
stream : add nullptr check of whisper_context (#3283)
Browse files* stream : add nullptr check of whisper_context
This commit adds a check to ensure that the `whisper_context` is not
null after initialization.
The motivation for this is that currently, if the initialization fails,
the program continues to run leading to a segmentation fault. This sort
of check is performed by others examples like whisper-cli.
Refs: https://github.com/ggml-org/whisper.cpp/issues/3280#issuecomment-3003778035
* examples : add nullptr check for whisper_context
examples/bench/bench.cpp
CHANGED
|
@@ -67,6 +67,10 @@ static int whisper_bench_full(const whisper_params & params) {
|
|
| 67 |
cparams.flash_attn = params.flash_attn;
|
| 68 |
|
| 69 |
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
{
|
| 72 |
fprintf(stderr, "\n");
|
|
|
|
| 67 |
cparams.flash_attn = params.flash_attn;
|
| 68 |
|
| 69 |
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
|
| 70 |
+
if (ctx == nullptr) {
|
| 71 |
+
fprintf(stderr, "error: failed to initialize whisper context\n");
|
| 72 |
+
return 2;
|
| 73 |
+
}
|
| 74 |
|
| 75 |
{
|
| 76 |
fprintf(stderr, "\n");
|
examples/command/command.cpp
CHANGED
|
@@ -709,6 +709,10 @@ int main(int argc, char ** argv) {
|
|
| 709 |
cparams.flash_attn = params.flash_attn;
|
| 710 |
|
| 711 |
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 712 |
|
| 713 |
// print some info about the processing
|
| 714 |
{
|
|
|
|
| 709 |
cparams.flash_attn = params.flash_attn;
|
| 710 |
|
| 711 |
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
|
| 712 |
+
if (ctx == nullptr) {
|
| 713 |
+
fprintf(stderr, "error: failed to initialize whisper context\n");
|
| 714 |
+
return 2;
|
| 715 |
+
}
|
| 716 |
|
| 717 |
// print some info about the processing
|
| 718 |
{
|
examples/stream/stream.cpp
CHANGED
|
@@ -163,6 +163,10 @@ int main(int argc, char ** argv) {
|
|
| 163 |
cparams.flash_attn = params.flash_attn;
|
| 164 |
|
| 165 |
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
std::vector<float> pcmf32 (n_samples_30s, 0.0f);
|
| 168 |
std::vector<float> pcmf32_old;
|
|
|
|
| 163 |
cparams.flash_attn = params.flash_attn;
|
| 164 |
|
| 165 |
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
|
| 166 |
+
if (ctx == nullptr) {
|
| 167 |
+
fprintf(stderr, "error: failed to initialize whisper context\n");
|
| 168 |
+
return 2;
|
| 169 |
+
}
|
| 170 |
|
| 171 |
std::vector<float> pcmf32 (n_samples_30s, 0.0f);
|
| 172 |
std::vector<float> pcmf32_old;
|
examples/vad-speech-segments/speech.cpp
CHANGED
|
@@ -111,6 +111,10 @@ int main(int argc, char ** argv) {
|
|
| 111 |
struct whisper_vad_context * vctx = whisper_vad_init_from_file_with_params(
|
| 112 |
cli_params.vad_model.c_str(),
|
| 113 |
ctx_params);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
// Detect speech in the input audio file.
|
| 116 |
if (!whisper_vad_detect_speech(vctx, pcmf32.data(), pcmf32.size())) {
|
|
|
|
| 111 |
struct whisper_vad_context * vctx = whisper_vad_init_from_file_with_params(
|
| 112 |
cli_params.vad_model.c_str(),
|
| 113 |
ctx_params);
|
| 114 |
+
if (vctx == nullptr) {
|
| 115 |
+
fprintf(stderr, "error: failed to initialize whisper context\n");
|
| 116 |
+
return 2;
|
| 117 |
+
}
|
| 118 |
|
| 119 |
// Detect speech in the input audio file.
|
| 120 |
if (!whisper_vad_detect_speech(vctx, pcmf32.data(), pcmf32.size())) {
|