in rust with whisper-rs "0.11.1" crate transcribe the audio file
- User
in rust with whisper-rs "0.11.1" crate transcribe the audio file
- Assistant
in rust with whisper-rs "0.11.1" crate transcribe the audio file
To transcribe an audio file using the whisper-rs crate version 0.11.1 in Rust, you can follow the steps below. This guide assumes you have an audio file (e.g., audio.wav) sampled at 16 kHz mono and a Whisper model file (e.g., ggml-base.en.bin) available.
1. Set Up Your Rust Project
First, create a new Rust project if you haven't already:
cargo new whisper_transcribe
cd whisper_transcribe
2. Add Dependencies
Edit your Cargo.toml file to include the whisper-rs crate and hound (for reading WAV files):
[dependencies]
whisper-rs = "0.11.1"
hound = "3.4.0"
3. Download a Whisper Model
Download one of the Whisper models in GGML format (e.g., ggml-base.en.bin) from the whisper.cpp repository. Place the model file in a known directory.
4. Write the Transcription Code
Create or edit src/main.rs with the following code:
use std::path::Path;
use whisper_rs::{FullParams, SamplingStrategy, WhisperContext};
fn main() {
// Path to your Whisper model file
let model_path = Path::new("/path/to/your/ggml-base.en.bin");
// Initialize the Whisper context with the model
let mut ctx = WhisperContext::new(&model_path).expect("Failed to load model");
// Path to your audio file
let audio_path = "/path/to/your/audio.wav";
// Load audio samples from the WAV file
let reader = hound::WavReader::open(audio_path).expect("Failed to open audio file");
// Ensure the audio is mono and sampled at 16kHz
let spec = reader.spec();
assert_eq!(spec.channels, 1, "Audio file must be mono");
assert_eq!(spec.sample_rate, 16000, "Audio must be sampled at 16 kHz");
// Read the audio samples into a Vec<f32>
let samples: Vec<f32> = reader
.into_samples::<i16>()
.filter_map(Result::ok)
.map(|s| s as f32)
.collect();
// Set up the transcription parameters
let mut params = FullParams::new(SamplingStrategy::Greedy { best_of: 1 });
params.set_language(Some("en")); // Set the language to English
// Create a state for the model and run the transcription
let mut state = ctx.create_state().expect("Failed to create state");
state.full(params, &samples).expect("Failed to run model");
// Retrieve and print the transcription segments
let num_segments = state.full_n_segments();
for i in 0..num_segments {
let segment_text = state
.full_get_segment_text(i)
.expect("Failed to get segment text");
println!("{}", segment_text);
}
}
5. Explanation of the Code
Initialize the Whisper Context: Load the Whisper model using WhisperContext::new().
Load Audio Samples: Use the hound crate to read the WAV audio file. Ensure it's mono and sampled at 16 kHz, as required by Whisper.
Prepare Audio Data: Convert the audio samples into Vec<f32> format.
Set Transcription Parameters: Configure FullParams for the transcription. Here, we're using a greedy sampling strategy and specifying the language as English.
Run the Model: Create a model state and run the transcription with state.full().
Retrieve Transcription: Iterate over the segments and print out the transcribed text.
6. Compile and Run
Make sure to replace "/path/to/your/ggml-base.en.bin" and "/path/to/your/audio.wav" with the actual paths to your model and audio file.
Compile and run your program:
cargo run
7. Notes
Audio Format: Whisper requires audio data sampled at 16 kHz mono. If your audio file is in a different format, you'll need to convert it before running the transcription. You can use tools like ffmpeg to convert your audio file:
ffmpeg -i input_audio.mp3 -ac 1 -ar 16000 audio.wav
Model Files: Depending on your needs, you might choose different model sizes (e.g., tiny, base, small, medium, large). Larger models may provide better accuracy at the cost of increased computational resources.
Language Support: If you're transcribing non-English audio, set the appropriate language code in params.set_language(Some("language_code")). For multilingual support, you can leave it as None.
8. Additional Resources
Example Output
When you run the program, it will print the transcribed text segments to the console:
Hello, this is a test audio file.
It is being used to demonstrate transcription with whisper-rs.
Conclusion
You've successfully transcribed an audio file using the whisper-rs crate in Rust. This setup provides a foundation for building more advanced speech-to-text applications, such as voice assistants, transcription services, or language processing tools.