Fix C# APIs (#183)

* Fix c# APIs

* reformat
This commit is contained in:
Fangjun Kuang
2023-06-24 00:56:53 +08:00
committed by GitHub
parent 81579bbddd
commit 157b6b801b
4 changed files with 84 additions and 56 deletions

View File

@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.13 FATAL_ERROR) cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(sherpa-onnx) project(sherpa-onnx)
set(SHERPA_ONNX_VERSION "1.4.4") set(SHERPA_ONNX_VERSION "1.4.5")
# Disable warning about # Disable warning about
# #

View File

@@ -5,59 +5,54 @@
// This file shows how to use sherpa-onnx C API // This file shows how to use sherpa-onnx C API
// to decode a file. // to decode a file.
#include "cargs.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "cargs.h"
#include "sherpa-onnx/c-api/c-api.h" #include "sherpa-onnx/c-api/c-api.h"
static struct cag_option options[] = { static struct cag_option options[] = {
{ {.identifier = 'h',
.identifier = 't', .access_letters = "h",
.access_letters = NULL, .access_name = "help",
.access_name = "tokens", .description = "Show help"},
.value_name = "tokens", {.identifier = 't',
.description = "Tokens file" .access_letters = NULL,
}, { .access_name = "tokens",
.identifier = 'e', .value_name = "tokens",
.access_letters = NULL, .description = "Tokens file"},
.access_name = "encoder", {.identifier = 'e',
.value_name = "encoder", .access_letters = NULL,
.description = "Encoder ONNX file" .access_name = "encoder",
}, { .value_name = "encoder",
.identifier = 'd', .description = "Encoder ONNX file"},
.access_letters = NULL, {.identifier = 'd',
.access_name = "decoder", .access_letters = NULL,
.value_name = "decoder", .access_name = "decoder",
.description = "Decoder ONNX file" .value_name = "decoder",
}, { .description = "Decoder ONNX file"},
.identifier = 'j', {.identifier = 'j',
.access_letters = NULL, .access_letters = NULL,
.access_name = "joiner", .access_name = "joiner",
.value_name = "joiner", .value_name = "joiner",
.description = "Joiner ONNX file" .description = "Joiner ONNX file"},
}, { {.identifier = 'n',
.identifier = 'n', .access_letters = NULL,
.access_letters = NULL, .access_name = "num-threads",
.access_name = "num-threads", .value_name = "num-threads",
.value_name = "num-threads", .description = "Number of threads"},
.description = "Number of threads" {.identifier = 'p',
}, { .access_letters = NULL,
.identifier = 'p', .access_name = "provider",
.access_letters = NULL, .value_name = "provider",
.access_name = "provider", .description = "Provider: cpu (default), cuda, coreml"},
.value_name = "provider", {.identifier = 'm',
.description = "Provider: cpu (default), cuda, coreml" .access_letters = NULL,
}, { .access_name = "decoding-method",
.identifier = 'm', .value_name = "decoding-method",
.access_letters = NULL, .description =
.access_name = "decoding-method", "Decoding method: greedy_search (default), modified_beam_search"}};
.value_name = "decoding-method",
.description =
"Decoding method: greedy_search (default), modified_beam_search"
}
};
const char *kUsage = const char *kUsage =
"\n" "\n"
@@ -67,6 +62,7 @@ const char *kUsage =
" --encoder=/path/to/encoder.onnx \\\n" " --encoder=/path/to/encoder.onnx \\\n"
" --decoder=/path/to/decoder.onnx \\\n" " --decoder=/path/to/decoder.onnx \\\n"
" --joiner=/path/to/joiner.onnx \\\n" " --joiner=/path/to/joiner.onnx \\\n"
" --provider=cpu \\\n"
" /path/to/foo.wav\n" " /path/to/foo.wav\n"
"\n\n" "\n\n"
"Default num_threads is 1.\n" "Default num_threads is 1.\n"
@@ -77,6 +73,11 @@ const char *kUsage =
"for a list of pre-trained models to download.\n"; "for a list of pre-trained models to download.\n";
int32_t main(int32_t argc, char *argv[]) { int32_t main(int32_t argc, char *argv[]) {
if (argc < 6) {
fprintf(stderr, "%s\n", kUsage);
exit(0);
}
SherpaOnnxOnlineRecognizerConfig config; SherpaOnnxOnlineRecognizerConfig config;
config.model_config.debug = 0; config.model_config.debug = 0;
@@ -105,13 +106,32 @@ int32_t main(int32_t argc, char *argv[]) {
identifier = cag_option_get(&context); identifier = cag_option_get(&context);
value = cag_option_get_value(&context); value = cag_option_get_value(&context);
switch (identifier) { switch (identifier) {
case 't': config.model_config.tokens = value; break; case 't':
case 'e': config.model_config.encoder = value; break; config.model_config.tokens = value;
case 'd': config.model_config.decoder = value; break; break;
case 'j': config.model_config.joiner = value; break; case 'e':
case 'n': config.model_config.num_threads = atoi(value); break; config.model_config.encoder = value;
case 'p': config.model_config.provider = value; break; break;
case 'm': config.decoding_method = value; break; case 'd':
config.model_config.decoder = value;
break;
case 'j':
config.model_config.joiner = value;
break;
case 'n':
config.model_config.num_threads = atoi(value);
break;
case 'p':
config.model_config.provider = value;
break;
case 'm':
config.decoding_method = value;
break;
case 'h': {
fprintf(stderr, "%s\n", kUsage);
exit(0);
break;
}
default: default:
// do nothing as config already have valid default values // do nothing as config already have valid default values
break; break;

View File

@@ -20,6 +20,9 @@ class OnlineDecodeFiles
[Option(Required = true, HelpText = "Path to tokens.txt")] [Option(Required = true, HelpText = "Path to tokens.txt")]
public string Tokens { get; set; } public string Tokens { get; set; }
[Option(Required = false, Default = "cpu", HelpText = "Provider, e.g., cpu, coreml")]
public string Provider { get; set; }
[Option(Required = true, HelpText = "Path to encoder.onnx")] [Option(Required = true, HelpText = "Path to encoder.onnx")]
public string Encoder { get; set; } public string Encoder { get; set; }
@@ -124,6 +127,7 @@ to download pre-trained streaming models.
config.TransducerModelConfig.Decoder = options.Decoder; config.TransducerModelConfig.Decoder = options.Decoder;
config.TransducerModelConfig.Joiner = options.Joiner; config.TransducerModelConfig.Joiner = options.Joiner;
config.TransducerModelConfig.Tokens = options.Tokens; config.TransducerModelConfig.Tokens = options.Tokens;
config.TransducerModelConfig.Provider = options.Provider;
config.TransducerModelConfig.NumThreads = options.NumThreads; config.TransducerModelConfig.NumThreads = options.NumThreads;
config.TransducerModelConfig.Debug = options.Debug ? 1 : 0; config.TransducerModelConfig.Debug = options.Debug ? 1 : 0;

View File

@@ -23,6 +23,7 @@ namespace SherpaOnnx
Joiner = ""; Joiner = "";
Tokens = ""; Tokens = "";
NumThreads = 1; NumThreads = 1;
Provider = "cpu";
Debug = 0; Debug = 0;
} }
[MarshalAs(UnmanagedType.LPStr)] [MarshalAs(UnmanagedType.LPStr)]
@@ -40,6 +41,9 @@ namespace SherpaOnnx
/// Number of threads used to run the neural network model /// Number of threads used to run the neural network model
public int NumThreads; public int NumThreads;
[MarshalAs(UnmanagedType.LPStr)]
public string Provider;
/// true to print debug information of the model /// true to print debug information of the model
public int Debug; public int Debug;
} }