Exposing ErrorCode API in Tree bindings (#2806)

* Added API to Python bindings

* Added API to JavaScript bindings

* Added API to Java bindings

* Added API to .NET binding
This commit is contained in:
Shubham Kumar 2020-03-21 20:10:39 +05:30 committed by GitHub
parent dfd69e47f1
commit 5d50d21da0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 12 additions and 37 deletions

View File

@ -89,38 +89,9 @@ namespace DeepSpeechClient
/// <param name="resultCode">Native result code.</param> /// <param name="resultCode">Native result code.</param>
private void EvaluateResultCode(ErrorCodes resultCode) private void EvaluateResultCode(ErrorCodes resultCode)
{ {
switch (resultCode) if (resultCode != ErrorCodes.DS_ERR_OK)
{ {
case ErrorCodes.DS_ERR_OK: throw new ArgumentException(NativeImp.DS_ErrorCodeToErrorMessage((int)resultCode).PtrToString());
break;
case ErrorCodes.DS_ERR_NO_MODEL:
throw new ArgumentException("Missing model information.");
case ErrorCodes.DS_ERR_INVALID_ALPHABET:
throw new ArgumentException("Invalid alphabet embedded in model. (Data corruption?)");
case ErrorCodes.DS_ERR_INVALID_SHAPE:
throw new ArgumentException("Invalid model shape.");
case ErrorCodes.DS_ERR_INVALID_SCORER:
throw new ArgumentException("Invalid scorer file.");
case ErrorCodes.DS_ERR_FAIL_INIT_MMAP:
throw new ArgumentException("Failed to initialize memory mapped model.");
case ErrorCodes.DS_ERR_FAIL_INIT_SESS:
throw new ArgumentException("Failed to initialize the session.");
case ErrorCodes.DS_ERR_FAIL_INTERPRETER:
throw new ArgumentException("Interpreter failed.");
case ErrorCodes.DS_ERR_FAIL_RUN_SESS:
throw new ArgumentException("Failed to run the session.");
case ErrorCodes.DS_ERR_FAIL_CREATE_STREAM:
throw new ArgumentException("Error creating the stream.");
case ErrorCodes.DS_ERR_FAIL_READ_PROTOBUF:
throw new ArgumentException("Error reading the proto buffer model file.");
case ErrorCodes.DS_ERR_FAIL_CREATE_SESS:
throw new ArgumentException("Error failed to create session.");
case ErrorCodes.DS_ERR_MODEL_INCOMPATIBLE:
throw new ArgumentException("Error incompatible model.");
case ErrorCodes.DS_ERR_SCORER_NOT_ENABLED:
throw new ArgumentException("External scorer is not enabled.");
default:
throw new ArgumentException("Unknown error, please make sure you are using the correct native binary.");
} }
} }
@ -140,7 +111,6 @@ namespace DeepSpeechClient
/// <exception cref="FileNotFoundException">Thrown when cannot find the scorer file.</exception> /// <exception cref="FileNotFoundException">Thrown when cannot find the scorer file.</exception>
public unsafe void EnableExternalScorer(string aScorerPath) public unsafe void EnableExternalScorer(string aScorerPath)
{ {
string exceptionMessage = null;
if (string.IsNullOrWhiteSpace(aScorerPath)) if (string.IsNullOrWhiteSpace(aScorerPath))
{ {
throw new FileNotFoundException("Path to the scorer file cannot be empty."); throw new FileNotFoundException("Path to the scorer file cannot be empty.");

View File

@ -19,6 +19,9 @@ namespace DeepSpeechClient
internal unsafe static extern ErrorCodes DS_CreateModel(string aModelPath, internal unsafe static extern ErrorCodes DS_CreateModel(string aModelPath,
ref IntPtr** pint); ref IntPtr** pint);
[DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)]
internal unsafe static extern IntPtr DS_ErrorCodeToErrorMessage(int aErrorCode);
[DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)] [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)]
internal unsafe static extern uint DS_GetModelBeamWidth(IntPtr** aCtx); internal unsafe static extern uint DS_GetModelBeamWidth(IntPtr** aCtx);

View File

@ -37,6 +37,7 @@ using namespace node;
%newobject DS_IntermediateDecode; %newobject DS_IntermediateDecode;
%newobject DS_FinishStream; %newobject DS_FinishStream;
%newobject DS_Version; %newobject DS_Version;
%newobject DS_ErrorCodeToErrorMessage;
// convert double pointer retval in CreateModel to an output // convert double pointer retval in CreateModel to an output
%typemap(in, numinputs=0) ModelState **retval (ModelState *ret) { %typemap(in, numinputs=0) ModelState **retval (ModelState *ret) {

View File

@ -2,7 +2,7 @@
const binary = require('node-pre-gyp'); const binary = require('node-pre-gyp');
const path = require('path') const path = require('path')
// 'lib', 'binding', 'v0.1.1', ['node', 'v' + process.versions.modules, process.platform, process.arch].join('-'), 'deepspeech-bingings.node') // 'lib', 'binding', 'v0.1.1', ['node', 'v' + process.versions.modules, process.platform, process.arch].join('-'), 'deepspeech-bindings.node')
const binding_path = binary.find(path.resolve(path.join(__dirname, 'package.json'))); const binding_path = binary.find(path.resolve(path.join(__dirname, 'package.json')));
// On Windows, we can't rely on RPATH being set to $ORIGIN/../ or on // On Windows, we can't rely on RPATH being set to $ORIGIN/../ or on
@ -35,7 +35,7 @@ function Model(aModelPath) {
const status = rets[0]; const status = rets[0];
const impl = rets[1]; const impl = rets[1];
if (status !== 0) { if (status !== 0) {
throw "CreateModel failed with error code 0x" + status.toString(16); throw "CreateModel failed "+binding.ErrorCodeToErrorMessage(status)+" 0x" + status.toString(16);
} }
this._impl = impl; this._impl = impl;
@ -139,7 +139,7 @@ Model.prototype.createStream = function() {
const status = rets[0]; const status = rets[0];
const ctx = rets[1]; const ctx = rets[1];
if (status !== 0) { if (status !== 0) {
throw "CreateStream failed with error code 0x" + status.toString(16); throw "CreateStream failed "+binding.ErrorCodeToErrorMessage(status)+" 0x" + status.toString(16);
} }
return ctx; return ctx;
} }

View File

@ -35,7 +35,7 @@ class Model(object):
status, impl = deepspeech.impl.CreateModel(model_path) status, impl = deepspeech.impl.CreateModel(model_path)
if status != 0: if status != 0:
raise RuntimeError("CreateModel failed with error code 0x{:X}".format(status)) raise RuntimeError("CreateModel failed with '{}' (0x{:X})".format(deepspeech.impl.ErrorCodeToErrorMessage(status),status))
self._impl = impl self._impl = impl
def __del__(self): def __del__(self):
@ -148,7 +148,7 @@ class Model(object):
""" """
status, ctx = deepspeech.impl.CreateStream(self._impl) status, ctx = deepspeech.impl.CreateStream(self._impl)
if status != 0: if status != 0:
raise RuntimeError("CreateStream failed with error code 0x{:X}".format(status)) raise RuntimeError("CreateStream failed with '{}' (0x{:X})".format(deepspeech.impl.ErrorCodeToErrorMessage(status),status))
return Stream(ctx) return Stream(ctx)

View File

@ -121,6 +121,7 @@ static PyObject *parent_reference() {
%newobject DS_IntermediateDecode; %newobject DS_IntermediateDecode;
%newobject DS_FinishStream; %newobject DS_FinishStream;
%newobject DS_Version; %newobject DS_Version;
%newobject DS_ErrorCodeToErrorMessage;
%rename ("%(strip:[DS_])s") ""; %rename ("%(strip:[DS_])s") "";