react-native-vosk icon indicating copy to clipboard operation
react-native-vosk copied to clipboard

Load model from the another path

Open ArkaitzSKR opened this issue 1 year ago • 11 comments

Hello! Is it possible to use the library by importing the model instead of from the assets folder from a different location?

We are trying to use the library with 4.5GB models and by entering it in the assets it is impossible to compile.

In the tests we have carried out we get an error with a file called uuid. This file is generated by Android Studio when compiling the assets and contains, as the name indicates, a uuid. We have tried putting a uuid file in the external storage in the same folder where the model is located but it still gives us the same error.

Is there any limit on the size of the model?

Thanks in advance and greetings!

ArkaitzSKR avatar Mar 12 '24 13:03 ArkaitzSKR

Hi @ArkaitzSKR,

For the UUID generation, take a look at the build.gradle task that handle this, it may help: https://github.com/riderodd/react-native-vosk/blob/3903c0e96d16f251e278441861b4acb250c9f47f/android/build.gradle#L85C1-L98C27

Maybe by changing the path you can handle a different storage location. The library should work with all models, no matter the size. If it's not, it may be related to device capacity ?

Maybe if you could share the error and what you've already tried, we may try to think about a fix together :)

riderodd avatar Mar 27 '24 22:03 riderodd

I'm hoping to download from remote and unzip the files into the DocumentDirectoryPath and then load the model. Is this also possible?

vidman22 avatar Mar 30 '24 00:03 vidman22

I really don't know, the only way is try. Generally and from my only and personaly experience, Android is more permissive with this type of approach than iOS. If you succeed in your tries feel free to let us know, that would be nice to explain how to do it in the docs !

riderodd avatar Apr 12 '24 15:04 riderodd

I really don't know, the only way is try. Generally and from my only and personaly experience, Android is more permissive with this type of approach than iOS. If you succeed in your tries feel free to let us know, that would be nice to explain how to do it in the docs !

I couldn't get it to work on iOS, i'll check on Android soon. Thanks!

vidman22 avatar Apr 12 '24 16:04 vidman22

Hey, @vidman22 I want to do a similar thing of downloading a zip from a CDN, unzipping in react native and then loading the model in vosk. Were you able to make it work with this vosk api? Or come up with a work around?

ShristiC avatar Jul 22 '24 14:07 ShristiC

@ShristiC Hello, I was able to get it working by modifying the 'ios/VoskModel.swift' file so that the model is loaded from the document directory and not the bundle. I'm not a swift developer so i can't attest to the robustness of the modification.

` init(name: String) throws {

    // Set to -1 to disable logs
    vosk_set_log_level(0);
    
    let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let modelURL = documentsDirectory.appendingPathComponent(name)
    var isDirectory: ObjCBool = false
    if FileManager.default.fileExists(atPath: modelURL.path, isDirectory: &isDirectory) && isDirectory.boolValue {
        // File exists, you can read its contents or perform other operations
        do {
            let directoryContents = try FileManager.default.contentsOfDirectory(at: modelURL, includingPropertiesForKeys: nil)
             for fileURL in directoryContents {
                print("File: \(fileURL.lastPathComponent)")
                // Perform operations with each file URL as needed
            }
            // Load model from documents directory
            model = vosk_model_new(modelURL.path)
            let appBundle = Bundle(for: Self.self)
            // Get the URL to the spk model inside this pod
            if let spkModelPath = appBundle.path(forResource: "vosk-model-spk-0.4", ofType: nil) {
                spkModel = vosk_spk_model_new(spkModelPath)
            }
        } catch {
            print("Error reading file: \(error.localizedDescription)")
        }
    } else {
        print("File does not exist.")
    }
    
}

deinit {
    vosk_model_free(model)
    vosk_spk_model_free(spkModel)
}`

vidman22 avatar Jul 22 '24 14:07 vidman22

This is great, can do some further testing to determine robustness (also not a Swift developer). Does it work with android as well?

Additional question. Did you use react-native-fs to download a .zip and react-native-zip-archive to unzip, then store to DocumentDirectoryPath?

ShristiC avatar Jul 22 '24 14:07 ShristiC

Yes, i use react-native-fs and react-native-zip-archive to store the model in the DocumentDirectoryPath and it should work on android as well.

vidman22 avatar Jul 22 '24 15:07 vidman22

Thanks for the help @vidman22 ! I have opened a PR with the changes and tested the flow in Android and iOS.

ShristiC avatar Aug 05 '24 19:08 ShristiC

Hey @vidman22 can you release a new version with that PR?

fkesheh avatar Sep 06 '24 14:09 fkesheh

Hey @vidman22 can you release a new version with that PR?

Looks like it's in the works here https://github.com/riderodd/react-native-vosk/pull/57

vidman22 avatar Sep 06 '24 14:09 vidman22