Repository navigation
Add Dart API for FunASR Nano - #3055
Conversation
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughThis change adds FunASR Nano offline speech recognition model support to the Dart/Flutter bindings. It introduces new configuration structs with JSON serialization, native conversion logic, and memory management across multiple layers. New example code and test scripts are provided to demonstrate usage. Changes
Sequence Diagram(s)sequenceDiagram
participant App as Dart App
participant Config as OfflineFunAsrNanoModelConfig
participant Offline as OfflineModelConfig
participant Recognizer as OfflineRecognizer
participant FFI as Native C-API
App->>Config: Create with encoder-adaptor,<br/>llm, embedding, tokenizer
App->>Offline: Create with funasrNano
App->>Recognizer: Initialize with OfflineModelConfig
Recognizer->>FFI: convertConfig() - populate<br/>SherpaOnnxOfflineFunAsrNanoModelConfig
FFI-->>Recognizer: Native config ready
App->>Recognizer: Decode audio stream
Recognizer->>FFI: Process & transcribe
FFI-->>Recognizer: Transcription result
App->>Recognizer: Cleanup
Recognizer->>FFI: freeConfig() - deallocate<br/>native pointers
FFI-->>Recognizer: Resources freed
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Possibly related PRs
Suggested labels
Poem
✨ Finishing touches
📜 Recent review detailsConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (6)
✏️ Tip: You can disable this entire section by setting Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @csukuangfj, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Dart API of the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces Dart API support for FunASR Nano, including a new example application and updates to the core Dart bindings. The changes are well-structured and follow the existing patterns for adding new models. I have a couple of suggestions for the new example file to improve argument handling and ensure robust resource management. Overall, this is a great addition.
| final recognizer = sherpa_onnx.OfflineRecognizer(config); | ||
|
|
||
| final waveData = sherpa_onnx.readWave(inputWav); | ||
| final stream = recognizer.createStream(); | ||
|
|
||
| stream.acceptWaveform( | ||
| samples: waveData.samples, | ||
| sampleRate: waveData.sampleRate, | ||
| ); | ||
| recognizer.decode(stream); | ||
|
|
||
| final result = recognizer.getResult(stream); | ||
| print(result.text); | ||
|
|
||
| stream.free(); | ||
| recognizer.free(); |
There was a problem hiding this comment.
The recognizer and stream resources are manually freed at the end of the main function. If an exception occurs during processing (e.g., while reading the wave file or during decoding), these resources will not be freed, leading to a memory leak. It's safer to use a try...finally block to ensure resources are always released.
sherpa_onnx.OfflineRecognizer? recognizer;
sherpa_onnx.OfflineStream? stream;
try {
recognizer = sherpa_onnx.OfflineRecognizer(config);
final waveData = sherpa_onnx.readWave(inputWav);
stream = recognizer.createStream();
stream.acceptWaveform(
samples: waveData.samples,
sampleRate: waveData.sampleRate,
);
recognizer.decode(stream);
final result = recognizer.getResult(stream);
print(result.text);
} finally {
stream?.free();
recognizer?.free();
}| if (res['encoder-adaptor'] == null || | ||
| res['llm'] == null || | ||
| res['embedding'] == null || | ||
| res['tokenizer'] == null || | ||
| res['input-wav'] == null) { | ||
| print(parser.usage); | ||
| exit(1); | ||
| } |
There was a problem hiding this comment.
The current argument validation works, but it could be improved to provide more specific feedback to the user about which required option is missing. This would enhance the command-line usability of this example.
| if (res['encoder-adaptor'] == null || | |
| res['llm'] == null || | |
| res['embedding'] == null || | |
| res['tokenizer'] == null || | |
| res['input-wav'] == null) { | |
| print(parser.usage); | |
| exit(1); | |
| } | |
| const requiredOptions = [ | |
| 'encoder-adaptor', | |
| 'llm', | |
| 'embedding', | |
| 'tokenizer', | |
| 'input-wav', | |
| ]; | |
| var optionsOk = true; | |
| for (final opt in requiredOptions) { | |
| if (res[opt] == null) { | |
| print('Missing required argument: --$opt'); | |
| optionsOk = false; | |
| } | |
| } | |
| if (!optionsOk) { | |
| print(parser.usage); | |
| exit(1); | |
| } |
Summary by CodeRabbit
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.