spokestack / react-native-spokestack

Spokestack: give your React Native app a voice interface!
https://spokestack.io
Apache License 2.0
56 stars 13 forks source link

feat(download): add ability to download model files, as well as pass source objects such as from require('./filter.tflite') #89

Closed timmywil closed 3 years ago

timmywil commented 3 years ago

BREAKING CHANGE: all path options have been switched to options that can accept a remote URL or a JS source object (i.e. from require or import).

Path options include: wakeword.filterPath, wakeword.detectPath,
  wakeword.encodePath, nlu.modelPath, nlu.metadataPath, and nlu.vocabPath.

These have been changed to wakeword.filter, wakeword.detect, etc.

- This commit also adds wakeword to the example.

Reasoning

I started to add wakeword to the example app and realized again that I would need to implement downloading and caching the model files in order to use the feature. We've used rn-fetch-blob for this in the past, but it turns out that it is no longer maintained. I spent a little time researching other libraries, including expo-file-system (which depends on expo core) and react-native-fs. Those libraries are behemoths that handle a multitude of use cases beyond what we need for this library. So, I went ahead and changed the options here to automatically download and cache the respective files, which simplifies the example app greatly. This will also simplify the tray and reduce its dependency size.

I'm not 100% confident in the Swift and Kotlin code I ended up with to download and cache the model files. I'm more happy with the Swift code because it downloads all of the model files in parallel before finally executing the builder. I think the Android side is synchronous, but it has the advantage of being very simple, and it doesn't seem to take very long to download and then initialize.

timmywil commented 3 years ago

The strategy here has been switched to support either remote URLs or source objects (i.e. the return value of require('./filter.tflite')). Two options have also been added. One to indicate whether to download over cellular (allowCellularDownloads) and another to refresh the model files even if they are present in the cache (refreshModels).

I've also added the close calls on the i/o streams in Android.