|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +This file shows how to use a non-streaming Google MedASR CTC model from |
| 5 | +https://huggingface.co/google/medasr |
| 6 | +to decode files. |
| 7 | +
|
| 8 | +Please download model files from |
| 9 | +https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models |
| 10 | +
|
| 11 | +For instance, |
| 12 | +
|
| 13 | +wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-medasr-ctc-en-int8-2025-12-25.tar.bz2 |
| 14 | +tar xvf sherpa-onnx-medasr-ctc-en-int8-2025-12-25.tar.bz2 |
| 15 | +rm sherpa-onnx-medasr-ctc-en-int8-2025-12-25.tar.bz2 |
| 16 | +""" |
| 17 | + |
| 18 | +import time |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +import librosa |
| 22 | +import numpy as np |
| 23 | +import sherpa_onnx |
| 24 | + |
| 25 | + |
| 26 | +def create_recognizer(): |
| 27 | + model = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/model.int8.onnx" |
| 28 | + tokens = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/tokens.txt" |
| 29 | + test_wav_0 = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/test_wavs/0.wav" |
| 30 | + test_wav_1 = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/test_wavs/1.wav" |
| 31 | + test_wav_2 = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/test_wavs/2.wav" |
| 32 | + test_wav_3 = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/test_wavs/3.wav" |
| 33 | + test_wav_4 = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/test_wavs/4.wav" |
| 34 | + test_wav_5 = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/test_wavs/5.wav" |
| 35 | + |
| 36 | + for f in [ |
| 37 | + model, |
| 38 | + tokens, |
| 39 | + test_wav_0, |
| 40 | + test_wav_1, |
| 41 | + test_wav_2, |
| 42 | + test_wav_3, |
| 43 | + test_wav_4, |
| 44 | + test_wav_5, |
| 45 | + ]: |
| 46 | + if not Path(f).is_file(): |
| 47 | + print(f"{f} does not exist") |
| 48 | + |
| 49 | + raise ValueError( |
| 50 | + """Please download model files from |
| 51 | + https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models |
| 52 | + """ |
| 53 | + ) |
| 54 | + return ( |
| 55 | + sherpa_onnx.OfflineRecognizer.from_medasr_ctc( |
| 56 | + model=model, |
| 57 | + tokens=tokens, |
| 58 | + num_threads=2, |
| 59 | + ), |
| 60 | + test_wav_0, |
| 61 | + test_wav_1, |
| 62 | + test_wav_2, |
| 63 | + test_wav_3, |
| 64 | + test_wav_4, |
| 65 | + test_wav_5, |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +def load_audio(filename): |
| 70 | + audio, sample_rate = librosa.load(filename, sr=16000) |
| 71 | + assert sample_rate == 16000, sample_rate |
| 72 | + |
| 73 | + return np.ascontiguousarray(audio) |
| 74 | + |
| 75 | + |
| 76 | +def decode_single_file(recognizer, filename): |
| 77 | + samples = load_audio(filename) |
| 78 | + |
| 79 | + start_time = time.time() |
| 80 | + |
| 81 | + stream = recognizer.create_stream() |
| 82 | + stream.accept_waveform(sample_rate=16000, waveform=samples) |
| 83 | + recognizer.decode_stream(stream) |
| 84 | + |
| 85 | + end_time = time.time() |
| 86 | + elapsed_seconds = end_time - start_time |
| 87 | + audio_duration = len(samples) / 16000 |
| 88 | + real_time_factor = elapsed_seconds / audio_duration |
| 89 | + |
| 90 | + print("---") |
| 91 | + print(filename) |
| 92 | + print(stream.result) |
| 93 | + print(f"Elapsed seconds: {elapsed_seconds:.3f}") |
| 94 | + print(f"Audio duration in seconds: {audio_duration:.3f}") |
| 95 | + print(f"RTF: {elapsed_seconds:.3f}/{audio_duration:.3f} = {real_time_factor:.3f}") |
| 96 | + print() |
| 97 | + |
| 98 | + |
| 99 | +def decode_multiple_files(recognizer, filenames): |
| 100 | + streams = [] |
| 101 | + |
| 102 | + start_time = time.time() |
| 103 | + |
| 104 | + audio_duration = 0 |
| 105 | + |
| 106 | + for filename in filenames: |
| 107 | + samples = load_audio(filename) |
| 108 | + audio_duration += len(samples) / 16000 |
| 109 | + |
| 110 | + stream = recognizer.create_stream() |
| 111 | + stream.accept_waveform(sample_rate=16000, waveform=samples) |
| 112 | + streams.append(stream) |
| 113 | + |
| 114 | + recognizer.decode_streams(streams) |
| 115 | + |
| 116 | + end_time = time.time() |
| 117 | + elapsed_seconds = end_time - start_time |
| 118 | + real_time_factor = elapsed_seconds / audio_duration |
| 119 | + |
| 120 | + for name, stream in zip(filenames, streams): |
| 121 | + print("---") |
| 122 | + print(name) |
| 123 | + print(stream.result) |
| 124 | + print() |
| 125 | + |
| 126 | + print(f"Elapsed seconds: {elapsed_seconds:.3f}") |
| 127 | + print(f"Audio duration in seconds: {audio_duration:.3f}") |
| 128 | + print(f"RTF: {elapsed_seconds:.3f}/{audio_duration:.3f} = {real_time_factor:.3f}") |
| 129 | + print() |
| 130 | + print() |
| 131 | + |
| 132 | + |
| 133 | +def main(): |
| 134 | + recognizer, *filenames = create_recognizer() |
| 135 | + |
| 136 | + decode_single_file(recognizer, filenames[0]) |
| 137 | + decode_single_file(recognizer, filenames[1]) |
| 138 | + decode_multiple_files(recognizer, filenames[2:]) |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == "__main__": |
| 142 | + main() |
0 commit comments