From 5d50d21da0d34751faab725e2619c8e1c9b1215c Mon Sep 17 00:00:00 2001 From: Shubham Kumar Date: Sat, 21 Mar 2020 20:10:39 +0530 Subject: [PATCH] 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 --- .../dotnet/DeepSpeechClient/DeepSpeech.cs | 34 ++----------------- .../dotnet/DeepSpeechClient/NativeImp.cs | 3 ++ native_client/javascript/deepspeech.i | 1 + native_client/javascript/index.js | 6 ++-- native_client/python/__init__.py | 4 +-- native_client/python/impl.i | 1 + 6 files changed, 12 insertions(+), 37 deletions(-) diff --git a/native_client/dotnet/DeepSpeechClient/DeepSpeech.cs b/native_client/dotnet/DeepSpeechClient/DeepSpeech.cs index 3340c9b3..a30bd4de 100644 --- a/native_client/dotnet/DeepSpeechClient/DeepSpeech.cs +++ b/native_client/dotnet/DeepSpeechClient/DeepSpeech.cs @@ -89,38 +89,9 @@ namespace DeepSpeechClient /// Native result code. private void EvaluateResultCode(ErrorCodes resultCode) { - switch (resultCode) + if (resultCode != ErrorCodes.DS_ERR_OK) { - case ErrorCodes.DS_ERR_OK: - 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."); + throw new ArgumentException(NativeImp.DS_ErrorCodeToErrorMessage((int)resultCode).PtrToString()); } } @@ -140,7 +111,6 @@ namespace DeepSpeechClient /// Thrown when cannot find the scorer file. public unsafe void EnableExternalScorer(string aScorerPath) { - string exceptionMessage = null; if (string.IsNullOrWhiteSpace(aScorerPath)) { throw new FileNotFoundException("Path to the scorer file cannot be empty."); diff --git a/native_client/dotnet/DeepSpeechClient/NativeImp.cs b/native_client/dotnet/DeepSpeechClient/NativeImp.cs index eabbfe48..bc77cf1b 100644 --- a/native_client/dotnet/DeepSpeechClient/NativeImp.cs +++ b/native_client/dotnet/DeepSpeechClient/NativeImp.cs @@ -19,6 +19,9 @@ namespace DeepSpeechClient internal unsafe static extern ErrorCodes DS_CreateModel(string aModelPath, ref IntPtr** pint); + [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)] + internal unsafe static extern IntPtr DS_ErrorCodeToErrorMessage(int aErrorCode); + [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)] internal unsafe static extern uint DS_GetModelBeamWidth(IntPtr** aCtx); diff --git a/native_client/javascript/deepspeech.i b/native_client/javascript/deepspeech.i index cb3968c2..e311a41b 100644 --- a/native_client/javascript/deepspeech.i +++ b/native_client/javascript/deepspeech.i @@ -37,6 +37,7 @@ using namespace node; %newobject DS_IntermediateDecode; %newobject DS_FinishStream; %newobject DS_Version; +%newobject DS_ErrorCodeToErrorMessage; // convert double pointer retval in CreateModel to an output %typemap(in, numinputs=0) ModelState **retval (ModelState *ret) { diff --git a/native_client/javascript/index.js b/native_client/javascript/index.js index 6ce06c0d..30bcb690 100644 --- a/native_client/javascript/index.js +++ b/native_client/javascript/index.js @@ -2,7 +2,7 @@ const binary = require('node-pre-gyp'); 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'))); // 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 impl = rets[1]; 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; @@ -139,7 +139,7 @@ Model.prototype.createStream = function() { const status = rets[0]; const ctx = rets[1]; 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; } diff --git a/native_client/python/__init__.py b/native_client/python/__init__.py index a44cf05f..a6af56f1 100644 --- a/native_client/python/__init__.py +++ b/native_client/python/__init__.py @@ -35,7 +35,7 @@ class Model(object): status, impl = deepspeech.impl.CreateModel(model_path) 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 def __del__(self): @@ -148,7 +148,7 @@ class Model(object): """ status, ctx = deepspeech.impl.CreateStream(self._impl) 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) diff --git a/native_client/python/impl.i b/native_client/python/impl.i index 259a5b5d..3ee4b516 100644 --- a/native_client/python/impl.i +++ b/native_client/python/impl.i @@ -121,6 +121,7 @@ static PyObject *parent_reference() { %newobject DS_IntermediateDecode; %newobject DS_FinishStream; %newobject DS_Version; +%newobject DS_ErrorCodeToErrorMessage; %rename ("%(strip:[DS_])s") "";