@ds-hu/dsh-bvi-local 0.0.0-stage → 0.2.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.
- package/LICENSE +21 -0
- package/NOTICE +31 -0
- package/icon.svg +14 -0
- package/lib/index.js +112 -0
- package/lib/provider.js +691 -0
- package/lib/provision/archive.js +334 -0
- package/lib/provision/download.js +344 -0
- package/lib/provision/install.js +427 -0
- package/lib/worker/adapters.js +535 -0
- package/lib/worker/entry.js +672 -0
- package/lib/worker/runtime.js +762 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +41 -4
- package/README.md +0 -3
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-family sherpa-onnx configuration and text policy for the local worker.
|
|
3
|
+
*
|
|
4
|
+
* The worker process owns one recognizer for one model family, so everything that differs between
|
|
5
|
+
* families lives here: the exact `OfflineRecognizer` / `OnlineRecognizer` configuration literal
|
|
6
|
+
* (copied field by field from `docs/research/local-models.md` §2.2–§8.2), the language mapping
|
|
7
|
+
* (SenseVoice language codes, FunASR Chinese prompt words, Qwen3 English language names), the
|
|
8
|
+
* per-stream options and the text cleanup rules. The worker's transport and decode loop stay
|
|
9
|
+
* family-neutral.
|
|
10
|
+
*
|
|
11
|
+
* **This module never loads `sherpa-onnx-node`.** `configure` is a pure function of the resolved
|
|
12
|
+
* absolute file paths and options, so the literals can be asserted by unit tests on a machine
|
|
13
|
+
* without the native library.
|
|
14
|
+
*
|
|
15
|
+
* @module @ds-hu/dsh-bvi-local/worker/adapters
|
|
16
|
+
*/
|
|
17
|
+
import { dirname } from 'node:path'
|
|
18
|
+
|
|
19
|
+
/** Recordings longer than this are segmented before decoding (docs §1.5 / §2.4 / §7.4). */
|
|
20
|
+
export const LONG_AUDIO_SECONDS = 24
|
|
21
|
+
|
|
22
|
+
/** A request rejected before native inference; the worker maps it to HTTP 400. */
|
|
23
|
+
export class AdapterInputError extends Error {
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} message - human-readable explanation.
|
|
26
|
+
* @param {ErrorOptions} [options] - native error options.
|
|
27
|
+
*/
|
|
28
|
+
constructor(message, options) {
|
|
29
|
+
super(message, options)
|
|
30
|
+
this.name = 'AdapterInputError'
|
|
31
|
+
this.code = 'invalid-input'
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Language codes SenseVoice's C++ `Validate()` accepts (anything else is rejected by the model). */
|
|
36
|
+
export const SENSE_VOICE_LANGUAGES = Object.freeze(['auto', 'zh', 'en', 'ja', 'ko', 'yue'])
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* FunASR Nano language prompt words (docs §3.3): the C++ layer splices the value into
|
|
40
|
+
* `语音转写成<language>:`, so only these Chinese words are meaningful. `auto` maps to the empty
|
|
41
|
+
* string, which leaves the default `userPrompt` untouched.
|
|
42
|
+
*/
|
|
43
|
+
export const FUNASR_LANGUAGE_PROMPTS = Object.freeze({
|
|
44
|
+
auto: '',
|
|
45
|
+
zh: '中文',
|
|
46
|
+
en: '英文',
|
|
47
|
+
ja: '日文',
|
|
48
|
+
yue: '粤语',
|
|
49
|
+
ko: '韩文',
|
|
50
|
+
vi: '越南语',
|
|
51
|
+
id: '印尼语',
|
|
52
|
+
th: '泰语',
|
|
53
|
+
ms: '马来语',
|
|
54
|
+
fil: '菲律宾语',
|
|
55
|
+
ar: '阿拉伯语',
|
|
56
|
+
hi: '印地语',
|
|
57
|
+
bg: '保加利亚语',
|
|
58
|
+
hr: '克罗地亚语',
|
|
59
|
+
cs: '捷克语',
|
|
60
|
+
da: '丹麦语',
|
|
61
|
+
nl: '荷兰语',
|
|
62
|
+
et: '爱沙尼亚语',
|
|
63
|
+
fi: '芬兰语',
|
|
64
|
+
el: '希腊语',
|
|
65
|
+
hu: '匈牙利语',
|
|
66
|
+
ga: '爱尔兰语',
|
|
67
|
+
lv: '拉脱维亚语',
|
|
68
|
+
lt: '立陶宛语',
|
|
69
|
+
mt: '马耳他语',
|
|
70
|
+
pl: '波兰语',
|
|
71
|
+
pt: '葡萄牙语',
|
|
72
|
+
ro: '罗马尼亚语',
|
|
73
|
+
sk: '斯洛伐克语',
|
|
74
|
+
sl: '斯洛文尼亚语',
|
|
75
|
+
sv: '瑞典语',
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Qwen3-ASR per-stream language hints use the English language names from the model card
|
|
80
|
+
* (docs §4.3). Qwen3 only receives a hint when the user explicitly selects a language; `auto`
|
|
81
|
+
* leaves language detection to the model.
|
|
82
|
+
*/
|
|
83
|
+
export const QWEN3_LANGUAGE_NAMES = Object.freeze({
|
|
84
|
+
zh: 'Chinese',
|
|
85
|
+
en: 'English',
|
|
86
|
+
yue: 'Cantonese',
|
|
87
|
+
ar: 'Arabic',
|
|
88
|
+
de: 'German',
|
|
89
|
+
fr: 'French',
|
|
90
|
+
es: 'Spanish',
|
|
91
|
+
pt: 'Portuguese',
|
|
92
|
+
id: 'Indonesian',
|
|
93
|
+
it: 'Italian',
|
|
94
|
+
ko: 'Korean',
|
|
95
|
+
ru: 'Russian',
|
|
96
|
+
th: 'Thai',
|
|
97
|
+
vi: 'Vietnamese',
|
|
98
|
+
ja: 'Japanese',
|
|
99
|
+
tr: 'Turkish',
|
|
100
|
+
hi: 'Hindi',
|
|
101
|
+
ms: 'Malay',
|
|
102
|
+
nl: 'Dutch',
|
|
103
|
+
sv: 'Swedish',
|
|
104
|
+
da: 'Danish',
|
|
105
|
+
fi: 'Finnish',
|
|
106
|
+
pl: 'Polish',
|
|
107
|
+
cs: 'Czech',
|
|
108
|
+
fil: 'Filipino',
|
|
109
|
+
fa: 'Persian',
|
|
110
|
+
el: 'Greek',
|
|
111
|
+
hu: 'Hungarian',
|
|
112
|
+
mk: 'Macedonian',
|
|
113
|
+
ro: 'Romanian',
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Find one of several documented file names in the resolved path map.
|
|
118
|
+
* @param {Record<string, string>} paths - variant-relative path -> absolute path.
|
|
119
|
+
* @param {string[]} candidates - documented relative paths, most specific first.
|
|
120
|
+
* @param {string} family - family used in the error message.
|
|
121
|
+
* @returns {string} absolute path of the first present candidate.
|
|
122
|
+
* @throws {AdapterInputError} when the catalog variant does not contain any candidate.
|
|
123
|
+
*/
|
|
124
|
+
function requiredPath(paths, candidates, family) {
|
|
125
|
+
for (const candidate of candidates) {
|
|
126
|
+
if (typeof paths[candidate] === 'string') return paths[candidate]
|
|
127
|
+
}
|
|
128
|
+
throw new AdapterInputError(`${family}: the variant is missing ${candidates.join(' or ')}`)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Resolve the directory that holds a tokenizer, given one of its files.
|
|
133
|
+
* @param {Record<string, string>} paths - variant-relative path -> absolute path.
|
|
134
|
+
* @param {string} suffix - suffix of a file inside the tokenizer directory (e.g. `/merges.txt`).
|
|
135
|
+
* @param {string} family - family used in the error message.
|
|
136
|
+
* @returns {string} absolute tokenizer directory.
|
|
137
|
+
* @throws {AdapterInputError} when the variant has no file with this suffix.
|
|
138
|
+
*/
|
|
139
|
+
function tokenizerDirectory(paths, suffix, family) {
|
|
140
|
+
for (const [relativePath, absolute] of Object.entries(paths)) {
|
|
141
|
+
if (relativePath.endsWith(suffix)) return dirname(absolute)
|
|
142
|
+
}
|
|
143
|
+
throw new AdapterInputError(`${family}: the variant is missing its tokenizer files (${suffix})`)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @param {string} language - requested language code.
|
|
148
|
+
* @param {Record<string, string>} mapping - supported code table.
|
|
149
|
+
* @param {string} family - family used in the error message.
|
|
150
|
+
* @returns {string} the mapped value.
|
|
151
|
+
* @throws {AdapterInputError} when the code is not supported by this family.
|
|
152
|
+
*/
|
|
153
|
+
function mappedLanguage(language, mapping, family) {
|
|
154
|
+
if (!Object.hasOwn(mapping, language)) {
|
|
155
|
+
throw new AdapterInputError(`${family}: unsupported language code: ${language}`)
|
|
156
|
+
}
|
|
157
|
+
return mapping[language]
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Validate a SenseVoice language code.
|
|
162
|
+
* @param {string} language - requested language code.
|
|
163
|
+
* @returns {string} the accepted code.
|
|
164
|
+
* @throws {AdapterInputError} when the model cannot use this language.
|
|
165
|
+
*/
|
|
166
|
+
function senseVoiceLanguage(language) {
|
|
167
|
+
if (!SENSE_VOICE_LANGUAGES.includes(language)) {
|
|
168
|
+
throw new AdapterInputError(`sensevoice: unsupported language code: ${language}`)
|
|
169
|
+
}
|
|
170
|
+
return language
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* SenseVoice config (docs §2.2). `language` is model-level in the C++ binding, so it is a
|
|
175
|
+
* configuration field changed with `recognizer.setConfig()` per request.
|
|
176
|
+
* @param {Record<string, string>} paths - variant-relative path -> absolute path.
|
|
177
|
+
* @param {{ threads?: number, language?: string }} [options] - thread count and language code.
|
|
178
|
+
* @returns {Record<string, any>} `OfflineRecognizer` configuration literal.
|
|
179
|
+
*/
|
|
180
|
+
function configureSenseVoice(paths, options = {}) {
|
|
181
|
+
const language = senseVoiceLanguage(options.language ?? 'auto')
|
|
182
|
+
return {
|
|
183
|
+
featConfig: { sampleRate: 16000, featureDim: 80 },
|
|
184
|
+
modelConfig: {
|
|
185
|
+
senseVoice: {
|
|
186
|
+
model: requiredPath(paths, ['model.int8.onnx', 'model.onnx'], 'sensevoice'),
|
|
187
|
+
language,
|
|
188
|
+
useInverseTextNormalization: 1,
|
|
189
|
+
},
|
|
190
|
+
tokens: requiredPath(paths, ['tokens.txt'], 'sensevoice'),
|
|
191
|
+
numThreads: options.threads ?? 2,
|
|
192
|
+
provider: 'cpu',
|
|
193
|
+
debug: 0,
|
|
194
|
+
},
|
|
195
|
+
decodingMethod: 'greedy_search',
|
|
196
|
+
maxActivePaths: 4,
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* FunASR Nano config (docs §3.2/§3.3): `tokens` must stay empty because the tokenizer is loaded
|
|
202
|
+
* from the directory, and the language is expressed as a Chinese prompt word.
|
|
203
|
+
* @param {Record<string, string>} paths - variant-relative path -> absolute path.
|
|
204
|
+
* @param {{ threads?: number, language?: string }} [options] - thread count and language code.
|
|
205
|
+
* @returns {Record<string, any>} `OfflineRecognizer` configuration literal.
|
|
206
|
+
*/
|
|
207
|
+
function configureFunasrNano(paths, options = {}) {
|
|
208
|
+
const prompt = mappedLanguage(options.language ?? 'auto', FUNASR_LANGUAGE_PROMPTS, 'funasr-nano')
|
|
209
|
+
return {
|
|
210
|
+
featConfig: { sampleRate: 16000, featureDim: 80 },
|
|
211
|
+
modelConfig: {
|
|
212
|
+
funasrNano: {
|
|
213
|
+
encoderAdaptor: requiredPath(paths, ['encoder_adaptor.int8.onnx', 'encoder_adaptor.onnx'], 'funasr-nano'),
|
|
214
|
+
llm: requiredPath(paths, ['llm.int8.onnx', 'llm.onnx'], 'funasr-nano'),
|
|
215
|
+
embedding: requiredPath(paths, ['embedding.int8.onnx', 'embedding.onnx'], 'funasr-nano'),
|
|
216
|
+
tokenizer: tokenizerDirectory(paths, '/tokenizer.json', 'funasr-nano'),
|
|
217
|
+
systemPrompt: 'You are a helpful assistant.',
|
|
218
|
+
userPrompt: '语音转写:',
|
|
219
|
+
maxNewTokens: 512,
|
|
220
|
+
temperature: 1e-6,
|
|
221
|
+
topP: 0.8,
|
|
222
|
+
seed: 42,
|
|
223
|
+
itn: true,
|
|
224
|
+
language: prompt,
|
|
225
|
+
},
|
|
226
|
+
tokens: '',
|
|
227
|
+
numThreads: options.threads ?? 4,
|
|
228
|
+
provider: 'cpu',
|
|
229
|
+
debug: 0,
|
|
230
|
+
},
|
|
231
|
+
decodingMethod: 'greedy_search',
|
|
232
|
+
maxActivePaths: 4,
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Qwen3-ASR config (docs §4.2). Language hints are per-stream options, so the model configuration
|
|
238
|
+
* is constant; `tokens` must stay empty and the tokenizer is a directory.
|
|
239
|
+
* @param {Record<string, string>} paths - variant-relative path -> absolute path.
|
|
240
|
+
* @param {{ threads?: number }} [options] - thread count.
|
|
241
|
+
* @returns {Record<string, any>} `OfflineRecognizer` configuration literal.
|
|
242
|
+
*/
|
|
243
|
+
function configureQwen3Asr(paths, options = {}) {
|
|
244
|
+
return {
|
|
245
|
+
featConfig: { sampleRate: 16000, featureDim: 80 },
|
|
246
|
+
modelConfig: {
|
|
247
|
+
qwen3Asr: {
|
|
248
|
+
convFrontend: requiredPath(paths, ['conv_frontend.onnx'], 'qwen3-asr'),
|
|
249
|
+
encoder: requiredPath(paths, ['encoder.int8.onnx', 'encoder.onnx'], 'qwen3-asr'),
|
|
250
|
+
decoder: requiredPath(paths, ['decoder.int8.onnx', 'decoder.onnx'], 'qwen3-asr'),
|
|
251
|
+
tokenizer: tokenizerDirectory(paths, '/merges.txt', 'qwen3-asr'),
|
|
252
|
+
maxTotalLen: 512,
|
|
253
|
+
maxNewTokens: 512,
|
|
254
|
+
},
|
|
255
|
+
tokens: '',
|
|
256
|
+
numThreads: options.threads ?? 3,
|
|
257
|
+
provider: 'cpu',
|
|
258
|
+
debug: 0,
|
|
259
|
+
},
|
|
260
|
+
decodingMethod: 'greedy_search',
|
|
261
|
+
maxActivePaths: 4,
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Parakeet TDT config (docs §5.2): `modelType` is mandatory so the NeMo transducer branch is used
|
|
267
|
+
* instead of the default zipformer/transducer deserialization.
|
|
268
|
+
* @param {Record<string, string>} paths - variant-relative path -> absolute path.
|
|
269
|
+
* @param {{ threads?: number }} [options] - thread count.
|
|
270
|
+
* @returns {Record<string, any>} `OfflineRecognizer` configuration literal.
|
|
271
|
+
*/
|
|
272
|
+
function configureParakeet(paths, options = {}) {
|
|
273
|
+
return {
|
|
274
|
+
featConfig: { sampleRate: 16000, featureDim: 80 },
|
|
275
|
+
modelConfig: {
|
|
276
|
+
transducer: {
|
|
277
|
+
encoder: requiredPath(paths, ['encoder.int8.onnx', 'encoder.onnx'], 'parakeet'),
|
|
278
|
+
decoder: requiredPath(paths, ['decoder.int8.onnx', 'decoder.onnx'], 'parakeet'),
|
|
279
|
+
joiner: requiredPath(paths, ['joiner.int8.onnx', 'joiner.onnx'], 'parakeet'),
|
|
280
|
+
},
|
|
281
|
+
tokens: requiredPath(paths, ['tokens.txt'], 'parakeet'),
|
|
282
|
+
numThreads: options.threads ?? 3,
|
|
283
|
+
provider: 'cpu',
|
|
284
|
+
modelType: 'nemo_transducer',
|
|
285
|
+
debug: 0,
|
|
286
|
+
},
|
|
287
|
+
decodingMethod: 'greedy_search',
|
|
288
|
+
maxActivePaths: 4,
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* GigaAM v3 config (docs §6.2): a NeMo transducer whose decoder/joiner files are fp32 while the
|
|
294
|
+
* encoder is int8.
|
|
295
|
+
* @param {Record<string, string>} paths - variant-relative path -> absolute path.
|
|
296
|
+
* @param {{ threads?: number }} [options] - thread count.
|
|
297
|
+
* @returns {Record<string, any>} `OfflineRecognizer` configuration literal.
|
|
298
|
+
*/
|
|
299
|
+
function configureGigaAm(paths, options = {}) {
|
|
300
|
+
return {
|
|
301
|
+
featConfig: { sampleRate: 16000, featureDim: 80 },
|
|
302
|
+
modelConfig: {
|
|
303
|
+
transducer: {
|
|
304
|
+
encoder: requiredPath(paths, ['encoder.int8.onnx', 'encoder.onnx'], 'giga-am'),
|
|
305
|
+
decoder: requiredPath(paths, ['decoder.onnx'], 'giga-am'),
|
|
306
|
+
joiner: requiredPath(paths, ['joiner.onnx'], 'giga-am'),
|
|
307
|
+
},
|
|
308
|
+
tokens: requiredPath(paths, ['tokens.txt'], 'giga-am'),
|
|
309
|
+
numThreads: options.threads ?? 3,
|
|
310
|
+
provider: 'cpu',
|
|
311
|
+
modelType: 'nemo_transducer',
|
|
312
|
+
debug: 0,
|
|
313
|
+
},
|
|
314
|
+
decodingMethod: 'greedy_search',
|
|
315
|
+
maxActivePaths: 4,
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* FireRedASR2 CTC config (docs §7.2).
|
|
321
|
+
* @param {Record<string, string>} paths - variant-relative path -> absolute path.
|
|
322
|
+
* @param {{ threads?: number }} [options] - thread count.
|
|
323
|
+
* @returns {Record<string, any>} `OfflineRecognizer` configuration literal.
|
|
324
|
+
*/
|
|
325
|
+
function configureFireRedAsr(paths, options = {}) {
|
|
326
|
+
return {
|
|
327
|
+
featConfig: { sampleRate: 16000, featureDim: 80 },
|
|
328
|
+
modelConfig: {
|
|
329
|
+
fireRedAsrCtc: {
|
|
330
|
+
model: requiredPath(paths, ['model.int8.onnx', 'model.onnx'], 'firered-asr'),
|
|
331
|
+
},
|
|
332
|
+
tokens: requiredPath(paths, ['tokens.txt'], 'firered-asr'),
|
|
333
|
+
numThreads: options.threads ?? 2,
|
|
334
|
+
provider: 'cpu',
|
|
335
|
+
debug: 0,
|
|
336
|
+
},
|
|
337
|
+
decodingMethod: 'greedy_search',
|
|
338
|
+
maxActivePaths: 4,
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* X-ASR streaming config (docs §8.2). Endpointing stays disabled because the worker performs the
|
|
344
|
+
* documented drain → 2×480 ms silence → inputFinished → drain shutdown itself.
|
|
345
|
+
* @param {Record<string, string>} paths - variant-relative path -> absolute path.
|
|
346
|
+
* @param {{ threads?: number }} [options] - thread count.
|
|
347
|
+
* @returns {Record<string, any>} `OnlineRecognizer` configuration literal.
|
|
348
|
+
*/
|
|
349
|
+
function configureXAsr(paths, options = {}) {
|
|
350
|
+
return {
|
|
351
|
+
featConfig: { sampleRate: 16000, featureDim: 80 },
|
|
352
|
+
modelConfig: {
|
|
353
|
+
transducer: {
|
|
354
|
+
encoder: requiredPath(paths, ['encoder-480ms.onnx'], 'x-asr'),
|
|
355
|
+
decoder: requiredPath(paths, ['decoder-480ms.onnx'], 'x-asr'),
|
|
356
|
+
joiner: requiredPath(paths, ['joiner-480ms.onnx'], 'x-asr'),
|
|
357
|
+
},
|
|
358
|
+
tokens: requiredPath(paths, ['tokens.txt'], 'x-asr'),
|
|
359
|
+
numThreads: options.threads ?? 2,
|
|
360
|
+
provider: 'cpu',
|
|
361
|
+
modelType: 'zipformer2',
|
|
362
|
+
debug: 0,
|
|
363
|
+
},
|
|
364
|
+
decodingMethod: 'greedy_search',
|
|
365
|
+
maxActivePaths: 4,
|
|
366
|
+
enableEndpoint: false,
|
|
367
|
+
rule1MinTrailingSilence: 2.4,
|
|
368
|
+
rule2MinTrailingSilence: 1.2,
|
|
369
|
+
rule3MinUtteranceLength: 20,
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Remove the Qwen3 scaffold and special tokens the C++ layer normally strips (docs §4.5).
|
|
375
|
+
* @param {string} text - raw recognizer output.
|
|
376
|
+
* @returns {string} cleaned text.
|
|
377
|
+
*/
|
|
378
|
+
export function sanitizeQwen3(text) {
|
|
379
|
+
return text
|
|
380
|
+
.replace(/<\|im_start\|>|<\|im_end\|>/g, '')
|
|
381
|
+
.replace(/^\s*language\s+[^<]*<asr_text>/i, '')
|
|
382
|
+
.replace(/<asr_text>/g, '')
|
|
383
|
+
.trim()
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Clean FireRedASR2 output: drop silence markers and collapse whitespace (docs §7.3).
|
|
388
|
+
* @param {string} text - raw recognizer output.
|
|
389
|
+
* @returns {string} cleaned text.
|
|
390
|
+
*/
|
|
391
|
+
export function sanitizeFireRed(text) {
|
|
392
|
+
return text
|
|
393
|
+
.replace(/<sil>/gi, '')
|
|
394
|
+
.replace(/\s+/g, ' ')
|
|
395
|
+
.trim()
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Normalize X-ASR output spacing: no spaces between CJK characters, one space elsewhere
|
|
400
|
+
* (docs §8.4).
|
|
401
|
+
* @param {string} text - raw recognizer output.
|
|
402
|
+
* @returns {string} cleaned text.
|
|
403
|
+
*/
|
|
404
|
+
export function sanitizeXAsr(text) {
|
|
405
|
+
return text
|
|
406
|
+
.replace(/\s+/g, ' ')
|
|
407
|
+
.replace(/([\u4e00-\u9fff])\s+(?=[\u4e00-\u9fff])/g, '$1')
|
|
408
|
+
.trim()
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** Characters that may need a separating space at a chunk boundary (ASCII letters, digits, Cyrillic). */
|
|
412
|
+
const BOUNDARY_WORD = /[A-Za-z0-9\u0400-\u04ff]/
|
|
413
|
+
/** Punctuation that already ends a sentence; the next chunk starts a new one. */
|
|
414
|
+
const BOUNDARY_PUNCTUATION = /[,!?;:.]$/
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Join the texts of segmented chunks (docs §1.5 / `joinNonStreamingChunkTexts`): a space is added
|
|
418
|
+
* only between two ASCII/Cyrillic word characters or after sentence punctuation; CJK boundaries
|
|
419
|
+
* stay unspaced.
|
|
420
|
+
* @param {string[]} parts - per-chunk texts in order.
|
|
421
|
+
* @returns {string} joined transcript.
|
|
422
|
+
*/
|
|
423
|
+
export function joinChunkTexts(parts) {
|
|
424
|
+
let joined = ''
|
|
425
|
+
for (const part of parts) {
|
|
426
|
+
const text = part.trim()
|
|
427
|
+
if (text === '') continue
|
|
428
|
+
if (joined === '') {
|
|
429
|
+
joined = text
|
|
430
|
+
continue
|
|
431
|
+
}
|
|
432
|
+
const previous = joined[joined.length - 1]
|
|
433
|
+
const next = text[0]
|
|
434
|
+
const needsSpace = BOUNDARY_PUNCTUATION.test(previous) || (BOUNDARY_WORD.test(previous) && BOUNDARY_WORD.test(next))
|
|
435
|
+
joined += needsSpace ? ` ${text}` : text
|
|
436
|
+
}
|
|
437
|
+
return joined
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Per-family policy table consumed by the worker.
|
|
442
|
+
*
|
|
443
|
+
* - `configure` returns the exact sherpa-onnx literal for the family.
|
|
444
|
+
* - `languageSensitive` marks families whose language is part of the recognizer configuration and
|
|
445
|
+
* therefore needs `recognizer.setConfig()` per request (SenseVoice, FunASR Nano).
|
|
446
|
+
* - `streamOptions` returns `OfflineStream.setOption` pairs (Qwen3-ASR language hints).
|
|
447
|
+
* - `sanitize` applies the documented text cleanup.
|
|
448
|
+
*
|
|
449
|
+
* @type {Record<string, {
|
|
450
|
+
* family: string,
|
|
451
|
+
* languageSensitive: boolean,
|
|
452
|
+
* configure: (paths: Record<string, string>, options?: { threads?: number, language?: string }) => Record<string, any>,
|
|
453
|
+
* validateLanguage: (language: string) => void,
|
|
454
|
+
* streamOptions: (language: string) => Array<[string, string]>,
|
|
455
|
+
* sanitize: (text: string) => string,
|
|
456
|
+
* }>}
|
|
457
|
+
*/
|
|
458
|
+
export const FAMILY_ADAPTERS = Object.freeze({
|
|
459
|
+
sensevoice: Object.freeze({
|
|
460
|
+
family: 'sensevoice',
|
|
461
|
+
languageSensitive: true,
|
|
462
|
+
configure: configureSenseVoice,
|
|
463
|
+
validateLanguage: (language) => { senseVoiceLanguage(language) },
|
|
464
|
+
streamOptions: () => [],
|
|
465
|
+
sanitize: (text) => text.trim(),
|
|
466
|
+
}),
|
|
467
|
+
'funasr-nano': Object.freeze({
|
|
468
|
+
family: 'funasr-nano',
|
|
469
|
+
languageSensitive: true,
|
|
470
|
+
configure: configureFunasrNano,
|
|
471
|
+
validateLanguage: (language) => { mappedLanguage(language, FUNASR_LANGUAGE_PROMPTS, 'funasr-nano') },
|
|
472
|
+
streamOptions: () => [],
|
|
473
|
+
sanitize: (text) => text.trim(),
|
|
474
|
+
}),
|
|
475
|
+
'qwen3-asr': Object.freeze({
|
|
476
|
+
family: 'qwen3-asr',
|
|
477
|
+
languageSensitive: false,
|
|
478
|
+
configure: configureQwen3Asr,
|
|
479
|
+
validateLanguage: (language) => {
|
|
480
|
+
if (language === 'auto') return
|
|
481
|
+
if (QWEN3_LANGUAGE_NAMES[language] === undefined) throw new AdapterInputError(`qwen3-asr: unsupported language code: ${language}`)
|
|
482
|
+
},
|
|
483
|
+
streamOptions: (language) => {
|
|
484
|
+
if (language === 'auto') return []
|
|
485
|
+
const name = QWEN3_LANGUAGE_NAMES[language]
|
|
486
|
+
if (name === undefined) throw new AdapterInputError(`qwen3-asr: unsupported language code: ${language}`)
|
|
487
|
+
return [['language', name]]
|
|
488
|
+
},
|
|
489
|
+
sanitize: sanitizeQwen3,
|
|
490
|
+
}),
|
|
491
|
+
parakeet: Object.freeze({
|
|
492
|
+
family: 'parakeet',
|
|
493
|
+
languageSensitive: false,
|
|
494
|
+
configure: configureParakeet,
|
|
495
|
+
validateLanguage: () => undefined,
|
|
496
|
+
streamOptions: () => [],
|
|
497
|
+
sanitize: (text) => text.trim(),
|
|
498
|
+
}),
|
|
499
|
+
'giga-am': Object.freeze({
|
|
500
|
+
family: 'giga-am',
|
|
501
|
+
languageSensitive: false,
|
|
502
|
+
configure: configureGigaAm,
|
|
503
|
+
validateLanguage: () => undefined,
|
|
504
|
+
streamOptions: () => [],
|
|
505
|
+
sanitize: (text) => text.trim(),
|
|
506
|
+
}),
|
|
507
|
+
'firered-asr': Object.freeze({
|
|
508
|
+
family: 'firered-asr',
|
|
509
|
+
languageSensitive: false,
|
|
510
|
+
configure: configureFireRedAsr,
|
|
511
|
+
validateLanguage: () => undefined,
|
|
512
|
+
streamOptions: () => [],
|
|
513
|
+
sanitize: sanitizeFireRed,
|
|
514
|
+
}),
|
|
515
|
+
'x-asr': Object.freeze({
|
|
516
|
+
family: 'x-asr',
|
|
517
|
+
languageSensitive: false,
|
|
518
|
+
configure: configureXAsr,
|
|
519
|
+
validateLanguage: () => undefined,
|
|
520
|
+
streamOptions: () => [],
|
|
521
|
+
sanitize: sanitizeXAsr,
|
|
522
|
+
}),
|
|
523
|
+
})
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Look up the adapter of one catalog family.
|
|
527
|
+
* @param {string} family - catalog family id.
|
|
528
|
+
* @returns {(typeof FAMILY_ADAPTERS)[string]} the family adapter.
|
|
529
|
+
* @throws {Error} when the family is unknown (a catalog/code mismatch must fail loudly).
|
|
530
|
+
*/
|
|
531
|
+
export function adapterFor(family) {
|
|
532
|
+
const adapter = FAMILY_ADAPTERS[family]
|
|
533
|
+
if (adapter === undefined) throw new Error(`Unknown local model family: ${String(family)}`)
|
|
534
|
+
return adapter
|
|
535
|
+
}
|