Skip to content

Commit

Permalink
Add C API for FireRedAsr AED model. (#1871)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Feb 17, 2025
1 parent d148860 commit 193d313
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 9 deletions.
50 changes: 41 additions & 9 deletions .github/workflows/c-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
os: [ubuntu-latest, macos-latest, ubuntu-22.04-arm]

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -69,7 +69,7 @@ jobs:
ls -lh install/lib
ls -lh install/include
if [[ ${{ matrix.os }} == ubuntu-latest ]]; then
if [[ ${{ matrix.os }} == ubuntu-latest || ${{ matrix.os }} == ubuntu-22.04-arm ]]; then
ldd ./install/lib/libsherpa-onnx-c-api.so
echo "---"
readelf -d ./install/lib/libsherpa-onnx-c-api.so
Expand All @@ -79,6 +79,38 @@ jobs:
otool -L ./install/lib/libsherpa-onnx-c-api.dylib
fi
- name: Test FireRedAsr
shell: bash
run: |
gcc -o fire-red-asr-c-api ./c-api-examples/fire-red-asr-c-api.c \
-I ./build/install/include \
-L ./build/install/lib/ \
-l sherpa-onnx-c-api \
-l onnxruntime
ls -lh fire-red-asr-c-api
if [[ ${{ matrix.os }} == ubuntu-latest || ${{ matrix.os }} == ubuntu-22.04-arm ]]; then
ldd ./fire-red-asr-c-api
echo "----"
readelf -d ./fire-red-asr-c-api
fi
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16.tar.bz2
tar xvf sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16.tar.bz2
rm sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16.tar.bz2
ls -lh sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16
echo "---"
ls -lh sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16/test_wavs
export LD_LIBRARY_PATH=$PWD/build/install/lib:$LD_LIBRARY_PATH
export DYLD_LIBRARY_PATH=$PWD/build/install/lib:$DYLD_LIBRARY_PATH
./fire-red-asr-c-api
rm -rf sherpa-onnx-fire-red-asr-*
- name: Test kws (zh)
shell: bash
run: |
Expand Down Expand Up @@ -301,7 +333,7 @@ jobs:
ls -lh vad-sense-voice-c-api
if [[ ${{ matrix.os }} == ubuntu-latest ]]; then
if [[ ${{ matrix.os }} == ubuntu-latest || ${{ matrix.os }} == ubuntu-22.04-arm ]]; then
ldd ./vad-sense-voice-c-api
echo "----"
readelf -d ./vad-sense-voice-c-api
Expand Down Expand Up @@ -340,7 +372,7 @@ jobs:
ls -lh sense-voice-c-api
if [[ ${{ matrix.os }} == ubuntu-latest ]]; then
if [[ ${{ matrix.os }} == ubuntu-latest || ${{ matrix.os }} == ubuntu-22.04-arm ]]; then
ldd ./sense-voice-c-api
echo "----"
readelf -d ./sense-voice-c-api
Expand Down Expand Up @@ -373,7 +405,7 @@ jobs:
ls -lh whisper-c-api
if [[ ${{ matrix.os }} == ubuntu-latest ]]; then
if [[ ${{ matrix.os }} == ubuntu-latest || ${{ matrix.os }} == ubuntu-22.04-arm ]]; then
ldd ./whisper-c-api
echo "----"
readelf -d ./whisper-c-api
Expand Down Expand Up @@ -405,7 +437,7 @@ jobs:
ls -lh zipformer-c-api
if [[ ${{ matrix.os }} == ubuntu-latest ]]; then
if [[ ${{ matrix.os }} == ubuntu-latest || ${{ matrix.os }} == ubuntu-22.04-arm ]]; then
ldd ./zipformer-c-api
echo "----"
readelf -d ./zipformer-c-api
Expand Down Expand Up @@ -437,7 +469,7 @@ jobs:
ls -lh streaming-zipformer-c-api
if [[ ${{ matrix.os }} == ubuntu-latest ]]; then
if [[ ${{ matrix.os }} == ubuntu-latest || ${{ matrix.os }} == ubuntu-22.04-arm ]]; then
ldd ./streaming-zipformer-c-api
echo "----"
readelf -d ./streaming-zipformer-c-api
Expand Down Expand Up @@ -469,7 +501,7 @@ jobs:
ls -lh paraformer-c-api
if [[ ${{ matrix.os }} == ubuntu-latest ]]; then
if [[ ${{ matrix.os }} == ubuntu-latest || ${{ matrix.os }} == ubuntu-22.04-arm ]]; then
ldd ./paraformer-c-api
echo "----"
readelf -d ./paraformer-c-api
Expand Down Expand Up @@ -501,7 +533,7 @@ jobs:
ls -lh streaming-paraformer-c-api
if [[ ${{ matrix.os }} == ubuntu-latest ]]; then
if [[ ${{ matrix.os }} == ubuntu-latest || ${{ matrix.os }} == ubuntu-22.04-arm ]]; then
ldd ./streaming-paraformer-c-api
echo "----"
readelf -d ./streaming-paraformer-c-api
Expand Down
3 changes: 3 additions & 0 deletions c-api-examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ target_link_libraries(add-punctuation-c-api sherpa-onnx-c-api)
add_executable(whisper-c-api whisper-c-api.c)
target_link_libraries(whisper-c-api sherpa-onnx-c-api)

add_executable(fire-red-asr-c-api fire-red-asr-c-api.c)
target_link_libraries(fire-red-asr-c-api sherpa-onnx-c-api)

add_executable(sense-voice-c-api sense-voice-c-api.c)
target_link_libraries(sense-voice-c-api sherpa-onnx-c-api)

Expand Down
84 changes: 84 additions & 0 deletions c-api-examples/fire-red-asr-c-api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// c-api-examples/fire-red-asr-c-api.c
//
// Copyright (c) 2025 Xiaomi Corporation

// We assume you have pre-downloaded the FireRedAsr model
// from https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models
// An example is given below:
//
// clang-format off
//
// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16.tar.bz2
// tar xvf sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16.tar.bz2
// rm sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16.tar.bz2
//
// clang-format on

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sherpa-onnx/c-api/c-api.h"

int32_t main() {
const char *wav_filename =
"./sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16/test_wavs/0.wav";
const char *encoder_filename =
"sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16/encoder.int8.onnx";
const char *decoder_filename =
"sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16/decoder.int8.onnx";
const char *tokens_filename =
"sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16/tokens.txt";
const char *provider = "cpu";

const SherpaOnnxWave *wave = SherpaOnnxReadWave(wav_filename);
if (wave == NULL) {
fprintf(stderr, "Failed to read %s\n", wav_filename);
return -1;
}

// Offline model config
SherpaOnnxOfflineModelConfig offline_model_config;
memset(&offline_model_config, 0, sizeof(offline_model_config));
offline_model_config.debug = 1;
offline_model_config.num_threads = 1;
offline_model_config.provider = provider;
offline_model_config.tokens = tokens_filename;
offline_model_config.fire_red_asr.encoder = encoder_filename;
offline_model_config.fire_red_asr.decoder = decoder_filename;

// Recognizer config
SherpaOnnxOfflineRecognizerConfig recognizer_config;
memset(&recognizer_config, 0, sizeof(recognizer_config));
recognizer_config.decoding_method = "greedy_search";
recognizer_config.model_config = offline_model_config;

const SherpaOnnxOfflineRecognizer *recognizer =
SherpaOnnxCreateOfflineRecognizer(&recognizer_config);

if (recognizer == NULL) {
fprintf(stderr, "Please check your config!\n");

SherpaOnnxFreeWave(wave);

return -1;
}

const SherpaOnnxOfflineStream *stream =
SherpaOnnxCreateOfflineStream(recognizer);

SherpaOnnxAcceptWaveformOffline(stream, wave->sample_rate, wave->samples,
wave->num_samples);
SherpaOnnxDecodeOfflineStream(recognizer, stream);
const SherpaOnnxOfflineRecognizerResult *result =
SherpaOnnxGetOfflineStreamResult(stream);

fprintf(stderr, "Decoded text: %s\n", result->text);

SherpaOnnxDestroyOfflineRecognizerResult(result);
SherpaOnnxDestroyOfflineStream(stream);
SherpaOnnxDestroyOfflineRecognizer(recognizer);
SherpaOnnxFreeWave(wave);

return 0;
}
6 changes: 6 additions & 0 deletions sherpa-onnx/c-api/c-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,12 @@ static sherpa_onnx::OfflineRecognizerConfig GetOfflineRecognizerConfig(
recognizer_config.model_config.moonshine.cached_decoder =
SHERPA_ONNX_OR(config->model_config.moonshine.cached_decoder, "");

recognizer_config.model_config.fire_red_asr.encoder =
SHERPA_ONNX_OR(config->model_config.fire_red_asr.encoder, "");

recognizer_config.model_config.fire_red_asr.decoder =
SHERPA_ONNX_OR(config->model_config.fire_red_asr.decoder, "");

recognizer_config.lm_config.model =
SHERPA_ONNX_OR(config->lm_config.model, "");
recognizer_config.lm_config.scale =
Expand Down
6 changes: 6 additions & 0 deletions sherpa-onnx/c-api/c-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@ SHERPA_ONNX_API typedef struct SherpaOnnxOfflineWhisperModelConfig {
int32_t tail_paddings;
} SherpaOnnxOfflineWhisperModelConfig;

SHERPA_ONNX_API typedef struct SherpaOnnxOfflineFireRedAsrModelConfig {
const char *encoder;
const char *decoder;
} SherpaOnnxOfflineFireRedAsrModelConfig;

SHERPA_ONNX_API typedef struct SherpaOnnxOfflineMoonshineModelConfig {
const char *preprocessor;
const char *encoder;
Expand Down Expand Up @@ -432,6 +437,7 @@ SHERPA_ONNX_API typedef struct SherpaOnnxOfflineModelConfig {
const char *telespeech_ctc;
SherpaOnnxOfflineSenseVoiceModelConfig sense_voice;
SherpaOnnxOfflineMoonshineModelConfig moonshine;
SherpaOnnxOfflineFireRedAsrModelConfig fire_red_asr;
} SherpaOnnxOfflineModelConfig;

SHERPA_ONNX_API typedef struct SherpaOnnxOfflineRecognizerConfig {
Expand Down

0 comments on commit 193d313

Please # to comment.