mirror of
https://github.com/k2-fsa/sherpa-onnx.git
synced 2026-11-10 13:04:05 +08:00
Add C API and CXX API for Cohere transcribe models (#3457)
This commit is contained in:
1 parent
6fb6f7d693
commit
6ee89f0242
12 files changed
+301
-3
No files matched your search
@@ -76,6 +76,36 @@ jobs:
|
||||
otool -L ./install/lib/libsherpa-onnx-c-api.dylib
|
||||
fi
|
||||
|
||||
- name: Test Cohere Transcribe
|
||||
shell: bash
|
||||
run: |
|
||||
name=cohere-transcribe-c-api
|
||||
gcc -o $name ./c-api-examples/$name.c \
|
||||
-I ./build/install/include \
|
||||
-L ./build/install/lib/ \
|
||||
-l sherpa-onnx-c-api \
|
||||
-l onnxruntime
|
||||
|
||||
ls -lh $name
|
||||
|
||||
if [[ ${{ matrix.os }} == ubuntu-latest || ${{ matrix.os }} == ubuntu-22.04-arm ]]; then
|
||||
ldd ./$name
|
||||
echo "----"
|
||||
readelf -d ./$name
|
||||
fi
|
||||
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
|
||||
tar xvf sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
|
||||
rm sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
|
||||
|
||||
export LD_LIBRARY_PATH=$PWD/build/install/lib:$LD_LIBRARY_PATH
|
||||
export DYLD_LIBRARY_PATH=$PWD/build/install/lib:$DYLD_LIBRARY_PATH
|
||||
|
||||
./$name
|
||||
|
||||
rm $name
|
||||
rm -rf sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01
|
||||
|
||||
- name: Test Qwen3 ASR
|
||||
if: matrix.os != 'ubuntu-latest'
|
||||
shell: bash
|
||||
|
||||
@@ -5,6 +5,7 @@ on:
|
||||
branches:
|
||||
- master
|
||||
- cxx-api
|
||||
- c-api
|
||||
paths:
|
||||
- '.github/workflows/cxx-api.yaml'
|
||||
- 'cmake/**'
|
||||
@@ -78,6 +79,37 @@ jobs:
|
||||
otool -L ./install/lib/libsherpa-onnx-cxx-api.dylib
|
||||
fi
|
||||
|
||||
- name: Test Cohere Transcribe
|
||||
shell: bash
|
||||
run: |
|
||||
name=cohere-transcribe-cxx-api
|
||||
g++ -std=c++17 -o $name ./cxx-api-examples/$name.cc \
|
||||
-I ./build/install/include \
|
||||
-L ./build/install/lib/ \
|
||||
-l sherpa-onnx-cxx-api \
|
||||
-l sherpa-onnx-c-api \
|
||||
-l onnxruntime
|
||||
|
||||
ls -lh $name
|
||||
|
||||
if [[ ${{ matrix.os }} == ubuntu-latest || ${{ matrix.os }} == ubuntu-22.04-arm ]]; then
|
||||
ldd ./$name
|
||||
echo "----"
|
||||
readelf -d ./$name
|
||||
fi
|
||||
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
|
||||
tar xvf sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
|
||||
rm sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
|
||||
|
||||
export LD_LIBRARY_PATH=$PWD/build/install/lib:$LD_LIBRARY_PATH
|
||||
export DYLD_LIBRARY_PATH=$PWD/build/install/lib:$DYLD_LIBRARY_PATH
|
||||
|
||||
./$name
|
||||
|
||||
rm -rf sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01
|
||||
rm $name
|
||||
|
||||
- name: Test Qwen3 ASR
|
||||
if: matrix.os != 'ubuntu-latest'
|
||||
shell: bash
|
||||
|
||||
@@ -91,6 +91,9 @@ target_link_libraries(fire-red-asr-c-api sherpa-onnx-c-api)
|
||||
add_executable(nemo-canary-c-api nemo-canary-c-api.c)
|
||||
target_link_libraries(nemo-canary-c-api sherpa-onnx-c-api)
|
||||
|
||||
add_executable(cohere-transcribe-c-api cohere-transcribe-c-api.c)
|
||||
target_link_libraries(cohere-transcribe-c-api sherpa-onnx-c-api)
|
||||
|
||||
add_executable(nemo-parakeet-c-api nemo-parakeet-c-api.c)
|
||||
target_link_libraries(nemo-parakeet-c-api sherpa-onnx-c-api)
|
||||
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
// c-api-examples/cohere-transcribe-c-api.c
|
||||
//
|
||||
// Copyright (c) 2026 Xiaomi Corporation
|
||||
|
||||
// We assume you have pre-downloaded the Cohere Transcribe 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-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
|
||||
// tar xvf sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
|
||||
// rm sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
|
||||
//
|
||||
// clang-format on
|
||||
|
||||
#include <stdint.h>
|
||||
#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-cohere-transcribe-14-lang-int8-2026-04-01/test_wavs/"
|
||||
"en.wav";
|
||||
const char *encoder_filename =
|
||||
"sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01/encoder.int8.onnx";
|
||||
const char *decoder_filename =
|
||||
"sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01/decoder.int8.onnx";
|
||||
const char *tokens_filename =
|
||||
"sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01/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;
|
||||
}
|
||||
|
||||
SherpaOnnxOfflineModelConfig offline_model_config;
|
||||
memset(&offline_model_config, 0, sizeof(offline_model_config));
|
||||
offline_model_config.debug = 0;
|
||||
offline_model_config.num_threads = 1;
|
||||
offline_model_config.provider = provider;
|
||||
offline_model_config.tokens = tokens_filename;
|
||||
offline_model_config.cohere_transcribe.encoder = encoder_filename;
|
||||
offline_model_config.cohere_transcribe.decoder = decoder_filename;
|
||||
offline_model_config.cohere_transcribe.use_punct = 1;
|
||||
offline_model_config.cohere_transcribe.use_itn = 1;
|
||||
|
||||
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);
|
||||
|
||||
// we are testing an English wave file, so we use the language en
|
||||
SherpaOnnxOfflineStreamSetOption(stream, "language", "en");
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -73,6 +73,9 @@ target_link_libraries(medasr-ctc-cxx-api sherpa-onnx-cxx-api)
|
||||
add_executable(nemo-canary-cxx-api ./nemo-canary-cxx-api.cc)
|
||||
target_link_libraries(nemo-canary-cxx-api sherpa-onnx-cxx-api)
|
||||
|
||||
add_executable(cohere-transcribe-cxx-api ./cohere-transcribe-cxx-api.cc)
|
||||
target_link_libraries(cohere-transcribe-cxx-api sherpa-onnx-cxx-api)
|
||||
|
||||
add_executable(offline-punctuation-cxx-api ./offline-punctuation-cxx-api.cc)
|
||||
target_link_libraries(offline-punctuation-cxx-api sherpa-onnx-cxx-api)
|
||||
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
// cxx-api-examples/cohere-transcribe-cxx-api.cc
|
||||
//
|
||||
// Copyright (c) 2026 Xiaomi Corporation
|
||||
//
|
||||
// This file demonstrates how to use Cohere Transcribe models with
|
||||
// sherpa-onnx's C++ API wrapper.
|
||||
//
|
||||
// clang-format off
|
||||
//
|
||||
// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
|
||||
// tar xvf sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
|
||||
// rm sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01.tar.bz2
|
||||
//
|
||||
// clang-format on
|
||||
|
||||
#include <chrono> // NOLINT
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "sherpa-onnx/c-api/cxx-api.h"
|
||||
|
||||
int32_t main() {
|
||||
using namespace sherpa_onnx::cxx; // NOLINT
|
||||
OfflineRecognizerConfig config;
|
||||
|
||||
config.model_config.cohere_transcribe.encoder =
|
||||
"./sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01/encoder.int8.onnx";
|
||||
config.model_config.cohere_transcribe.decoder =
|
||||
"./sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01/decoder.int8.onnx";
|
||||
config.model_config.cohere_transcribe.use_punct = true;
|
||||
config.model_config.cohere_transcribe.use_itn = true;
|
||||
config.model_config.tokens =
|
||||
"./sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01/tokens.txt";
|
||||
|
||||
config.model_config.num_threads = 1;
|
||||
config.model_config.provider = "cpu";
|
||||
|
||||
std::cout << "Loading model\n";
|
||||
OfflineRecognizer recognizer = OfflineRecognizer::Create(config);
|
||||
if (!recognizer.Get()) {
|
||||
std::cerr << "Please check your config\n";
|
||||
return -1;
|
||||
}
|
||||
std::cout << "Loading model done\n";
|
||||
|
||||
std::string wave_filename =
|
||||
"./sherpa-onnx-cohere-transcribe-14-lang-int8-2026-04-01/test_wavs/en.wav";
|
||||
Wave wave = ReadWave(wave_filename);
|
||||
if (wave.samples.empty()) {
|
||||
std::cerr << "Failed to read: '" << wave_filename << "'\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::cout << "Start recognition\n";
|
||||
const auto begin = std::chrono::steady_clock::now();
|
||||
|
||||
OfflineStream stream = recognizer.CreateStream();
|
||||
stream.SetOption("language", "en");
|
||||
stream.AcceptWaveform(wave.sample_rate, wave.samples.data(),
|
||||
wave.samples.size());
|
||||
|
||||
recognizer.Decode(&stream);
|
||||
|
||||
OfflineRecognizerResult result = recognizer.GetResult(&stream);
|
||||
|
||||
const auto end = std::chrono::steady_clock::now();
|
||||
const float elapsed_seconds =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(end - begin)
|
||||
.count() /
|
||||
1000.;
|
||||
float duration = wave.samples.size() / static_cast<float>(wave.sample_rate);
|
||||
float rtf = elapsed_seconds / duration;
|
||||
|
||||
std::cout << "text: " << result.text << "\n";
|
||||
printf("Number of threads: %d\n", config.model_config.num_threads);
|
||||
printf("Duration: %.3fs\n", duration);
|
||||
printf("Elapsed seconds: %.3fs\n", elapsed_seconds);
|
||||
printf("(Real time factor) RTF = %.3f / %.3f = %.3f\n", elapsed_seconds,
|
||||
duration, rtf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
This file shows how to use a non-streaming Canary model from NeMo
|
||||
This file shows how to use a non-streaming Cohere Transcribe model
|
||||
to decode files.
|
||||
|
||||
Please download model files from
|
||||
|
||||
@@ -542,6 +542,21 @@ static sherpa_onnx::OfflineRecognizerConfig GetOfflineRecognizerConfig(
|
||||
recognizer_config.model_config.canary.use_pnc =
|
||||
config->model_config.canary.use_pnc;
|
||||
|
||||
recognizer_config.model_config.cohere_transcribe.encoder =
|
||||
SHERPA_ONNX_OR(config->model_config.cohere_transcribe.encoder, "");
|
||||
|
||||
recognizer_config.model_config.cohere_transcribe.decoder =
|
||||
SHERPA_ONNX_OR(config->model_config.cohere_transcribe.decoder, "");
|
||||
|
||||
recognizer_config.model_config.cohere_transcribe.language =
|
||||
SHERPA_ONNX_OR(config->model_config.cohere_transcribe.language, "");
|
||||
|
||||
recognizer_config.model_config.cohere_transcribe.use_punct =
|
||||
config->model_config.cohere_transcribe.use_punct;
|
||||
|
||||
recognizer_config.model_config.cohere_transcribe.use_itn =
|
||||
config->model_config.cohere_transcribe.use_itn;
|
||||
|
||||
recognizer_config.model_config.wenet_ctc.model =
|
||||
SHERPA_ONNX_OR(config->model_config.wenet_ctc.model, "");
|
||||
|
||||
|
||||
@@ -875,6 +875,20 @@ typedef struct SherpaOnnxOfflineCanaryModelConfig {
|
||||
int32_t use_pnc;
|
||||
} SherpaOnnxOfflineCanaryModelConfig;
|
||||
|
||||
/** @brief Configuration for a Cohere Transcribe model. */
|
||||
typedef struct SherpaOnnxOfflineCohereTranscribeModelConfig {
|
||||
/** Path to the encoder ONNX model. */
|
||||
const char *encoder;
|
||||
/** Path to the decoder ONNX model. */
|
||||
const char *decoder;
|
||||
/** Optional language hint, for example "en" or "zh". */
|
||||
const char *language;
|
||||
/** Non-zero to enable punctuation. */
|
||||
int32_t use_punct;
|
||||
/** Non-zero to enable inverse text normalization. */
|
||||
int32_t use_itn;
|
||||
} SherpaOnnxOfflineCohereTranscribeModelConfig;
|
||||
|
||||
/** @brief Configuration for a FireRedAsr encoder/decoder model. */
|
||||
typedef struct SherpaOnnxOfflineFireRedAsrModelConfig {
|
||||
/** Path to the encoder ONNX model. */
|
||||
@@ -1021,8 +1035,9 @@ typedef struct SherpaOnnxOfflineMedAsrCtcModelConfig {
|
||||
* Exactly one model family should be configured for each recognizer. For
|
||||
* example, set only one of @c transducer, @c paraformer, @c nemo_ctc,
|
||||
* @c whisper, @c tdnn, @c sense_voice, @c moonshine, @c fire_red_asr,
|
||||
* @c dolphin, @c zipformer_ctc, @c canary, @c wenet_ctc, @c omnilingual,
|
||||
* @c medasr, @c funasr_nano, @c fire_red_asr_ctc, or @c qwen3_asr.
|
||||
* @c dolphin, @c zipformer_ctc, @c canary, @c cohere_transcribe,
|
||||
* @c wenet_ctc, @c omnilingual, @c medasr, @c funasr_nano,
|
||||
* @c fire_red_asr_ctc, or @c qwen3_asr.
|
||||
*
|
||||
* If multiple model families are configured at the same time, the
|
||||
* implementation will choose one of them, and which one is used is
|
||||
@@ -1080,6 +1095,8 @@ typedef struct SherpaOnnxOfflineModelConfig {
|
||||
SherpaOnnxOfflineFireRedAsrCtcModelConfig fire_red_asr_ctc;
|
||||
/** Qwen3-ASR configuration. */
|
||||
SherpaOnnxOfflineQwen3ASRModelConfig qwen3_asr;
|
||||
/** Cohere Transcribe configuration. */
|
||||
SherpaOnnxOfflineCohereTranscribeModelConfig cohere_transcribe;
|
||||
} SherpaOnnxOfflineModelConfig;
|
||||
|
||||
/**
|
||||
|
||||
@@ -310,6 +310,17 @@ static SherpaOnnxOfflineRecognizerConfig Convert(
|
||||
c.model_config.canary.tgt_lang = config.model_config.canary.tgt_lang.c_str();
|
||||
c.model_config.canary.use_pnc = config.model_config.canary.use_pnc;
|
||||
|
||||
c.model_config.cohere_transcribe.encoder =
|
||||
config.model_config.cohere_transcribe.encoder.c_str();
|
||||
c.model_config.cohere_transcribe.decoder =
|
||||
config.model_config.cohere_transcribe.decoder.c_str();
|
||||
c.model_config.cohere_transcribe.language =
|
||||
config.model_config.cohere_transcribe.language.c_str();
|
||||
c.model_config.cohere_transcribe.use_punct =
|
||||
config.model_config.cohere_transcribe.use_punct;
|
||||
c.model_config.cohere_transcribe.use_itn =
|
||||
config.model_config.cohere_transcribe.use_itn;
|
||||
|
||||
c.model_config.wenet_ctc.model = config.model_config.wenet_ctc.model.c_str();
|
||||
|
||||
c.model_config.omnilingual.model =
|
||||
|
||||
@@ -475,6 +475,20 @@ struct OfflineCanaryModelConfig {
|
||||
bool use_pnc = true;
|
||||
};
|
||||
|
||||
/** @brief Offline Cohere Transcribe model configuration. */
|
||||
struct OfflineCohereTranscribeModelConfig {
|
||||
/** Encoder ONNX model. */
|
||||
std::string encoder;
|
||||
/** Decoder ONNX model. */
|
||||
std::string decoder;
|
||||
/** Cohere language string such as `"en"` or `"zh"`. */
|
||||
std::string language;
|
||||
/** Whether punctuation is enabled by the model. */
|
||||
bool use_punct = true;
|
||||
/** Whether inverse text normalization is enabled. */
|
||||
bool use_itn = true;
|
||||
};
|
||||
|
||||
/** @brief Offline FireRed ASR model files. */
|
||||
struct OfflineFireRedAsrModelConfig {
|
||||
/** Encoder ONNX model. */
|
||||
@@ -662,6 +676,8 @@ struct OfflineModelConfig {
|
||||
OfflineFireRedAsrCtcModelConfig fire_red_asr_ctc;
|
||||
/** Qwen3-ASR configuration. */
|
||||
OfflineQwen3ASRModelConfig qwen3_asr;
|
||||
/** Cohere Transcribe configuration. */
|
||||
OfflineCohereTranscribeModelConfig cohere_transcribe;
|
||||
};
|
||||
|
||||
/** @brief Optional language-model rescoring configuration for offline ASR. */
|
||||
|
||||
@@ -89,6 +89,7 @@ Useful examples include:
|
||||
|
||||
- [`decode-file-c-api.c`](https://github.com/k2-fsa/sherpa-onnx/blob/master/c-api-examples/decode-file-c-api.c)
|
||||
- [`whisper-c-api.c`](https://github.com/k2-fsa/sherpa-onnx/blob/master/c-api-examples/whisper-c-api.c)
|
||||
- [`cohere-transcribe-c-api.c`](https://github.com/k2-fsa/sherpa-onnx/blob/master/c-api-examples/cohere-transcribe-c-api.c)
|
||||
- [`sense-voice-c-api.c`](https://github.com/k2-fsa/sherpa-onnx/blob/master/c-api-examples/sense-voice-c-api.c)
|
||||
- [`nemo-parakeet-c-api.c`](https://github.com/k2-fsa/sherpa-onnx/blob/master/c-api-examples/nemo-parakeet-c-api.c)
|
||||
- [`streaming-zipformer-with-hr-cxx-api.cc`](https://github.com/k2-fsa/sherpa-onnx/blob/master/cxx-api-examples/streaming-zipformer-with-hr-cxx-api.cc)
|
||||
|
||||
Reference in new issue
Block a user