Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/scripts/test-rust.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ rm -rf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12-int8

./run-fire-red-asr-ctc.sh

./run-paraformer.sh
rm -rf sherpa-onnx-paraformer-zh-small-2024-03-09

./run-silero-vad-remove-silence.sh

./run-ten-vad-remove-silence.sh
Expand Down
8 changes: 6 additions & 2 deletions rust-api-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ in your own Cargo project, see
| 46 | [zipformer_transducer_simulate_streaming_microphone](#example-46-simulated-streaming-asr-with-zipformer-transducer-japanese-and-vad-from-microphone) | Simulated streaming ASR with Zipformer transducer (Japanese) and VAD from microphone |
| 47 | [qwen3_asr_simulate_streaming_microphone](#example-47-simulated-streaming-asr-with-qwen3-asr-and-vad-from-microphone) | Simulated streaming ASR with Qwen3 ASR and VAD from microphone |
| 48 | [whisper](#example-48-asr-with-non-streaming-whisper) | Non-streaming ASR with Whisper (multilingual) |
| 49 | [funasr_nano](#example-49-asr-with-non-streaming-funasr-nano) | Non-streaming ASR with FunASR Nano |
| 50 | [ten_vad_remove_silence](#example-50-remove-silences-from-a-file-using-ten-vad) | Remove silences from an audio file using ten-vad |
| 51 | [paraformer](#example-51-asr-with-non-streaming-paraformer) | Non-streaming ASR with Paraformer |

## Run it

Expand Down Expand Up @@ -436,3 +435,8 @@ Qwen3 ASR recognizer on each detected segment.
```bash
./run-ten-vad-remove-silence.sh
```

### Example 51: ASR with non-streaming Paraformer

```bash
./run-paraformer.sh
108 changes: 108 additions & 0 deletions rust-api-examples/examples/paraformer.rs
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);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
32 changes: 32 additions & 0 deletions rust-api-examples/run-paraformer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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"