@qvac/translation-nmtcpp 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +470 -0
  3. package/binding.js +1 -0
  4. package/index.d.ts +82 -0
  5. package/index.js +188 -0
  6. package/lib/error.js +65 -0
  7. package/marian.js +186 -0
  8. package/package.json +69 -0
  9. package/prebuilds/android-arm/qvac__translation-nmtcpp.bare +0 -0
  10. package/prebuilds/android-arm64/qvac__translation-nmtcpp.bare +0 -0
  11. package/prebuilds/android-ia32/qvac__translation-nmtcpp.bare +0 -0
  12. package/prebuilds/android-x64/qvac__translation-nmtcpp.bare +0 -0
  13. package/prebuilds/darwin-arm64/qvac__translation-nmtcpp.bare +0 -0
  14. package/prebuilds/darwin-arm64/qvac__translation-nmtcpp.bare.exports +3622 -0
  15. package/prebuilds/darwin-x64/qvac__translation-nmtcpp.bare +0 -0
  16. package/prebuilds/darwin-x64/qvac__translation-nmtcpp.bare.exports +3731 -0
  17. package/prebuilds/ios-arm64/qvac__translation-nmtcpp.bare +0 -0
  18. package/prebuilds/ios-arm64/qvac__translation-nmtcpp.bare.exports +3603 -0
  19. package/prebuilds/ios-arm64-simulator/qvac__translation-nmtcpp.bare +0 -0
  20. package/prebuilds/ios-arm64-simulator/qvac__translation-nmtcpp.bare.exports +3603 -0
  21. package/prebuilds/ios-x64-simulator/qvac__translation-nmtcpp.bare +0 -0
  22. package/prebuilds/ios-x64-simulator/qvac__translation-nmtcpp.bare.exports +3720 -0
  23. package/prebuilds/linux-x64/qvac__translation-nmtcpp.bare +0 -0
  24. package/prebuilds/win32-x64/qvac__translation-nmtcpp.bare +0 -0
  25. package/prebuilds/win32-x64/qvac__translation-nmtcpp.bare.exports +0 -0
  26. package/third-party/indic-processor-deps/indicnlp/INDIC_NLP_LICENCE +9 -0
  27. package/third-party/indic-processor-deps/indicnlp/index.js +11 -0
  28. package/third-party/indic-processor-deps/indicnlp/indic_detokenize.js +141 -0
  29. package/third-party/indic-processor-deps/indicnlp/indic_normalize.js +1213 -0
  30. package/third-party/indic-processor-deps/indicnlp/indic_tokenize.js +123 -0
  31. package/third-party/indic-processor-deps/indicnlp/langinfo.js +609 -0
  32. package/third-party/indic-processor-deps/indicnlp/sinhala_transliterator.js +197 -0
  33. package/third-party/indic-processor-deps/indicnlp/unicode_transliterator.js +120 -0
  34. package/third-party/indic-processor-deps/sacremoses/SACREMOSES_LICENCE +21 -0
  35. package/third-party/indic-processor-deps/sacremoses/cjk.js +202 -0
  36. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/README.txt +8 -0
  37. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.as +65 -0
  38. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.bn +65 -0
  39. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.ca +75 -0
  40. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.cs +390 -0
  41. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.de +325 -0
  42. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.el +1568 -0
  43. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.en +123 -0
  44. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.es +118 -0
  45. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.et +138 -0
  46. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.fi +138 -0
  47. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.fr +153 -0
  48. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.ga +48 -0
  49. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.gu +105 -0
  50. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.hi +113 -0
  51. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.hu +103 -0
  52. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.is +251 -0
  53. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.it +180 -0
  54. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.kn +70 -0
  55. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.lt +698 -0
  56. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.lv +100 -0
  57. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.ml +67 -0
  58. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.mni +65 -0
  59. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.mr +113 -0
  60. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.nl +115 -0
  61. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.or +101 -0
  62. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.pa +102 -0
  63. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.pl +283 -0
  64. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.pt +210 -0
  65. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.ro +38 -0
  66. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.ru +293 -0
  67. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.sk +474 -0
  68. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.sl +78 -0
  69. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.sv +97 -0
  70. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.ta +71 -0
  71. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.tdt +210 -0
  72. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.te +70 -0
  73. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.yue +53 -0
  74. package/third-party/indic-processor-deps/sacremoses/data/nonbreaking_prefixes/nonbreaking_prefix.zh +53 -0
  75. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/CJK.txt +23246 -0
  76. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/CJKSymbols.txt +1 -0
  77. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Close_Punctuation.txt +1 -0
  78. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Currency_Symbol.txt +1 -0
  79. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Han.txt +1 -0
  80. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Hangul.txt +1 -0
  81. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Hangul_Syllables.txt +1 -0
  82. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Hiragana.txt +1 -0
  83. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/IsAlnum-unichars-au.txt +1 -0
  84. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/IsAlnum.txt +1 -0
  85. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/IsAlpha-unichars-au.txt +1 -0
  86. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/IsAlpha.txt +1 -0
  87. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/IsLower.txt +1 -0
  88. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/IsN.txt +1 -0
  89. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/IsPf.txt +1 -0
  90. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/IsPi.txt +1 -0
  91. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/IsSc.txt +1 -0
  92. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/IsSo.txt +1 -0
  93. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/IsUpper.txt +1 -0
  94. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Katakana.txt +1 -0
  95. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Line_Separator.txt +1 -0
  96. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Lowercase_Letter.txt +1 -0
  97. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Number.txt +1 -0
  98. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Open_Punctuation.txt +1 -0
  99. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Punctuation.txt +1 -0
  100. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Separator.txt +1 -0
  101. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Symbol.txt +1 -0
  102. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Titlecase_Letter.txt +1 -0
  103. package/third-party/indic-processor-deps/sacremoses/data/perluniprops/Uppercase_Letter.txt +1 -0
  104. package/third-party/indic-processor-deps/sacremoses/index.js +8 -0
  105. package/third-party/indic-processor-deps/sacremoses/indic.js +76 -0
  106. package/third-party/indic-processor-deps/sacremoses/normalizer.js +264 -0
  107. package/third-party/indic-processor-deps/sacremoses/pernuliprops.js +287 -0
  108. package/third-party/indic-processor-deps/sacremoses/tokenizer.js +1217 -0
  109. package/third-party/indic-processor.js +565 -0
package/index.d.ts ADDED
@@ -0,0 +1,82 @@
1
+ import QvacResponse from "@qvac/response"
2
+ import BaseInference, {
3
+ BaseInferenceArgs,
4
+ ReportProgressCallback,
5
+ } from "@qvac/infer-base/WeightsProvider/BaseInference"
6
+ import WeightsProvider from "@qvac/infer-base/WeightsProvider/WeightsProvider"
7
+
8
+ declare interface Loader {
9
+ getStream: (path: string) => Promise<ReadableStream>
10
+ getFileSize?: (path: string) => Promise<number>
11
+ download?: (path: string, options?: {diskPath: string, progressCallback?: ReportProgressCallback}) => Promise<void>
12
+ deleteLocal?: () => Promise<void>
13
+ }
14
+
15
+ declare interface TranslationNmtcppParams {
16
+ dstLang: string
17
+ srcLang: string
18
+ [args: string]: unknown
19
+ }
20
+
21
+ declare interface TranslationNmtcppArgs extends BaseInferenceArgs {
22
+ loader: Loader
23
+ params: TranslationNmtcppParams
24
+ diskPath: string
25
+ modelName: string
26
+ [args: string]: unknown
27
+ }
28
+
29
+ declare interface TranslationNmtcppModelTypes {
30
+ readonly IndicTrans: "IndicTrans"
31
+ readonly Opus: "Opus"
32
+ }
33
+
34
+ declare interface TranslationNmtcppConfig {
35
+ modelType: TranslationNmtcppModelTypes[keyof TranslationNmtcppModelTypes];
36
+ [args: string]: unknown
37
+ }
38
+
39
+ /**
40
+ * GGML client implementation for Marian/IndicTrans translation model
41
+ */
42
+ declare class TranslationNmtcpp extends BaseInference {
43
+ protected _config: TranslationNmtcppConfig
44
+ protected _diskPath: string
45
+ protected _modelName: string
46
+ protected weightsProvider: WeightsProvider
47
+ static readonly ModelTypes: TranslationNmtcppModelTypes
48
+
49
+ modelType: TranslationNmtcppModelTypes[keyof TranslationNmtcppModelTypes]
50
+
51
+ /**
52
+ * Creates an instance of TranslationNmtcpp.
53
+ * @constructor
54
+ * @param {TranslationNmtcppArgs} args arguments for inference setup
55
+ * @param {TranslationNmtcppConfig} config - environment specific inference setup configuration
56
+ */
57
+ constructor(args: TranslationNmtcppArgs, config: TranslationNmtcppConfig)
58
+
59
+ /**
60
+ * Loads the TranslationNmtcpp model files.
61
+ * @param {boolean} [close=false] - Optional boolean to close the loader after it finish (default: false).
62
+ * @param {ReportProgressCallback} [reportProgressCallback] - Optional callback function for reporting progress.
63
+ * @returns {Promise<void>} - A promise that resolves when the model is fully loaded.
64
+ */
65
+ load(
66
+ close?: boolean,
67
+ reportProgressCallback?: ReportProgressCallback
68
+ ): Promise<void>
69
+
70
+ /**
71
+ * Runs the process of inference output for a given input
72
+ * @param {string} input - The input text to translate
73
+ * @returns {Promise<QvacResponse>} - A promise that resolves with the response object
74
+ */
75
+ run(input: string): Promise<QvacResponse>
76
+ }
77
+
78
+ declare namespace TranslationNmtcpp {
79
+ export { TranslationNmtcppArgs, TranslationNmtcppConfig }
80
+ }
81
+
82
+ export = TranslationNmtcpp;
package/index.js ADDED
@@ -0,0 +1,188 @@
1
+ 'use strict'
2
+
3
+ const path = require('bare-path')
4
+ const BaseInference = require('@qvac/infer-base/WeightsProvider/BaseInference')
5
+ const WeightsProvider = require('@qvac/infer-base/WeightsProvider/WeightsProvider')
6
+
7
+ const { TranslationInterface } = require('./marian')
8
+ const QvacResponse = require('@qvac/response')
9
+ const { IndicProcessor } = require('./third-party/indic-processor')
10
+
11
+ const END_OF_INPUT = 'end of job'
12
+
13
+ class QvacIndicTransResponse extends QvacResponse {
14
+ /**
15
+ * Creates an instance of QvacIndicTransResponse.
16
+ * @constructor
17
+ * @param {IndicProcessor} processor
18
+ * @param {string} dstLang
19
+ * @param {Object} handlers
20
+ */
21
+ constructor (processor, dstLang, handlers) {
22
+ super(handlers)
23
+ this.processor = processor
24
+ this.dstLang = dstLang
25
+ }
26
+
27
+ onCancel (callback) {
28
+ return super.onCancel(callback)
29
+ }
30
+
31
+ onError (callback) {
32
+ return super.onError(callback)
33
+ }
34
+
35
+ onFinish (callback) {
36
+ return super.onFinish(callback)
37
+ }
38
+
39
+ onUpdate (callback) {
40
+ return super.onUpdate((data) => {
41
+ const [postProcessedText] = this.processor.postprocessBatch(
42
+ [data],
43
+ this.dstLang
44
+ )
45
+ return callback(postProcessedText)
46
+ })
47
+ }
48
+
49
+ async * iterate () {
50
+ for await (const output of super.iterate()) {
51
+ const [postProcessedText] = this.processor.postprocessBatch(
52
+ [output],
53
+ this.dstLang
54
+ )
55
+ yield postProcessedText
56
+ }
57
+ }
58
+ }
59
+
60
+ /**
61
+ * TranslationNmtcpp implementation for Marian/IndicTrans translation model
62
+ */
63
+ class TranslationNmtcpp extends BaseInference {
64
+ /**
65
+ * Available model types for translation
66
+ * @static
67
+ * @type {Object}
68
+ * @property {string} IndicTrans
69
+ * @property {string} Opus
70
+ */
71
+ static ModelTypes = {
72
+ IndicTrans: 'IndicTrans',
73
+ Opus: 'Opus'
74
+ }
75
+
76
+ /**
77
+ * Creates an instance of MLCInference.
78
+ * @constructor
79
+ *
80
+ * @param {Object} args arguments for inference setup
81
+ * @param {Object} config - environment specific inference setup configuration
82
+ */
83
+ constructor ({ loader, diskPath, modelName, params, logger = null, exclusiveRun = true, ...args }, config = {}) {
84
+ super({ logger, exclusiveRun, ...args })
85
+ this.loader = loader
86
+ this.weightsProvider = new WeightsProvider(loader, this.logger)
87
+ const { modelType, ...additionalConfig } = config
88
+ this._modelType = modelType
89
+ this._config = additionalConfig
90
+ this._diskPath = diskPath
91
+ this._modelName = modelName
92
+ this._params = params
93
+ }
94
+
95
+ async _load (close = false, reportProgressCallback) {
96
+ await this.loader.ready()
97
+ try {
98
+ await this.downloadWeights(reportProgressCallback)
99
+
100
+ const configurationParams = {
101
+ path: this._config.path || path.join(this._diskPath, this._modelName),
102
+ config: this._config
103
+ }
104
+ this.addon = this.createAddon(configurationParams)
105
+ await this.addon.activate()
106
+ this.state.configLoaded = true
107
+ } finally {
108
+ if (close) {
109
+ await this.loader.close()
110
+ }
111
+ }
112
+ }
113
+
114
+ async _runInternal (input) {
115
+ if (this._modelType === TranslationNmtcpp.ModelTypes.IndicTrans) {
116
+ const processor = new IndicProcessor()
117
+ const [processedText] = processor.preprocessBatch(
118
+ [input],
119
+ this._params.srcLang,
120
+ this._params.dstLang
121
+ )
122
+ const jobId = await this.addon.append({
123
+ type: 'text',
124
+ input: processedText
125
+ })
126
+ const response = new QvacIndicTransResponse(
127
+ processor,
128
+ this._params.dstLang,
129
+ {
130
+ cancelHandler: () => {
131
+ return this.addon.cancel(jobId)
132
+ },
133
+ pauseHandler: () => {
134
+ return this.addon.pause()
135
+ },
136
+ continueHandler: () => {
137
+ return this.addon.activate()
138
+ }
139
+ }
140
+ )
141
+
142
+ this._saveJobToResponseMapping(jobId, response)
143
+ await this.addon.append({ type: END_OF_INPUT })
144
+ return response
145
+ }
146
+
147
+ const jobId = await this.addon.append({ type: 'text', input })
148
+
149
+ const response = new QvacResponse({
150
+ cancelHandler: () => {
151
+ return this.addon.cancel(jobId)
152
+ },
153
+ pauseHandler: () => {
154
+ return this.addon.pause()
155
+ },
156
+ continueHandler: () => {
157
+ return this.addon.activate()
158
+ }
159
+ })
160
+
161
+ this._saveJobToResponseMapping(jobId, response)
162
+ await this.addon.append({ type: END_OF_INPUT })
163
+ return response
164
+ }
165
+
166
+ createAddon (configurationParams) {
167
+ return new TranslationInterface(
168
+ configurationParams,
169
+ this._outputCallback.bind(this),
170
+ this.logger
171
+ )
172
+ }
173
+
174
+ async _downloadWeights (reportProgressCallback) {
175
+ const models = [this._modelName]
176
+
177
+ this.logger.info('Loading weight files:', models)
178
+
179
+ const result = await this.weightsProvider.downloadFiles(models, this._diskPath, {
180
+ closeLoader: true,
181
+ onDownloadProgress: reportProgressCallback
182
+ })
183
+ this.logger.info('Weight files downloaded successfully', { models })
184
+ return result
185
+ }
186
+ }
187
+
188
+ module.exports = TranslationNmtcpp
package/lib/error.js ADDED
@@ -0,0 +1,65 @@
1
+ 'use strict'
2
+
3
+ const { QvacErrorBase, addCodes } = require('@qvac/error')
4
+ const { name, version } = require('../package.json')
5
+
6
+ class QvacErrorAddonMarian extends QvacErrorBase { }
7
+
8
+ // This library has error code range from 8001 to 9000
9
+ const ERR_CODES = Object.freeze({
10
+ FAILED_TO_LOAD_WEIGHTS: 8001,
11
+ FAILED_TO_CANCEL: 8002,
12
+ FAILED_TO_APPEND: 8003,
13
+ FAILED_TO_GET_STATUS: 8004,
14
+ FAILED_TO_DESTROY: 8005,
15
+ FAILED_TO_ACTIVATE: 8006,
16
+ FAILED_TO_RESET: 8007,
17
+ FAILED_TO_PAUSE: 8008
18
+ })
19
+
20
+ addCodes(
21
+ {
22
+ [ERR_CODES.FAILED_TO_LOAD_WEIGHTS]: {
23
+ name: 'FAILED_TO_LOAD_WEIGHTS',
24
+ message: message => `Failed to load weights, error: ${message}`
25
+ },
26
+ [ERR_CODES.FAILED_TO_CANCEL]: {
27
+ name: 'FAILED_TO_CANCEL',
28
+ message: message => `Failed to cancel inference, error: ${message}`
29
+ },
30
+ [ERR_CODES.FAILED_TO_APPEND]: {
31
+ name: 'FAILED_TO_APPEND',
32
+ message: message =>
33
+ `Failed to append data to processing queue, error: ${message}`
34
+ },
35
+ [ERR_CODES.FAILED_TO_GET_STATUS]: {
36
+ name: 'FAILED_TO_GET_STATUS',
37
+ message: message => `Failed to get addon status, error: ${message}`
38
+ },
39
+ [ERR_CODES.FAILED_TO_DESTROY]: {
40
+ name: 'FAILED_TO_DESTROY',
41
+ message: message => `Failed to destroy instance, error: ${message}`
42
+ },
43
+ [ERR_CODES.FAILED_TO_ACTIVATE]: {
44
+ name: 'FAILED_TO_ACTIVATE',
45
+ message: message => `Failed to activate model, error: ${message}`
46
+ },
47
+ [ERR_CODES.FAILED_TO_RESET]: {
48
+ name: 'FAILED_TO_RESET',
49
+ message: message => `Failed to reset model state, error: ${message}`
50
+ },
51
+ [ERR_CODES.FAILED_TO_PAUSE]: {
52
+ name: 'FAILED_TO_PAUSE',
53
+ message: message => `Failed to pause inference, error: ${message}`
54
+ }
55
+ },
56
+ {
57
+ name,
58
+ version
59
+ }
60
+ )
61
+
62
+ module.exports = {
63
+ ERR_CODES,
64
+ QvacErrorAddonMarian
65
+ }
package/marian.js ADDED
@@ -0,0 +1,186 @@
1
+ const binding = require('./binding')
2
+ const { QvacErrorAddonMarian, ERR_CODES } = require('./lib/error')
3
+
4
+ /**
5
+ * An interface between Bare addon in C++ and JS runtime.
6
+ */
7
+ class TranslationInterface {
8
+ /**
9
+ *
10
+ * @param {Object} configurationParams - all the required configuration for inference setup
11
+ * @param {Function} outputCb - to be called on any inference event ( started, new output, error, etc )
12
+ * @param {Function} transitionCb - to be called on addon state changes (LISTENING, IDLE, STOPPED, etc )
13
+ */
14
+ constructor (configurationParams, outputCb, transitionCb = null) {
15
+ this._handle = binding.createInstance(
16
+ this,
17
+ configurationParams,
18
+ outputCb,
19
+ transitionCb
20
+ )
21
+ }
22
+
23
+ /**
24
+ * Stops the current process execution,
25
+ * frees memory allocated for configuration and weights,
26
+ * and moves addon to the UNLOADED state.
27
+ */
28
+ async unload () {
29
+ binding.unload(this._handle)
30
+ }
31
+
32
+ /**
33
+ * Moves addon the the LOADING state and loads configuration for the model.
34
+ * Can only be invoked after unload()
35
+ * @param {Object} configurationParams - all the required configuration for inference setup
36
+ */
37
+ async load (configurationParams) {
38
+ binding.load(this._handle, configurationParams)
39
+ }
40
+
41
+ /**
42
+ * Stops the current process execution,
43
+ * frees memory allocated for configuration and weights,
44
+ * loads the new configuration,
45
+ * and moves addon to the LOADING state.
46
+ * @param {Object} configurationParams - all the required configuration for inference setup
47
+ */
48
+ async reload (configurationParams) {
49
+ binding.reload(this._handle, configurationParams)
50
+ }
51
+
52
+ /**
53
+ * Loads weights for the model.
54
+ * Can only be invoked after instance is constructed or after load()/reload() are called
55
+ * @param {Object} weightsData
56
+ * @param {String} weightsData.filename
57
+ * @param {Uint8Array} weightsData.contents
58
+ * @param {Boolean} weightsData.completed
59
+ */
60
+ async loadWeights (weightsData) {
61
+ try {
62
+ binding.loadWeights(this._handle, weightsData)
63
+ } catch (err) {
64
+ throw new QvacErrorAddonMarian({
65
+ code: ERR_CODES.FAILED_TO_LOAD_WEIGHTS,
66
+ adds: err.message,
67
+ cause: err
68
+ })
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Unloads weights for the model.
74
+ * Can only be invoked after instance has loaded weights
75
+ */
76
+ async unloadWeights () {
77
+ binding.unloadWeights(this._handle)
78
+ }
79
+
80
+ /**
81
+ * Moves addon to the LISTENING state after all the initialization is done
82
+ */
83
+ async activate () {
84
+ try {
85
+ binding.activate(this._handle)
86
+ } catch (err) {
87
+ throw new QvacErrorAddonMarian({
88
+ code: ERR_CODES.FAILED_TO_ACTIVATE,
89
+ adds: err.message,
90
+ cause: err
91
+ })
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Pauses current inference process
97
+ */
98
+ async pause () {
99
+ try {
100
+ binding.pause(this._handle)
101
+ } catch (err) {
102
+ throw new QvacErrorAddonMarian({
103
+ code: ERR_CODES.FAILED_TO_PAUSE,
104
+ adds: err.message,
105
+ cause: err
106
+ })
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Pauses current inference process
112
+ */
113
+ async stop () {
114
+ binding.stop(this._handle)
115
+ }
116
+
117
+ /**
118
+ * Cancel a inference process by jobId, if no jobId is provided it cancel the whole queue
119
+ */
120
+ async cancel (jobId) {
121
+ try {
122
+ binding.cancel(this._handle, jobId)
123
+ } catch (err) {
124
+ throw new QvacErrorAddonMarian({
125
+ code: ERR_CODES.FAILED_TO_CANCEL,
126
+ adds: err.message,
127
+ cause: err
128
+ })
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Adds new input to the processing queue
134
+ * @param {Object} data
135
+ * @param {String} data.type
136
+ * @param {String} data.input
137
+ * @returns {Number} - job ID
138
+ */
139
+ async append (data) {
140
+ try {
141
+ return binding.append(this._handle, data)
142
+ } catch (err) {
143
+ throw new QvacErrorAddonMarian({
144
+ code: ERR_CODES.FAILED_TO_APPEND,
145
+ adds: err.message,
146
+ cause: err
147
+ })
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Addon process status
153
+ * @returns {String}
154
+ */
155
+ async status () {
156
+ try {
157
+ return binding.status(this._handle)
158
+ } catch (err) {
159
+ throw new QvacErrorAddonMarian({
160
+ code: ERR_CODES.FAILED_TO_GET_STATUS,
161
+ adds: err.message,
162
+ cause: err
163
+ })
164
+ }
165
+ }
166
+
167
+ /**
168
+ * Stops addon process and clears resources (including memory).
169
+ */
170
+ async destroy () {
171
+ try {
172
+ binding.destroyInstance(this._handle)
173
+ this._handle = null
174
+ } catch (err) {
175
+ throw new QvacErrorAddonMarian({
176
+ code: ERR_CODES.FAILED_TO_DESTROY,
177
+ adds: err.message,
178
+ cause: err
179
+ })
180
+ }
181
+ }
182
+ }
183
+
184
+ module.exports = {
185
+ TranslationInterface
186
+ }
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@qvac/translation-nmtcpp",
3
+ "version": "0.1.0",
4
+ "description": "translation addon for qvac",
5
+ "addon": true,
6
+ "engines": {
7
+ "bare": ">=1.19.0"
8
+ },
9
+ "scripts": {
10
+ "build": "bare-make generate && bare-make build && bare-make install",
11
+ "lint": "standard",
12
+ "lint:fix": "standard --fix",
13
+ "test:unit:generate": "brittle -r test/unit/all.js test/unit/*.test.js",
14
+ "test:unit": "npm run test:unit:generate && bare test/unit/all.js",
15
+ "test:integration:generate": "brittle -r test/integration/all.js test/integration/*.test.js",
16
+ "test:integration": "npm run test:integration:generate && bare test/integration/all.js",
17
+ "test": "npm run lint && npm run test:unit && npm run test:integration",
18
+ "model:init": "qvac-dev model init",
19
+ "model:new": "qvac-dev model new",
20
+ "model:generate": "qvac-dev model generate",
21
+ "test:dts": "tsc index.d.ts --noEmit"
22
+ },
23
+ "files": [
24
+ "binding.js",
25
+ "index.js",
26
+ "marian.js",
27
+ "prebuilds",
28
+ "third-party",
29
+ "lib",
30
+ "index.d.ts"
31
+ ],
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/tetherto/qvac-lib-infer-nmtcpp.git"
35
+ },
36
+ "author": "Tether",
37
+ "keywords": [
38
+ "tether",
39
+ "addon",
40
+ "marian",
41
+ "qvac"
42
+ ],
43
+ "license": "Apache-2.0",
44
+ "bugs": "https://github.com/tetherto/qvac-lib-infer-nmtcpp/issues",
45
+ "homepage": "https://github.com/tetherto/qvac-lib-infer-nmtcpp#readme",
46
+ "devDependencies": {
47
+ "@qvac/dl-base": "^0.1.0",
48
+ "bare-fs": "^3.1.1",
49
+ "bare-process": "^4.2.1",
50
+ "bare-stream": "^2.3.0",
51
+ "bare-subprocess": "^5.0.3",
52
+ "brittle": "^3.4.0",
53
+ "cmake-bare": "^1.5.2",
54
+ "cmake-vcpkg": "^1.1.0",
55
+ "standard": "^17.0.0"
56
+ },
57
+ "dependencies": {
58
+ "@qvac/infer-base": "^0.1.0",
59
+ "@qvac/dl-hyperdrive": "^0.1.0",
60
+ "@qvac/error": "^0.1.0",
61
+ "@qvac/response": "^0.1.0",
62
+ "bare-path": "^3.0.0"
63
+ },
64
+ "exports": {
65
+ "./package": "./package.json",
66
+ ".": "./index.js"
67
+ },
68
+ "types": "index.d.ts"
69
+ }