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
11 changes: 11 additions & 0 deletions .github/scripts/test-python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ log() {
echo -e "$(date '+%Y-%m-%d %H:%M:%S') (${fname}:${BASH_LINENO[0]}:${FUNCNAME[1]}) $*"
}

log "test Google MedASR"
curl -SL -O 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
ls -lh sherpa-onnx-medasr-ctc-en-int8-2025-12-25

ls -lh sherpa-onnx-medasr-ctc-en-int8-2025-12-25/test_wavs

python3 ./python-api-examples/offline-medasr-ctc-decode-files.py
rm -rf sherpa-onnx-medasr-ctc-en-int8-2025-12-25

log "test omnilingual ASR"
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-omnilingual-asr-1600-languages-300M-ctc-int8-2025-11-12.tar.bz2
tar xvf sherpa-onnx-omnilingual-asr-1600-languages-300M-ctc-int8-2025-11-12.tar.bz2
Expand Down
14 changes: 10 additions & 4 deletions .github/workflows/export-medasr-ctc-to-onnx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: export-medasr-ctc-to-onnx
on:
push:
branches:
- export-medasr-onnx
- cpp-medasr-2
workflow_dispatch:

concurrency:
Expand Down Expand Up @@ -42,7 +42,9 @@ jobs:
run: |
cd scripts/medasr

curl -SL -O https://huggingface.co/csukuangfj/sherpa-onnx-medasr-ctc-en-int8-2025-12-25/resolve/main/test_wavs/0.wav
for i in $(seq 0 5); do
curl -SL -O https://huggingface.co/csukuangfj/sherpa-onnx-medasr-ctc-en-int8-2025-12-25/resolve/main/test_wavs/$i.wav
done

curl -SL -O https://huggingface.co/csukuangfj/sherpa-onnx-medasr-ctc-en-int8-2025-12-25/resolve/main/test_wavs/transcript.txt

Expand All @@ -53,7 +55,9 @@ jobs:
run: |
cd scripts/medasr

python3 test_onnx.py --model ./model.onnx --tokens ./tokens.txt --wav ./0.wav
for i in $(seq 0 5); do
python3 test_onnx.py --model ./model.onnx --tokens ./tokens.txt --wav ./$i.wav
done

cat transcript.txt

Expand All @@ -62,7 +66,9 @@ jobs:
run: |
cd scripts/medasr

python3 test_onnx.py --model ./model.int8.onnx --tokens ./tokens.txt --wav ./0.wav
for i in $(seq 0 5); do
python3 test_onnx.py --model ./model.int8.onnx --tokens ./tokens.txt --wav ./$i.wav
done

cat transcript.txt

Expand Down
142 changes: 142 additions & 0 deletions python-api-examples/offline-medasr-ctc-decode-files.py
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,
)
Comment on lines +26 to +66

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The create_recognizer function 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.

def create_recognizer():
    model_dir = Path("./sherpa-onnx-medasr-ctc-en-int8-2025-12-25")
    model = model_dir / "model.int8.onnx"
    tokens = model_dir / "tokens.txt"

    test_wavs = [model_dir / "test_wavs" / f"{i}.wav" for i in range(6)]

    all_files = [model, tokens] + test_wavs
    for f in all_files:
        if not 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
                """
            )

    recognizer = sherpa_onnx.OfflineRecognizer.from_medasr_ctc(
        model=str(model),
        tokens=str(tokens),
        num_threads=2,
    )
    return (recognizer,) + tuple(str(w) for w in test_wavs)



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()
1 change: 1 addition & 0 deletions scripts/medasr/export_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def main():
"model_author": "google",
"maintainer": "k2-fsa",
"vocab_size": processor.tokenizer.vocab_size,
"subsampling_factor": 4,
"url": "https://github.com/Google-Health/medasr",
"license": "https://developers.google.com/health-ai-developer-foundations/terms",
}
Expand Down
2 changes: 2 additions & 0 deletions sherpa-onnx/csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ set(sources
offline-fire-red-asr-model.cc
offline-lm-config.cc
offline-lm.cc
offline-medasr-ctc-model-config.cc
offline-medasr-ctc-model.cc
offline-model-config.cc
offline-moonshine-greedy-search-decoder.cc
offline-moonshine-model-config.cc
Expand Down
5 changes: 5 additions & 0 deletions sherpa-onnx/csrc/offline-ctc-model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "sherpa-onnx/csrc/file-utils.h"
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/offline-dolphin-model.h"
#include "sherpa-onnx/csrc/offline-medasr-ctc-model.h"
#include "sherpa-onnx/csrc/offline-nemo-enc-dec-ctc-model.h"
#include "sherpa-onnx/csrc/offline-omnilingual-asr-ctc-model.h"
#include "sherpa-onnx/csrc/offline-tdnn-ctc-model.h"
Expand Down Expand Up @@ -126,6 +127,8 @@ std::unique_ptr<OfflineCtcModel> OfflineCtcModel::Create(
return std::make_unique<OfflineTeleSpeechCtcModel>(config);
} else if (!config.omnilingual.model.empty()) {
return std::make_unique<OfflineOmnilingualAsrCtcModel>(config);
} else if (!config.medasr.model.empty()) {
return std::make_unique<OfflineMedAsrCtcModel>(config);
}

// TODO(fangjun): Refactor it. We don't need to use model_type here
Expand Down Expand Up @@ -192,6 +195,8 @@ std::unique_ptr<OfflineCtcModel> OfflineCtcModel::Create(
return std::make_unique<OfflineTeleSpeechCtcModel>(mgr, config);
} else if (!config.omnilingual.model.empty()) {
return std::make_unique<OfflineOmnilingualAsrCtcModel>(mgr, config);
} else if (!config.medasr.model.empty()) {
return std::make_unique<OfflineMedAsrCtcModel>(mgr, config);
}

// TODO(fangjun): Refactor it. We don't need to use model_type here
Expand Down
40 changes: 40 additions & 0 deletions sherpa-onnx/csrc/offline-medasr-ctc-model-config.cc
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"
Comment thread
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
29 changes: 29 additions & 0 deletions sherpa-onnx/csrc/offline-medasr-ctc-model-config.h
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_
Loading
Loading