Merge pull request #2074 from mozilla/bindings-idiomatic
Some bindings clean-up
This commit is contained in:
commit
656ab5734a
@ -100,7 +100,7 @@ After following the above build and installation instructions, the Node.JS bindi
|
|||||||
|
|
||||||
```
|
```
|
||||||
cd native_client/javascript
|
cd native_client/javascript
|
||||||
make package
|
make build
|
||||||
make npm-pack
|
make npm-pack
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const Fs = require('fs');
|
const Fs = require('fs');
|
||||||
@ -69,6 +68,7 @@ function metadataToString(metadata) {
|
|||||||
for (var i = 0; i < metadata.num_items; ++i) {
|
for (var i = 0; i < metadata.num_items; ++i) {
|
||||||
retval += metadata.items[i].character;
|
retval += metadata.items[i].character;
|
||||||
}
|
}
|
||||||
|
Ds.FreeMetadata(metadata);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,5 +135,6 @@ audioStream.on('finish', () => {
|
|||||||
}
|
}
|
||||||
const inference_stop = process.hrtime(inference_start);
|
const inference_stop = process.hrtime(inference_start);
|
||||||
console.error('Inference took %ds for %ds audio file.', totalTime(inference_stop), audioLength.toPrecision(4));
|
console.error('Inference took %ds for %ds audio file.', totalTime(inference_stop), audioLength.toPrecision(4));
|
||||||
|
Ds.DestroyModel(model);
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
|
@ -28,7 +28,6 @@ using namespace node;
|
|||||||
|
|
||||||
// make sure the string returned by SpeechToText is freed
|
// make sure the string returned by SpeechToText is freed
|
||||||
%typemap(newfree) char* "DS_FreeString($1);";
|
%typemap(newfree) char* "DS_FreeString($1);";
|
||||||
%typemap(newfree) Metadata* "DS_FreeMetadata($1);";
|
|
||||||
|
|
||||||
%newobject DS_SpeechToText;
|
%newobject DS_SpeechToText;
|
||||||
%newobject DS_IntermediateDecode;
|
%newobject DS_IntermediateDecode;
|
||||||
@ -61,19 +60,8 @@ using namespace node;
|
|||||||
%append_output(SWIG_NewPointerObj(%as_voidptr(*$1), $*1_descriptor, 0));
|
%append_output(SWIG_NewPointerObj(%as_voidptr(*$1), $*1_descriptor, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// extend ModelState with a destructor so that DestroyModel will be called
|
|
||||||
// when the JavaScript object gets finalized.
|
|
||||||
%nodefaultctor ModelState;
|
%nodefaultctor ModelState;
|
||||||
%nodefaultdtor ModelState;
|
%nodefaultdtor ModelState;
|
||||||
|
|
||||||
struct ModelState {};
|
|
||||||
|
|
||||||
%extend ModelState {
|
|
||||||
~ModelState() {
|
|
||||||
DS_DestroyModel($self);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
%nodefaultdtor Metadata;
|
%nodefaultdtor Metadata;
|
||||||
%nodefaultctor Metadata;
|
%nodefaultctor Metadata;
|
||||||
%nodefaultctor MetadataItem;
|
%nodefaultctor MetadataItem;
|
||||||
@ -94,9 +82,6 @@ struct ModelState {};
|
|||||||
v8::Handle<v8::Value> result = SWIGV8_ARRAY_NEW();
|
v8::Handle<v8::Value> result = SWIGV8_ARRAY_NEW();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
~Metadata() {
|
|
||||||
DS_FreeMetadata($self);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
%rename ("%(strip:[DS_])s") "";
|
%rename ("%(strip:[DS_])s") "";
|
||||||
|
@ -75,7 +75,13 @@ Model.prototype.finishStreamWithMetadata = function() {
|
|||||||
return binding.FinishStreamWithMetadata.apply(null, arguments);
|
return binding.FinishStreamWithMetadata.apply(null, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function DestroyModel(model) {
|
||||||
|
return binding.DestroyModel(model._impl);
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
Model: Model,
|
Model: Model,
|
||||||
printVersions: binding.PrintVersions
|
printVersions: binding.PrintVersions,
|
||||||
|
DestroyModel: DestroyModel,
|
||||||
|
FreeMetadata: binding.FreeMetadata
|
||||||
};
|
};
|
||||||
|
@ -39,6 +39,7 @@ N_FEATURES = 26
|
|||||||
# Size of the context window used for producing timesteps in the input vector
|
# Size of the context window used for producing timesteps in the input vector
|
||||||
N_CONTEXT = 9
|
N_CONTEXT = 9
|
||||||
|
|
||||||
|
|
||||||
def convert_samplerate(audio_path):
|
def convert_samplerate(audio_path):
|
||||||
sox_cmd = 'sox {} --type raw --bits 16 --channels 1 --rate 16000 --encoding signed-integer --endian little --compression 0.0 --no-dither - '.format(quote(audio_path))
|
sox_cmd = 'sox {} --type raw --bits 16 --channels 1 --rate 16000 --encoding signed-integer --endian little --compression 0.0 --no-dither - '.format(quote(audio_path))
|
||||||
try:
|
try:
|
||||||
@ -50,11 +51,9 @@ def convert_samplerate(audio_path):
|
|||||||
|
|
||||||
return 16000, np.frombuffer(output, np.int16)
|
return 16000, np.frombuffer(output, np.int16)
|
||||||
|
|
||||||
|
|
||||||
def metadata_to_string(metadata):
|
def metadata_to_string(metadata):
|
||||||
retval = ''
|
return ''.join(item.character for item in metadata.items)
|
||||||
for item in range(metadata.num_items):
|
|
||||||
retval += metadata.items[item].character
|
|
||||||
return retval
|
|
||||||
|
|
||||||
|
|
||||||
class VersionAction(argparse.Action):
|
class VersionAction(argparse.Action):
|
||||||
@ -65,6 +64,7 @@ class VersionAction(argparse.Action):
|
|||||||
printVersions()
|
printVersions()
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description='Running DeepSpeech inference.')
|
parser = argparse.ArgumentParser(description='Running DeepSpeech inference.')
|
||||||
parser.add_argument('--model', required=True,
|
parser.add_argument('--model', required=True,
|
||||||
|
@ -33,17 +33,19 @@ import_array();
|
|||||||
%append_output(SWIG_NewPointerObj(%as_voidptr(*$1), $*1_descriptor, 0));
|
%append_output(SWIG_NewPointerObj(%as_voidptr(*$1), $*1_descriptor, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
%extend struct MetadataItem {
|
|
||||||
MetadataItem* __getitem__(size_t i) {
|
|
||||||
return &$self[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
%typemap(out) Metadata* {
|
%typemap(out) Metadata* {
|
||||||
// owned, extended destructor needs to be called by SWIG
|
// owned, extended destructor needs to be called by SWIG
|
||||||
%append_output(SWIG_NewPointerObj(%as_voidptr($1), $1_descriptor, SWIG_POINTER_OWN));
|
%append_output(SWIG_NewPointerObj(%as_voidptr($1), $1_descriptor, SWIG_POINTER_OWN));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
%typemap(out) MetadataItem* %{
|
||||||
|
$result = PyList_New(arg1->num_items);
|
||||||
|
for (int i = 0; i < arg1->num_items; ++i) {
|
||||||
|
PyObject* o = SWIG_NewPointerObj(SWIG_as_voidptr(&arg1->items[i]), SWIGTYPE_p_MetadataItem, 0);
|
||||||
|
PyList_SetItem($result, i, o);
|
||||||
|
}
|
||||||
|
%}
|
||||||
|
|
||||||
%extend struct Metadata {
|
%extend struct Metadata {
|
||||||
~Metadata() {
|
~Metadata() {
|
||||||
DS_FreeMetadata($self);
|
DS_FreeMetadata($self);
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
[build_ext]
|
[build_ext]
|
||||||
include-dirs=./
|
include-dirs=./
|
||||||
swig-opts=-c++ -keyword
|
|
||||||
build-lib=temp_build
|
build-lib=temp_build
|
||||||
build-temp=temp_build
|
build-temp=temp_build
|
||||||
|
|
||||||
|
@ -7,93 +7,101 @@ import os
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
try:
|
def main():
|
||||||
import numpy
|
|
||||||
try:
|
try:
|
||||||
numpy_include = numpy.get_include()
|
import numpy
|
||||||
except AttributeError:
|
try:
|
||||||
numpy_include = numpy.get_numpy_include()
|
numpy_include = numpy.get_include()
|
||||||
except ImportError:
|
except AttributeError:
|
||||||
numpy_include = ''
|
numpy_include = numpy.get_numpy_include()
|
||||||
assert 'NUMPY_INCLUDE' in os.environ
|
except ImportError:
|
||||||
|
numpy_include = ''
|
||||||
|
assert 'NUMPY_INCLUDE' in os.environ
|
||||||
|
|
||||||
def read(fname):
|
def read(fname):
|
||||||
return open(os.path.join(os.path.dirname(__file__), fname)).read()
|
return open(os.path.join(os.path.dirname(__file__), fname)).read()
|
||||||
|
|
||||||
numpy_include = os.getenv('NUMPY_INCLUDE', numpy_include)
|
numpy_include = os.getenv('NUMPY_INCLUDE', numpy_include)
|
||||||
numpy_min_ver = os.getenv('NUMPY_DEP_VERSION', '')
|
numpy_min_ver = os.getenv('NUMPY_DEP_VERSION', '')
|
||||||
|
|
||||||
project_name = 'deepspeech'
|
project_name = 'deepspeech'
|
||||||
if '--project_name' in sys.argv:
|
if '--project_name' in sys.argv:
|
||||||
project_name_idx = sys.argv.index('--project_name')
|
project_name_idx = sys.argv.index('--project_name')
|
||||||
project_name = sys.argv[project_name_idx + 1]
|
project_name = sys.argv[project_name_idx + 1]
|
||||||
sys.argv.remove('--project_name')
|
sys.argv.remove('--project_name')
|
||||||
sys.argv.pop(project_name_idx)
|
sys.argv.pop(project_name_idx)
|
||||||
|
|
||||||
with open('../../VERSION', 'r') as ver:
|
with open('../../VERSION', 'r') as ver:
|
||||||
project_version = ver.read().strip()
|
project_version = ver.read().strip()
|
||||||
|
|
||||||
class BuildExtFirst(build):
|
class BuildExtFirst(build):
|
||||||
sub_commands = [('build_ext', build.has_ext_modules),
|
sub_commands = [('build_ext', build.has_ext_modules),
|
||||||
('build_py', build.has_pure_modules),
|
('build_py', build.has_pure_modules),
|
||||||
('build_clib', build.has_c_libraries),
|
('build_clib', build.has_c_libraries),
|
||||||
('build_scripts', build.has_scripts)]
|
('build_scripts', build.has_scripts)]
|
||||||
|
|
||||||
# Properly pass arguments for linking, setuptools will perform some checks
|
# Properly pass arguments for linking, setuptools will perform some checks
|
||||||
def lib_dirs_split(a):
|
def lib_dirs_split(a):
|
||||||
if os.name == 'posix':
|
if os.name == 'posix':
|
||||||
return a.split('-L')[1:]
|
return a.split('-L')[1:]
|
||||||
|
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def libs_split(a):
|
raise AssertionError('os.name == java not expected')
|
||||||
if os.name == 'posix':
|
|
||||||
return a.split('-l')[1:]
|
|
||||||
|
|
||||||
if os.name == 'nt':
|
def libs_split(a):
|
||||||
return a.split('.lib')[0:1]
|
if os.name == 'posix':
|
||||||
|
return a.split('-l')[1:]
|
||||||
|
|
||||||
ds_ext = Extension('deepspeech._impl',
|
if os.name == 'nt':
|
||||||
['impl.i'],
|
return a.split('.lib')[0:1]
|
||||||
include_dirs = [ numpy_include, '../' ],
|
|
||||||
library_dirs = list(map(lambda x: x.strip(), lib_dirs_split(os.getenv('MODEL_LDFLAGS', '')))),
|
|
||||||
libraries = list(map(lambda x: x.strip(), libs_split(os.getenv('MODEL_LIBS', ''))))
|
|
||||||
)
|
|
||||||
|
|
||||||
setup(name = project_name,
|
raise AssertionError('os.name == java not expected')
|
||||||
description = 'A library for running inference on a DeepSpeech model',
|
|
||||||
long_description = read('../../README.md'),
|
ds_ext = Extension(name='deepspeech._impl',
|
||||||
long_description_content_type = 'text/markdown; charset=UTF-8',
|
sources=['impl.i'],
|
||||||
author = 'Mozilla',
|
include_dirs=[numpy_include, '../'],
|
||||||
version = project_version,
|
library_dirs=list(map(lambda x: x.strip(), lib_dirs_split(os.getenv('MODEL_LDFLAGS', '')))),
|
||||||
package_dir = {'deepspeech': '.'},
|
libraries=list(map(lambda x: x.strip(), libs_split(os.getenv('MODEL_LIBS', '')))),
|
||||||
cmdclass = {'build': BuildExtFirst},
|
swig_opts=['-c++', '-keyword', '-builtin'])
|
||||||
license = 'MPL-2.0',
|
|
||||||
url = 'https://github.com/mozilla/DeepSpeech',
|
setup(name=project_name,
|
||||||
project_urls = {
|
description='A library for running inference on a DeepSpeech model',
|
||||||
'Documentation': 'https://github.com/mozilla/DeepSpeech/tree/v{}#project-deepspeech'.format(project_version),
|
long_description=read('../../README.md'),
|
||||||
'Tracker': 'https://github.com/mozilla/DeepSpeech/issues',
|
long_description_content_type='text/markdown; charset=UTF-8',
|
||||||
'Repository': 'https://github.com/mozilla/DeepSpeech/tree/v{}'.format(project_version),
|
author='Mozilla',
|
||||||
'Discussions': 'https://discourse.mozilla.org/c/deep-speech',
|
version=project_version,
|
||||||
},
|
package_dir={'deepspeech': '.'},
|
||||||
ext_modules = [ds_ext],
|
cmdclass={'build': BuildExtFirst},
|
||||||
py_modules = ['deepspeech', 'deepspeech.client', 'deepspeech.impl'],
|
license='MPL-2.0',
|
||||||
entry_points={'console_scripts':['deepspeech = deepspeech.client:main']},
|
url='https://github.com/mozilla/DeepSpeech',
|
||||||
install_requires = ['numpy%s' % numpy_min_ver],
|
project_urls={
|
||||||
include_package_data = True,
|
'Documentation': 'https://github.com/mozilla/DeepSpeech/tree/v{}#project-deepspeech'.format(project_version),
|
||||||
classifiers = [
|
'Tracker': 'https://github.com/mozilla/DeepSpeech/issues',
|
||||||
'Development Status :: 3 - Alpha',
|
'Repository': 'https://github.com/mozilla/DeepSpeech/tree/v{}'.format(project_version),
|
||||||
'Environment :: Console',
|
'Discussions': 'https://discourse.mozilla.org/c/deep-speech',
|
||||||
'Intended Audience :: Developers',
|
},
|
||||||
'Intended Audience :: Science/Research',
|
ext_modules=[ds_ext],
|
||||||
'License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)',
|
py_modules=['deepspeech', 'deepspeech.client', 'deepspeech.impl'],
|
||||||
'Programming Language :: Python :: 2.7',
|
entry_points={'console_scripts':['deepspeech=deepspeech.client:main']},
|
||||||
'Programming Language :: Python :: 3.4',
|
install_requires=['numpy%s' % numpy_min_ver],
|
||||||
'Programming Language :: Python :: 3.5',
|
include_package_data=True,
|
||||||
'Programming Language :: Python :: 3.6',
|
classifiers=[
|
||||||
'Topic :: Multimedia :: Sound/Audio :: Speech',
|
'Development Status :: 3 - Alpha',
|
||||||
'Topic :: Scientific/Engineering :: Human Machine Interfaces',
|
'Environment :: Console',
|
||||||
'Topic :: Scientific/Engineering',
|
'Intended Audience :: Developers',
|
||||||
'Topic :: Utilities',
|
'Intended Audience :: Science/Research',
|
||||||
])
|
'License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)',
|
||||||
|
'Programming Language :: Python :: 2.7',
|
||||||
|
'Programming Language :: Python :: 3.4',
|
||||||
|
'Programming Language :: Python :: 3.5',
|
||||||
|
'Programming Language :: Python :: 3.6',
|
||||||
|
'Topic :: Multimedia :: Sound/Audio :: Speech',
|
||||||
|
'Topic :: Scientific/Engineering :: Human Machine Interfaces',
|
||||||
|
'Topic :: Scientific/Engineering',
|
||||||
|
'Topic :: Utilities',
|
||||||
|
])
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user