Repository navigation
Add Paraformer Rust API example add rust paraformer example #3707
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
csukuangfj
merged 6 commits into
k2-fsa:master
from
nati2405:add-rust-paraformer-example
Jul 29, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
91346db
Add Paraformer Rust API example
nati2405 2952bff
Add Paraformer Rust example script
nati2405 f09ebe5
Make Paraformer run script executable
nati2405 026c6ae
Address review feedback for Paraformer example
nati2405 ed767b9
Fix README markdown formatting
nati2405 da4f4a2
Fix README Paraformer table entry
nati2405 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
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,108 @@ | ||
| // This example demonstrates how to use non-streaming Paraformer with | ||
| // sherpa-onnx's Rust API for offline speech recognition. | ||
| // | ||
| // Run it from the rust-api-examples directory with: | ||
| // ./run-paraformer.sh | ||
|
|
||
| use clap::Parser; | ||
| use sherpa_onnx::{ | ||
| OfflineParaformerModelConfig, OfflineRecognizer, OfflineRecognizerConfig, Wave, | ||
| }; | ||
| use std::{process, time::Instant}; | ||
|
|
||
| /// Paraformer offline ASR example | ||
| #[derive(Parser, Debug)] | ||
| #[command(author, version, about, long_about = None)] | ||
| struct Args { | ||
| /// Path to WAV file | ||
| #[arg(long)] | ||
| wav: String, | ||
|
|
||
| /// Path to Paraformer ONNX model | ||
| #[arg(long)] | ||
| model: String, | ||
|
|
||
| /// Path to tokens file | ||
| #[arg(long)] | ||
| tokens: String, | ||
|
|
||
| /// Provider (default: cpu) | ||
| #[arg(long, default_value = "cpu")] | ||
| provider: String, | ||
|
|
||
| /// Enable debug logs | ||
| #[arg(long, default_value_t = false)] | ||
| debug: bool, | ||
|
|
||
| /// Number of threads | ||
| #[arg(long, default_value_t = 2)] | ||
| num_threads: i32, | ||
| } | ||
|
|
||
| fn main() { | ||
| let args = Args::parse(); | ||
|
|
||
| let wave = Wave::read(&args.wav).expect("Failed to read WAV file"); | ||
| let audio_duration = if wave.sample_rate() > 0 && !wave.samples().is_empty() { | ||
| wave.samples().len() as f64 / wave.sample_rate() as f64 | ||
| } else { | ||
| 0.0 | ||
| }; | ||
|
|
||
| let mut recognizer_config = OfflineRecognizerConfig::default(); | ||
|
|
||
| recognizer_config.model_config.paraformer = OfflineParaformerModelConfig { | ||
| model: Some(args.model.clone()), | ||
| }; | ||
|
|
||
| recognizer_config.model_config.tokens = Some(args.tokens.clone()); | ||
| recognizer_config.model_config.provider = Some(args.provider.clone()); | ||
| recognizer_config.model_config.debug = args.debug; | ||
| recognizer_config.model_config.num_threads = args.num_threads; | ||
|
|
||
| println!("Creating recognizer ..."); | ||
| let start_creation = Instant::now(); | ||
|
|
||
| let recognizer = OfflineRecognizer::create(&recognizer_config) | ||
| .expect("Failed to create OfflineRecognizer"); | ||
|
|
||
| let creation_elapsed = start_creation.elapsed().as_secs_f64(); | ||
| println!("Recognizer created in {:.3} seconds.", creation_elapsed); | ||
|
|
||
| let stream = recognizer.create_stream(); | ||
|
|
||
| let start_recognition = Instant::now(); | ||
|
|
||
| stream.accept_waveform(wave.sample_rate(), wave.samples()); | ||
| recognizer.decode(&stream); | ||
|
|
||
| let recognition_elapsed = start_recognition.elapsed().as_secs_f64(); | ||
|
|
||
| if let Some(result) = stream.get_result() { | ||
| println!("Decoded text: {}", result.text); | ||
|
|
||
| let total_time = creation_elapsed + recognition_elapsed; | ||
| let rtf = if audio_duration > 0.0 { | ||
| recognition_elapsed / audio_duration | ||
| } else { | ||
| 0.0 | ||
| }; | ||
|
|
||
| println!("\n=== Performance Summary ==="); | ||
| println!("Audio duration : {:.3} seconds", audio_duration); | ||
| println!("Recognizer creation time: {:.3} seconds", creation_elapsed); | ||
| println!("Recognition time : {:.3} seconds", recognition_elapsed); | ||
| println!("Total elapsed time : {:.3} seconds", total_time); | ||
| println!( | ||
| "Real-Time Factor (RTF): {:.3} (recognition_elapsed / audio_duration = {:.3} / {:.3})", | ||
| rtf, recognition_elapsed, audio_duration | ||
| ); | ||
| println!( | ||
| "Number of threads : {}", | ||
| recognizer_config.model_config.num_threads | ||
| ); | ||
| } else { | ||
| eprintln!("Failed to get recognition result"); | ||
| process::exit(1); | ||
| } | ||
| } | ||
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,32 @@ | ||
| #!/usr/bin/env bash | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also update |
||
|
|
||
| set -ex | ||
|
|
||
| script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| cd "$script_dir" | ||
|
|
||
| repo_url=https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models | ||
| model=sherpa-onnx-paraformer-zh-small-2024-03-09 | ||
| archive=$model.tar.bz2 | ||
|
|
||
| model_dir="$script_dir/$model" | ||
| archive_path="$script_dir/$archive" | ||
|
|
||
| if [ ! -d "$model_dir" ]; then | ||
| if command -v wget >/dev/null 2>&1; then | ||
| wget -O "$archive_path" "$repo_url/$archive" | ||
| elif command -v curl >/dev/null 2>&1; then | ||
| curl -L -o "$archive_path" "$repo_url/$archive" | ||
| else | ||
| echo "Error: Neither wget nor curl found. Please install one of them." | ||
| exit 1 | ||
| fi | ||
|
|
||
| tar xvf "$archive_path" -C "$script_dir" | ||
| rm "$archive_path" | ||
| fi | ||
|
|
||
| cargo run --example paraformer -- \ | ||
| --model "$model_dir/model.int8.onnx" \ | ||
| --tokens "$model_dir/tokens.txt" \ | ||
| --wav "$model_dir/test_wavs/0.wav" | ||
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.
Uh oh!
There was an error while loading. Please reload this page.