Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crash when initializing with an invalid model #26

Open
zaphoyd opened this issue Aug 29, 2023 · 4 comments
Open

crash when initializing with an invalid model #26

zaphoyd opened this issue Aug 29, 2023 · 4 comments

Comments

@zaphoyd
Copy link

zaphoyd commented Aug 29, 2023

When Whisper.init(fromFileURL) is called with a file URL that is a file that exists, but not a valid model file, the error condition from the underlying whisper.cpp library is not handled.

Specifically:
self.whisperContext = fileURL.relativePath.withCString { whisper_init_from_file($0) }

whisper_init_from_file will return nullptr in this case. The attempted assignment produces the following error which crashes the program using the library.

whisper_init_from_file_no_state: loading model from '.'
whisper_model_load: loading model
whisper_model_load: invalid model data (bad magic)
whisper_init_no_state: failed to load model
SwiftWhisper/Whisper.swift:16: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

I'd like to add a check to this initializer so my program can catch and safely handle this case. In theory, my program could attempt to figure out if this was a valid model file, but this would involve re-implementing the detection code from whisper.cpp that I am trying to wrap. Letting that code that is already doing the error handling just pass the error through seems like a better arrangement.

Doing so would probably require changing the init signature to a throwing one or a fail-able one. I understand that this would involve a change in API here. Is there a way to handle this that would be likely to be accepted as a PR? Is there a more general plan for how to handle this sort of error case?

@exPHAT
Copy link
Owner

exPHAT commented Sep 5, 2023

As far as I'm aware, theres no way to catch fatally-throwing errors in Swift that originated from C. So I don't think a throwing initializer would be possible in this case.

If you know of a way that I'm not aware of, please let me know.

@exPHAT exPHAT closed this as not planned Won't fix, can't repro, duplicate, stale Sep 5, 2023
@zaphoyd
Copy link
Author

zaphoyd commented Sep 5, 2023

Hi, the error is passed using the return value, not an exception. Here is the fix that I am using in a fork: master...uni-industries:SwiftWhisper:master

This allows checking the return value of the Whisper initializer. You don't get the precise reason in an exception (it IS printed to the console though by the underlying library). But you can check for this nil value and handle it rather than having the program crash with a null reference exception, which is the key thing I am trying to avoid.

Would be happy to submit a PR if this direction is useful.

@exPHAT exPHAT reopened this Sep 5, 2023
@exPHAT
Copy link
Owner

exPHAT commented Sep 5, 2023

Your branch looks good to me, though the tests and README will need to be updated to reflect it.

If you'd like to do that as well, I'd be happy to merge it in a PR.

@roei46
Copy link

roei46 commented Dec 30, 2023

I am running your fork yet Whisper is still nil: let whisper = Whisper(fromFileURL: recordedFile!)
What am i missing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants