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

[Help wanted] Support Flutter #379

Closed
flutter-painter opened this issue Oct 20, 2023 · 22 comments
Closed

[Help wanted] Support Flutter #379

flutter-painter opened this issue Oct 20, 2023 · 22 comments

Comments

@flutter-painter
Copy link
Contributor

Could this possible using ffi ?
Ex : https://pub.dev/packages/onnxruntime

@nmfisher
Copy link
Contributor

@flutter-painter I have already implemented this, was planning to open source it eventually but haven't got it around to it just yet. Send me an e-mail if you'd like.

@flutter-painter
Copy link
Contributor Author

Hi @nmfisher,

Wonderful news,
I just sent an email,
Here is fine as well in case other aficionados wanna hop-in :)

@D-Idan
Copy link

D-Idan commented Jan 20, 2024

Hi
Did you manage to make it happen?

@flutter-painter
Copy link
Contributor Author

Almost, nick's repo is clear and his guidelines are complete.
I just need to build Onnx Runtime...
@nmfisher how about opening it as is ?

@D-Idan
Copy link

D-Idan commented Jan 20, 2024

Let me know if I can help 🙂

@eschmidbauer
Copy link
Contributor

also very interested in this- i have some flutter experience to test/help

@BrutalCoding
Copy link
Contributor

BrutalCoding commented Jan 26, 2024

@csukuangfj Am I right that the c-api only supports desktop platforms? I've tried porting your library over to Flutter today with ffi (https://docs.flutter.dev/platform-integration/android/c-interop) combined with ffigen (https://pub.dev/packages/ffigen).

I haven't pushed all my local changes because I haven't gotten it to work yet, but you can see the generated bindings here: https://github.com/BrutalCoding/aub.ai/blob/b16f7e5f9d317d83b38a5814afb1556c8de8a3d3/lib/aub_ai_bindings_generated.dart

I ran your the android build script for arm64-v8a successfully and they work fine when I'm using your native Android example apps. I can't get it to work in Flutter yet and would like some guidance.

Tree struc of aub.ai (not pushed to my repo, I'm trying to get it working locally first):
aub.ai/android/src/main/jniLibs/arm64-v8a
├── libcargs.so
├── libespeak-ng.so
├── libkaldi-decoder-core.so
├── libkaldi-native-fbank-core.so
├── libonnxruntime.so
├── libpiper_phonemize.so
├── libsherpa-onnx-c-api.so <- I tried with and without the C API.
├── libsherpa-onnx-core.so
├── libsherpa-onnx-fst.so
├── libsherpa-onnx-jni.so
├── libsherpa-onnx-kaldifst-core.so
└── libucd.so

1 directory, 12 files

When I look at your native Android examples, I noticed that Tts.kt is referring to 'sherpa-onnx-jni'.

 companion object {
        init {
            System.loadLibrary("sherpa-onnx-jni")
        }
    }

I've tried that one as well in Flutter by doing the following (aub_ai.dart):

...
return DynamicLibrary.open('libsherpa-onnx-jni.so');
...

But it fails to lookup the native symbols whenever I try to call functions from it. E.g. this fails:

// This function only relies on the onnxModel, the rest is hardcoded. 
 final Pointer<SherpaOnnxOfflineTtsConfig> ttsConfig = _getOfflineTtsConfig(
    pathToOnnxModel: pathToOnnxModel,
  );

  // Runtime error occurs on this line because it's failing to lookup the native symbol 'SherpaOnnxCreateOfflineTts'.
  final tts = sherpaOnnxBindings.SherpaOnnxCreateOfflineTts(ttsConfig);

Any tips?

I can solve it in a different way, by not using the c-api and instead use platform channels to execute native Kotlin code but having the c-api work for Android & iOS too would make long term maintenance easier and improves the dev experience (at least for me).

@nmfisher
Copy link
Contributor

Hi @D-Idan @flutter-painter @eschmidbauer @BrutalCoding - sorry I've been really busy with other projects so haven't responded.

I've basically just flicked the switch to make this repository public:

https://github.com/nmfisher/flutter_sherpa_onnx

This is reasonably stable on iOS/Android (I have it running in production) and I've tested it informally on macos/Windows, seems no issues there.

As it says though, this is totally undocumented so you'll have to look at the code yourself to see how it works. Maybe @flutter-painter can submit a PR adding the notes I sent via email to the README which will give at least some explanation.

@BrutalCoding maybe this will help solve your issue.

@csukuangfj
Copy link
Collaborator

When I look at your native Android examples, I noticed that Tts.kt is referring to 'sherpa-onnx-jni'.

@BrutalCoding

sherpa-onnx-jni is for JVM, which is call by either java or kotlin. I am afraid you cannot use it with flutter. For flutter you can safely ignore libsherpa-onnx-jni.so.


I haven't pushed all my local changes because I haven't gotten it to work yet

Could you describe the issues you have? For instance, you can post error logs.

@BrutalCoding
Copy link
Contributor

Hi @D-Idan @flutter-painter @eschmidbauer @BrutalCoding - sorry I've been really busy with other projects so haven't responded.

I've basically just flicked the switch to make this repository public:

https://github.com/nmfisher/flutter_sherpa_onnx

This is reasonably stable on iOS/Android (I have it running in production) and I've tested it informally on macos/Windows, seems no issues there.

As it says though, this is totally undocumented so you'll have to look at the code yourself to see how it works. Maybe @flutter-painter can submit a PR adding the notes I sent via email to the README which will give at least some explanation.

@BrutalCoding maybe this will help solve your issue.

I haven't dived into your code yet but I really appreciate it. Tried my best to get this to work on my own to no avail. Feels like an unhealthy hackathon for me last 24h. Trying to solve it from 9pm till 6:30am -> sleep -> again at 11am till I posted that comment (~6:30pm).

My day is almost up again but I'm certainly giving it a shot tmr with your repo, many thanks.

Could you describe the issues you have? For instance, you can post error logs.

My bad, I should've emphasized the actual error more, but it's just that Flutter is unable to find symbols in any of the .so files I've randomly picked (e.g. libsherpa-onnx-core.so). To confirm this, I used nm -g <filename>.so to find any symbols this way but there's nothing exposed (by design I guess).

Nonetheless, thanks for your quick reply. I will look into the solution that @nmfisher kindly shared, I don't expect any roadblockers anymore.

PS. Awesome CI/CD setup, it's what I imagined doing myself for a repo of mine but seeing it here is great to learn from. E.g. all binaries for each arch, the release versioning etc.

@flutter-painter
Copy link
Contributor Author

Hi @nmfisher,
Here goes : nmfisher/sherpa_onnx_dart#2

@flutter-painter
Copy link
Contributor Author

Hi,
Could dart ongoing support for native assets ease this integration process ?

@csukuangfj
Copy link
Collaborator

Hi, Could dart ongoing support for native assets ease this integration process ?

Sorry, I cannot understand it. Could you give more details?

@nmfisher
Copy link
Contributor

nmfisher commented Feb 27, 2024 via email

@BrutalCoding
Copy link
Contributor

BrutalCoding commented Feb 28, 2024

Hi, Could dart ongoing support for native assets ease this integration process ?

@flutter-painter While it would be nice, I think it would be better for @csukuangfj to continue focusing on existing work. The good news is that @csukuangfj already has makefiles, shell scripts, example apps for every platform and yeah- basically everything to build a fully working Flutter plugin.

It's probably a better idea for a (experienced/advanced) Flutter (package) dev to look into this project, unless sherpa-onnx is considered finished (it never is :P). While I can't share code or specific details, I can confirm that all the features of sherpa-onnx are working within Flutter. Think about TTS, ASR, VAD etc.

Sorry, I cannot understand it. Could you give more details?

@csukuangfj he is referring to https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli, which is a 1st party plugin (Google, or more specifically someone from the Dart team). It's the next way to interact with native code, basically a replacement for ffi and writing bindings for it. It's still experimental though so that's a 2nd reason that I discourage for you to jump in this potential rabbit hole.

While that is experimental, Flutter does already provide something for years that is robust:
Their 'ffi' package (plugin) combined with their 'ffigen' package, both found on https://pub.dev. Here is something that's easy to follow to allow native code in Flutter that was meant for pure C (note: Flutter uses Dart): https://docs.flutter.dev/platform-integration/android/c-interop

A Flutter dev could take a look at your native Android example projects and read through https://docs.flutter.dev/platform-integration/platform-channels?tab=type-mappings-kotlin-tab to get it working too.

TLDR: Many ways to achieve support of sherpa-onnx in Flutter, but it is challenging IMO if you want to be able to automate this for longevity / long term maintenance. Very do-able, it just takes a lot of time to set it up and test it for every platform.

I thought I'd just share some insights for anyone interested within the Flutter space.

@Web3Kev
Copy link

Web3Kev commented Apr 18, 2024

Hello., just thought I would bump this thread to see if anyone had progressed on a flutter package. I'm particularly interested in the tts function as I am porting a project that had been developed in c# and unity, and was using piper.

I am absolutely new to dart packages and how sherpa actually works but can I use @nmfisher 's repo which seems to have been tailored to speech recognition or should I give ffi a try ?

Thanks in advance.

@nmfisher
Copy link
Contributor

@Web3Kev I'm working on the package at the moment to make it slightly more beginner friendly. Check back in a week or two to see if it's a bit easier to get up and running.

@csukuangfj
Copy link
Collaborator

Please see
#905 (comment)

@csukuangfj
Copy link
Collaborator

Here is the Dart package for sherpa-onnx
https://pub.dev/packages/sherpa_onnx


Pure Dart examples can be found at
https://github.com/k2-fsa/sherpa-onnx/tree/master/dart-api-examples

@beswarm
Copy link

beswarm commented Jun 22, 2024

the sherpa_onnx package seems do not support android and ios, any plans for mobile device support?

@csukuangfj
Copy link
Collaborator

the sherpa_onnx package seems do not support android and ios, any plans for mobile device support?

We need the community help to

The current package is nearly 100 MB. pub.dev limits the maximum package size to 100MB.

@csukuangfj
Copy link
Collaborator

Closing via #1079

See also
https://pub.dev/packages/sherpa_onnx

Screenshot 2024-07-06 at 19 31 56

Help to support the Web platform for Flutter is needed.

Note that we have already supported WebAssembly.

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

8 participants