-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add C++ runtime and Python API for Google MedASR models #2935
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
3 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
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,142 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| """ | ||
| This file shows how to use a non-streaming Google MedASR CTC model from | ||
| https://huggingface.co/google/medasr | ||
| to decode files. | ||
|
|
||
| Please download model files from | ||
| https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models | ||
|
|
||
| For instance, | ||
|
|
||
| wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-medasr-ctc-en-int8-2025-12-25.tar.bz2 | ||
| tar xvf sherpa-onnx-medasr-ctc-en-int8-2025-12-25.tar.bz2 | ||
| rm sherpa-onnx-medasr-ctc-en-int8-2025-12-25.tar.bz2 | ||
| """ | ||
|
|
||
| import time | ||
| from pathlib import Path | ||
|
|
||
| import librosa | ||
| import numpy as np | ||
| import sherpa_onnx | ||
|
|
||
|
|
||
| def create_recognizer(): | ||
| model = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/model.int8.onnx" | ||
| tokens = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/tokens.txt" | ||
| test_wav_0 = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/test_wavs/0.wav" | ||
| test_wav_1 = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/test_wavs/1.wav" | ||
| test_wav_2 = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/test_wavs/2.wav" | ||
| test_wav_3 = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/test_wavs/3.wav" | ||
| test_wav_4 = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/test_wavs/4.wav" | ||
| test_wav_5 = "./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/test_wavs/5.wav" | ||
|
|
||
| for f in [ | ||
| model, | ||
| tokens, | ||
| test_wav_0, | ||
| test_wav_1, | ||
| test_wav_2, | ||
| test_wav_3, | ||
| test_wav_4, | ||
| test_wav_5, | ||
| ]: | ||
| if not Path(f).is_file(): | ||
| print(f"{f} does not exist") | ||
|
|
||
| raise ValueError( | ||
| """Please download model files from | ||
| https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models | ||
| """ | ||
| ) | ||
| return ( | ||
| sherpa_onnx.OfflineRecognizer.from_medasr_ctc( | ||
| model=model, | ||
| tokens=tokens, | ||
| num_threads=2, | ||
| ), | ||
| test_wav_0, | ||
| test_wav_1, | ||
| test_wav_2, | ||
| test_wav_3, | ||
| test_wav_4, | ||
| test_wav_5, | ||
| ) | ||
|
|
||
|
|
||
| def load_audio(filename): | ||
| audio, sample_rate = librosa.load(filename, sr=16000) | ||
| assert sample_rate == 16000, sample_rate | ||
|
|
||
| return np.ascontiguousarray(audio) | ||
|
|
||
|
|
||
| def decode_single_file(recognizer, filename): | ||
| samples = load_audio(filename) | ||
|
|
||
| start_time = time.time() | ||
|
|
||
| stream = recognizer.create_stream() | ||
| stream.accept_waveform(sample_rate=16000, waveform=samples) | ||
| recognizer.decode_stream(stream) | ||
|
|
||
| end_time = time.time() | ||
| elapsed_seconds = end_time - start_time | ||
| audio_duration = len(samples) / 16000 | ||
| real_time_factor = elapsed_seconds / audio_duration | ||
|
|
||
| print("---") | ||
| print(filename) | ||
| print(stream.result) | ||
| print(f"Elapsed seconds: {elapsed_seconds:.3f}") | ||
| print(f"Audio duration in seconds: {audio_duration:.3f}") | ||
| print(f"RTF: {elapsed_seconds:.3f}/{audio_duration:.3f} = {real_time_factor:.3f}") | ||
| print() | ||
|
|
||
|
|
||
| def decode_multiple_files(recognizer, filenames): | ||
| streams = [] | ||
|
|
||
| start_time = time.time() | ||
|
|
||
| audio_duration = 0 | ||
|
|
||
| for filename in filenames: | ||
| samples = load_audio(filename) | ||
| audio_duration += len(samples) / 16000 | ||
|
|
||
| stream = recognizer.create_stream() | ||
| stream.accept_waveform(sample_rate=16000, waveform=samples) | ||
| streams.append(stream) | ||
|
|
||
| recognizer.decode_streams(streams) | ||
|
|
||
| end_time = time.time() | ||
| elapsed_seconds = end_time - start_time | ||
| real_time_factor = elapsed_seconds / audio_duration | ||
|
|
||
| for name, stream in zip(filenames, streams): | ||
| print("---") | ||
| print(name) | ||
| print(stream.result) | ||
| print() | ||
|
|
||
| print(f"Elapsed seconds: {elapsed_seconds:.3f}") | ||
| print(f"Audio duration in seconds: {audio_duration:.3f}") | ||
| print(f"RTF: {elapsed_seconds:.3f}/{audio_duration:.3f} = {real_time_factor:.3f}") | ||
| print() | ||
| print() | ||
|
|
||
|
|
||
| def main(): | ||
| recognizer, *filenames = create_recognizer() | ||
|
|
||
| decode_single_file(recognizer, filenames[0]) | ||
| decode_single_file(recognizer, filenames[1]) | ||
| decode_multiple_files(recognizer, filenames[2:]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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
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,40 @@ | ||
| // sherpa-onnx/csrc/offline-medasr-ctc-model-config.cc | ||
| // | ||
| // Copyright (c) 2025 Xiaomi Corporation | ||
|
|
||
| #include "sherpa-onnx/csrc/offline-medasr-ctc-model-config.h" | ||
|
|
||
| #include <sstream> | ||
| #include <string> | ||
|
|
||
| #include "sherpa-onnx/csrc/file-utils.h" | ||
| #include "sherpa-onnx/csrc/macros.h" | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| namespace sherpa_onnx { | ||
|
|
||
| void OfflineMedAsrCtcModelConfig::Register(ParseOptions *po) { | ||
| po->Register( | ||
| "medasr", &model, | ||
| "Path to model.onnx from MedASR. Please see " | ||
| "https://github.com/k2-fsa/sherpa-onnx/pull/2934 for available models"); | ||
| } | ||
|
|
||
| bool OfflineMedAsrCtcModelConfig::Validate() const { | ||
| if (!FileExists(model)) { | ||
| SHERPA_ONNX_LOGE("MedASR model: '%s' does not exist", model.c_str()); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| std::string OfflineMedAsrCtcModelConfig::ToString() const { | ||
| std::ostringstream os; | ||
|
|
||
| os << "OfflineMedAsrCtcModelConfig("; | ||
| os << "model=\"" << model << "\")"; | ||
|
|
||
| return os.str(); | ||
| } | ||
|
|
||
| } // namespace 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // sherpa-onnx/csrc/offline-medasr-ctc-model-config.h | ||
| // | ||
| // Copyright (c) 2025 Xiaomi Corporation | ||
|
|
||
| #ifndef SHERPA_ONNX_CSRC_OFFLINE_MEDASR_CTC_MODEL_CONFIG_H_ | ||
| #define SHERPA_ONNX_CSRC_OFFLINE_MEDASR_CTC_MODEL_CONFIG_H_ | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "sherpa-onnx/csrc/parse-options.h" | ||
|
|
||
| namespace sherpa_onnx { | ||
|
|
||
| struct OfflineMedAsrCtcModelConfig { | ||
| std::string model; | ||
|
|
||
| OfflineMedAsrCtcModelConfig() = default; | ||
| explicit OfflineMedAsrCtcModelConfig(const std::string &model) | ||
| : model(model) {} | ||
|
|
||
| void Register(ParseOptions *po); | ||
| bool Validate() const; | ||
|
|
||
| std::string ToString() const; | ||
| }; | ||
|
|
||
| } // namespace sherpa_onnx | ||
|
|
||
| #endif // SHERPA_ONNX_CSRC_OFFLINE_MEDASR_CTC_MODEL_CONFIG_H_ |
Oops, something went wrong.
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.
The
create_recognizerfunction contains a lot of repeated strings for file paths, which makes it harder to read and maintain. You can refactor it by defining a base path and constructing the file paths programmatically. This will make the code cleaner and less error-prone.