k2-fsa / sherpa-onnx

Speech-to-text, text-to-speech, speaker diarization, and VAD using next-gen Kaldi with onnxruntime without Internet connection. Support embedded systems, Android, iOS, Raspberry Pi, RISC-V, x86_64 servers, websocket server/client, C/C++, Python, Kotlin, C#, Go, NodeJS, Java, Swift, Dart, JavaScript, Flutter, Object Pascal, Lazarus, Rust
https://k2-fsa.github.io/sherpa/onnx/index.html
Apache License 2.0
3.67k stars 427 forks source link

Unable to run it in MacOS with Swift APi examples #1368

Closed Intiserahmed closed 3 weeks ago

Intiserahmed commented 2 months ago

Hi, iOS is properly working, but when i try with MacOS build script with swift api examples, kindda confuse how to use it ? Any step by step guide for macos?

csukuangfj commented 2 months ago

The following is an example:

git clone https://github.com/k2-fsa/sherpa-onnx

cd sherpa-onnx
./build-swift-macos.sh

cd swift-api-examples

# there are several ./run-xxx.sh
# For instance,

./run-decode-file.sh

./run-tts.sh
Intiserahmed commented 2 months ago

@csukuangfj yeah i already run it, but how to use it with xcode project any example or hints please?

csukuangfj commented 2 months ago

We have provided the required header file, libraries, and swift APIs.

Could you tell us the difficulty you have? Would be great if you can post error logs.

EnotPoloskun commented 3 weeks ago

Hey @csukuangfj

I believe that what @Intiserahmed means is that there is example of how to use shepra-onnx in ios application, but there is no example of how to use it in MacOs(desktop) if it is even possible.

I am getting following error during build when I added framework from ios example to my MacOS desktop application

/.../Frameworks/sherpa-onnx.xcframework:1:1 While building for macOS, no library for this platform was found in '/../Frameworks/sherpa-onnx.xcframework'.
csukuangfj commented 3 weeks ago

but there is no example of how to use it in MacOs(desktop) if it is even possible.

By the way, there is nothing special with sherpa-onnx.

For macOS app development, we provide shared libraries with C API header files. The xcframework is for iOS development only.

We do provide Swift API examples. Please see https://github.com/k2-fsa/sherpa-onnx/issues/1368#issuecomment-2365064156

EnotPoloskun commented 3 weeks ago

Yeah, I see, but it is still not clear what I need to add and do with my xcode project in order to use shepra-onnx.

In other words - in one of the examples you have code like this:

  var config = sherpaOnnxOfflineSpeakerDiarizationConfig(
    segmentation: sherpaOnnxOfflineSpeakerSegmentationModelConfig(
      pyannote: sherpaOnnxOfflineSpeakerSegmentationPyannoteModelConfig(model: segmentationModel)),
    embedding: sherpaOnnxSpeakerEmbeddingExtractorConfig(model: embeddingExtractorModel),
    clustering: sherpaOnnxFastClusteringConfig(numClusters: numSpeakers)
  )

  let sd = SherpaOnnxOfflineSpeakerDiarizationWrapper(config: &config)

What I need to add to xcode project in order to be able to use sherpaOnnxOfflineSpeakerDiarizationConfig and other shepra-onnx functions. Thank you in advance.

(I am pretty new in ios/macos development and never used external non swift libraries/packages in my project, so maybe I am missing something trivial)

csukuangfj commented 3 weeks ago

I am pretty new in ios/macos development and never used external non swift libraries/packages in my project,

In that case, I suggest that you first spend some time learning how to do that.

We have provided everything you need to be used in your macos project. You have to learn how to add which file to which place.

You may find https://k2-fsa.github.io/sherpa/ncnn/ios/for-the-more-curious-swift.html helpful, though you need to replace the xcframework with the shared library since the xcframework is for ios, not for macos.

csukuangfj commented 3 weeks ago

@EnotPoloskun

https://github.com/k2-fsa/sherpa-onnx/releases/download/v1.10.30/sherpa-onnx-v1.10.30-macos-xcframework-static.tar.bz2

Here is the xcframework for sherpa-onnx that supports both macos x86_64 and macos arm64.

To use it in your macos xcode project, please do the following:

  1. Add the bridging header to your project Screenshot 2024-10-29 at 11 19 23

  2. Add SherpaOnnx.swift to your project Screenshot 2024-10-29 at 11 21 02

  3. Add sherpa-onnx.xcframework to your project

Screenshot 2024-10-29 at 11 22 45

  1. Add the libonnxruntime.a to your project

You can find it from https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.17.1/onnxruntime-osx-universal2-static_lib-1.17.1.zip

Screenshot 2024-10-29 at 11 32 15

  1. Add the directory containing libonnxruntime.a to the library search path

Screenshot 2024-10-29 at 11 33 56

  1. Add -lc++ as the other linker flag Screenshot 2024-10-29 at 11 35 48

  2. Done! Now you can use the Swift API of sherpa-onnx in your xcode project.

Please see how little code you need to write to set it up.

Intiserahmed commented 3 weeks ago

@csukuangfj Thanks a lot for the specific instructions.

EnotPoloskun commented 3 weeks ago

@csukuangfj It worked like a charm first try. Really appreciate it. Thank you very much.