Repository navigation
Add Rust API for streaming speech recognition #3204
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -ex | ||
|
|
||
| cd rust-api-examples | ||
|
|
||
| ./run-version.sh | ||
|
|
||
| ./run-streaming-zipformer.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| [package] | ||
| name = "rust-api-examples" | ||
| version = "0.1.0" | ||
| version = "0.1.1" | ||
| edition = "2021" | ||
|
|
||
|
|
||
| [dependencies] | ||
| sherpa-onnx = "0.1.0" | ||
| sherpa-onnx = "0.1.1" | ||
| # sherpa-onnx = { path = "../sherpa-onnx/rust/sherpa-onnx" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright (c) 2026 Xiaomi Corporation | ||
| // | ||
| // This file demonstrates how to use streaming Zipformer with sherpa-onnx's | ||
| // Rust API for speech recognition. | ||
| // | ||
| // See ../README.md for how to run it | ||
| // | ||
| // Note that even if we use a wave file as an example, this model supports | ||
| // real-time streaming speech recognition. You can read audio samples | ||
| // from a microphone. | ||
|
|
||
| use sherpa_onnx::{OnlineRecognizer, OnlineRecognizerConfig, Wave}; | ||
|
|
||
| fn main() { | ||
| let wav_path = "sherpa-onnx-streaming-zipformer-en-2023-06-21/test_wavs/1.wav"; | ||
| let encoder_path = | ||
| "sherpa-onnx-streaming-zipformer-en-2023-06-21/encoder-epoch-99-avg-1.int8.onnx"; | ||
| let decoder_path = "sherpa-onnx-streaming-zipformer-en-2023-06-21/decoder-epoch-99-avg-1.onnx"; | ||
| let joiner_path = | ||
| "sherpa-onnx-streaming-zipformer-en-2023-06-21/joiner-epoch-99-avg-1.int8.onnx"; | ||
| let tokens_path = "sherpa-onnx-streaming-zipformer-en-2023-06-21/tokens.txt"; | ||
| let provider = "cpu"; | ||
|
|
||
| let wave = Wave::read(wav_path).expect("Failed to read WAV file"); | ||
|
|
||
| let mut recognizer_config = OnlineRecognizerConfig::default(); | ||
| recognizer_config.model_config.transducer.encoder = Some(encoder_path.to_string()); | ||
| recognizer_config.model_config.transducer.decoder = Some(decoder_path.to_string()); | ||
| recognizer_config.model_config.transducer.joiner = Some(joiner_path.to_string()); | ||
| recognizer_config.model_config.tokens = Some(tokens_path.to_string()); | ||
| recognizer_config.model_config.provider = Some(provider.to_string()); | ||
| recognizer_config.enable_endpoint = true; | ||
|
|
||
| // set to true to see verbose logs | ||
| recognizer_config.model_config.debug = true; | ||
|
|
||
| recognizer_config.decoding_method = Some("greedy_search".to_string()); | ||
|
|
||
| let recognizer = | ||
| OnlineRecognizer::create(&recognizer_config).expect("Failed to create OnlineRecognizer"); | ||
|
|
||
| let stream = recognizer.create_stream(); | ||
|
|
||
| let mut segment_id = 0; | ||
|
|
||
| // use any positive value as you like | ||
| const CHUNK_SIZE: usize = 3200; | ||
|
|
||
| println!( | ||
| "Sample rate: {}, num samples: {}, duration: {:.2}s", | ||
| wave.sample_rate(), | ||
| wave.num_samples(), | ||
| wave.num_samples() as f32 / wave.sample_rate() as f32 | ||
| ); | ||
|
|
||
| for chunk in wave.samples().chunks(CHUNK_SIZE) { | ||
| stream.accept_waveform(wave.sample_rate(), chunk); | ||
|
|
||
| while recognizer.is_ready(&stream) { | ||
| recognizer.decode(&stream); | ||
|
|
||
| if let Some(result) = recognizer.get_result(&stream) { | ||
| if !result.text.is_empty() { | ||
| println!("Segment {}: {}", segment_id, result.text); | ||
| } | ||
| } | ||
|
|
||
| if recognizer.is_endpoint(&stream) { | ||
| recognizer.reset(&stream); | ||
| segment_id += 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Tail padding (~0.3s) | ||
| let tail_padding_len = (wave.sample_rate() as f32 * 0.3).round() as usize; | ||
| let tail_padding = vec![0.0f32; tail_padding_len]; | ||
|
|
||
| stream.accept_waveform(wave.sample_rate(), &tail_padding); | ||
|
|
||
| stream.input_finished(); | ||
|
|
||
| while recognizer.is_ready(&stream) { | ||
| recognizer.decode(&stream); | ||
| if let Some(result) = recognizer.get_result(&stream) { | ||
| if !result.text.is_empty() { | ||
| println!("Segment {}: {}", segment_id, result.text); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| println!("Transcription finished."); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #!/usr/bin/env bash | ||
| set -ex | ||
|
|
||
| if [ ! -f ./sherpa-onnx-streaming-zipformer-en-2023-06-21/encoder-epoch-99-avg-1.int8.onnx ]; then | ||
| curl -SsL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-en-2023-06-21.tar.bz2 | ||
|
|
||
| tar xvf sherpa-onnx-streaming-zipformer-en-2023-06-21.tar.bz2 | ||
| rm sherpa-onnx-streaming-zipformer-en-2023-06-21.tar.bz2 | ||
| ls -lh sherpa-onnx-streaming-zipformer-en-2023-06-21 | ||
| fi | ||
|
|
||
| cargo run --example streaming_zipformer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #!/usr/bin/env bash | ||
| set -ex | ||
| cargo run --example version |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid relying on executable bit for the script.
Using
./will fail if the file isn’t marked executable. Consider invoking viabashto make the workflow robust.✅ Suggested tweak
🤖 Prompt for AI Agents