speechflow 0.9.8 → 0.9.9
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/CHANGELOG.md +10 -0
- package/LICENSE.txt +674 -0
- package/README.md +66 -16
- package/dst/speechflow-node-a2a-vad.d.ts +16 -0
- package/dst/speechflow-node-a2a-vad.js +431 -0
- package/dst/speechflow-node-t2a-kokoro.d.ts +13 -0
- package/dst/speechflow-node-t2a-kokoro.js +147 -0
- package/dst/speechflow-node-t2t-gemma.js +23 -3
- package/dst/speechflow-node-t2t-ollama.d.ts +13 -0
- package/dst/speechflow-node-t2t-ollama.js +245 -0
- package/dst/speechflow-node-t2t-openai.d.ts +13 -0
- package/dst/speechflow-node-t2t-openai.js +225 -0
- package/dst/speechflow-node-t2t-opus.js +1 -1
- package/dst/speechflow-node-t2t-transformers.d.ts +14 -0
- package/dst/speechflow-node-t2t-transformers.js +260 -0
- package/dst/speechflow-node-x2x-trace.js +2 -2
- package/dst/speechflow.js +86 -40
- package/etc/speechflow.yaml +9 -2
- package/etc/stx.conf +1 -1
- package/package.json +7 -6
- package/src/speechflow-node-t2a-kokoro.ts +160 -0
- package/src/{speechflow-node-t2t-gemma.ts → speechflow-node-t2t-ollama.ts} +44 -10
- package/src/speechflow-node-t2t-openai.ts +246 -0
- package/src/speechflow-node-t2t-transformers.ts +244 -0
- package/src/speechflow-node-x2x-trace.ts +2 -2
- package/src/speechflow.ts +86 -40
- package/src/speechflow-node-t2t-opus.ts +0 -111
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
** SpeechFlow - Speech Processing Flow Graph
|
|
4
|
+
** Copyright (c) 2024-2025 Dr. Ralf S. Engelschall <rse@engelschall.com>
|
|
5
|
+
** Licensed under GPL 3.0 <https://spdx.org/licenses/GPL-3.0-only>
|
|
6
|
+
*/
|
|
7
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
8
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
|
+
};
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
/* standard dependencies */
|
|
12
|
+
const node_stream_1 = __importDefault(require("node:stream"));
|
|
13
|
+
/* external dependencies */
|
|
14
|
+
const openai_1 = __importDefault(require("openai"));
|
|
15
|
+
/* internal dependencies */
|
|
16
|
+
const speechflow_node_1 = __importDefault(require("./speechflow-node"));
|
|
17
|
+
/* SpeechFlow node for OpenAI/GPT text-to-text translation */
|
|
18
|
+
class SpeechFlowNodeOpenAI extends speechflow_node_1.default {
|
|
19
|
+
/* declare official node name */
|
|
20
|
+
static name = "openai";
|
|
21
|
+
/* internal state */
|
|
22
|
+
openai = null;
|
|
23
|
+
/* internal LLM setup */
|
|
24
|
+
setup = {
|
|
25
|
+
/* English (EN) spellchecking only */
|
|
26
|
+
"en-en": {
|
|
27
|
+
systemPrompt: "You are a proofreader and spellchecker for English.\n" +
|
|
28
|
+
"Output only the corrected text.\n" +
|
|
29
|
+
"Do NOT use markdown.\n" +
|
|
30
|
+
"Do NOT give any explanations.\n" +
|
|
31
|
+
"Do NOT give any introduction.\n" +
|
|
32
|
+
"Do NOT give any comments.\n" +
|
|
33
|
+
"Do NOT give any preamble.\n" +
|
|
34
|
+
"Do NOT give any prolog.\n" +
|
|
35
|
+
"Do NOT give any epilog.\n" +
|
|
36
|
+
"Do NOT change the gammar.\n" +
|
|
37
|
+
"Do NOT use synonyms for words.\n" +
|
|
38
|
+
"Keep all words.\n" +
|
|
39
|
+
"Fill in missing commas.\n" +
|
|
40
|
+
"Fill in missing points.\n" +
|
|
41
|
+
"Fill in missing question marks.\n" +
|
|
42
|
+
"Fill in missing hyphens.\n" +
|
|
43
|
+
"Focus ONLY on the word spelling.\n" +
|
|
44
|
+
"The text you have to correct is:\n",
|
|
45
|
+
chat: [
|
|
46
|
+
{ role: "user", content: "I luve my wyfe" },
|
|
47
|
+
{ role: "system", content: "I love my wife." },
|
|
48
|
+
{ role: "user", content: "The weether is wunderfull!" },
|
|
49
|
+
{ role: "system", content: "The weather is wonderful!" },
|
|
50
|
+
{ role: "user", content: "The live awesome but I'm hungry." },
|
|
51
|
+
{ role: "system", content: "The live is awesome, but I'm hungry." }
|
|
52
|
+
]
|
|
53
|
+
},
|
|
54
|
+
/* German (DE) spellchecking only */
|
|
55
|
+
"de-de": {
|
|
56
|
+
systemPrompt: "Du bist ein Korrekturleser und Rechtschreibprüfer für Deutsch.\n" +
|
|
57
|
+
"Gib nur den korrigierten Text aus.\n" +
|
|
58
|
+
"Benutze KEIN Markdown.\n" +
|
|
59
|
+
"Gib KEINE Erklärungen.\n" +
|
|
60
|
+
"Gib KEINE Einleitung.\n" +
|
|
61
|
+
"Gib KEINE Kommentare.\n" +
|
|
62
|
+
"Gib KEINE Preamble.\n" +
|
|
63
|
+
"Gib KEINEN Prolog.\n" +
|
|
64
|
+
"Gib KEINEN Epilog.\n" +
|
|
65
|
+
"Ändere NICHT die Grammatik.\n" +
|
|
66
|
+
"Verwende KEINE Synonyme für Wörter.\n" +
|
|
67
|
+
"Behalte alle Wörter bei.\n" +
|
|
68
|
+
"Füge fehlende Kommas ein.\n" +
|
|
69
|
+
"Füge fehlende Punkte ein.\n" +
|
|
70
|
+
"Füge fehlende Fragezeichen ein.\n" +
|
|
71
|
+
"Füge fehlende Bindestriche ein.\n" +
|
|
72
|
+
"Füge fehlende Gedankenstriche ein.\n" +
|
|
73
|
+
"Fokussiere dich NUR auf die Rechtschreibung der Wörter.\n" +
|
|
74
|
+
"Der von dir zu korrigierende Text ist:\n",
|
|
75
|
+
chat: [
|
|
76
|
+
{ role: "user", content: "Ich ljebe meine Frao" },
|
|
77
|
+
{ role: "system", content: "Ich liebe meine Frau." },
|
|
78
|
+
{ role: "user", content: "Die Wedter ist wunderschoen." },
|
|
79
|
+
{ role: "system", content: "Das Wetter ist wunderschön." },
|
|
80
|
+
{ role: "user", content: "Das Leben einfach großartig aber ich bin hungrig." },
|
|
81
|
+
{ role: "system", content: "Das Leben ist einfach großartig, aber ich bin hungrig." }
|
|
82
|
+
]
|
|
83
|
+
},
|
|
84
|
+
/* English (EN) to German (DE) translation */
|
|
85
|
+
"en-de": {
|
|
86
|
+
systemPrompt: "You are a translator.\n" +
|
|
87
|
+
"Output only the requested text.\n" +
|
|
88
|
+
"Do not use markdown.\n" +
|
|
89
|
+
"Do not chat.\n" +
|
|
90
|
+
"Do not show any explanations.\n" +
|
|
91
|
+
"Do not show any introduction.\n" +
|
|
92
|
+
"Do not show any preamble.\n" +
|
|
93
|
+
"Do not show any prolog.\n" +
|
|
94
|
+
"Do not show any epilog.\n" +
|
|
95
|
+
"Get to the point.\n" +
|
|
96
|
+
"Preserve the original meaning, tone, and nuance.\n" +
|
|
97
|
+
"Directly translate text from English (EN) to fluent and natural German (DE) language.\n",
|
|
98
|
+
chat: [
|
|
99
|
+
{ role: "user", content: "I love my wife." },
|
|
100
|
+
{ role: "system", content: "Ich liebe meine Frau." },
|
|
101
|
+
{ role: "user", content: "The weather is wonderful." },
|
|
102
|
+
{ role: "system", content: "Das Wetter ist wunderschön." },
|
|
103
|
+
{ role: "user", content: "The live is awesome." },
|
|
104
|
+
{ role: "system", content: "Das Leben ist einfach großartig." }
|
|
105
|
+
]
|
|
106
|
+
},
|
|
107
|
+
/* German (DE) to English (EN) translation */
|
|
108
|
+
"de-en": {
|
|
109
|
+
systemPrompt: "You are a translator.\n" +
|
|
110
|
+
"Output only the requested text.\n" +
|
|
111
|
+
"Do not use markdown.\n" +
|
|
112
|
+
"Do not chat.\n" +
|
|
113
|
+
"Do not show any explanations. \n" +
|
|
114
|
+
"Do not show any introduction.\n" +
|
|
115
|
+
"Do not show any preamble. \n" +
|
|
116
|
+
"Do not show any prolog. \n" +
|
|
117
|
+
"Do not show any epilog. \n" +
|
|
118
|
+
"Get to the point.\n" +
|
|
119
|
+
"Preserve the original meaning, tone, and nuance.\n" +
|
|
120
|
+
"Directly translate text from German (DE) to fluent and natural English (EN) language.\n",
|
|
121
|
+
chat: [
|
|
122
|
+
{ role: "user", content: "Ich liebe meine Frau." },
|
|
123
|
+
{ role: "system", content: "I love my wife." },
|
|
124
|
+
{ role: "user", content: "Das Wetter ist wunderschön." },
|
|
125
|
+
{ role: "system", content: "The weather is wonderful." },
|
|
126
|
+
{ role: "user", content: "Das Leben ist einfach großartig." },
|
|
127
|
+
{ role: "system", content: "The live is awesome." }
|
|
128
|
+
]
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
/* construct node */
|
|
132
|
+
constructor(id, cfg, opts, args) {
|
|
133
|
+
super(id, cfg, opts, args);
|
|
134
|
+
/* declare node configuration parameters */
|
|
135
|
+
this.configure({
|
|
136
|
+
src: { type: "string", pos: 0, val: "de", match: /^(?:de|en)$/ },
|
|
137
|
+
dst: { type: "string", pos: 1, val: "en", match: /^(?:de|en)$/ },
|
|
138
|
+
key: { type: "string", val: process.env.SPEECHFLOW_KEY_OPENAI },
|
|
139
|
+
api: { type: "string", val: "https://api.openai.com/v1", match: /^https?:\/\/.+?:\d+$/ },
|
|
140
|
+
model: { type: "string", val: "gpt-4o-mini" }
|
|
141
|
+
});
|
|
142
|
+
/* tell effective mode */
|
|
143
|
+
if (this.params.src === this.params.dst)
|
|
144
|
+
this.log("info", `OpenAI: operation mode: spellchecking for language "${this.params.src}"`);
|
|
145
|
+
else
|
|
146
|
+
this.log("info", `OpenAI: operation mode: translation from language "${this.params.src}"` +
|
|
147
|
+
` to language "${this.params.dst}"`);
|
|
148
|
+
/* declare node input/output format */
|
|
149
|
+
this.input = "text";
|
|
150
|
+
this.output = "text";
|
|
151
|
+
}
|
|
152
|
+
/* open node */
|
|
153
|
+
async open() {
|
|
154
|
+
/* instantiate OpenAI API */
|
|
155
|
+
this.openai = new openai_1.default({
|
|
156
|
+
baseURL: this.params.api,
|
|
157
|
+
apiKey: this.params.key,
|
|
158
|
+
dangerouslyAllowBrowser: true
|
|
159
|
+
});
|
|
160
|
+
/* provide text-to-text translation */
|
|
161
|
+
const translate = async (text) => {
|
|
162
|
+
const key = `${this.params.src}-${this.params.dst}`;
|
|
163
|
+
const cfg = this.setup[key];
|
|
164
|
+
const stream = this.openai.chat.completions.stream({
|
|
165
|
+
stream: true,
|
|
166
|
+
model: this.params.model,
|
|
167
|
+
seed: null,
|
|
168
|
+
temperature: 0.7,
|
|
169
|
+
n: 1,
|
|
170
|
+
messages: [
|
|
171
|
+
{ role: "system", content: cfg.systemPrompt },
|
|
172
|
+
...cfg.chat,
|
|
173
|
+
{ role: "user", content: text }
|
|
174
|
+
]
|
|
175
|
+
});
|
|
176
|
+
const completion = await stream.finalChatCompletion();
|
|
177
|
+
const translation = completion.choices[0].message.content;
|
|
178
|
+
if (!stream.ended)
|
|
179
|
+
stream.abort();
|
|
180
|
+
return translation;
|
|
181
|
+
};
|
|
182
|
+
/* establish a duplex stream and connect it to OpenAI */
|
|
183
|
+
this.stream = new node_stream_1.default.Transform({
|
|
184
|
+
readableObjectMode: true,
|
|
185
|
+
writableObjectMode: true,
|
|
186
|
+
decodeStrings: false,
|
|
187
|
+
transform(chunk, encoding, callback) {
|
|
188
|
+
if (Buffer.isBuffer(chunk.payload))
|
|
189
|
+
callback(new Error("invalid chunk payload type"));
|
|
190
|
+
else {
|
|
191
|
+
if (chunk.payload === "") {
|
|
192
|
+
this.push(chunk);
|
|
193
|
+
callback();
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
translate(chunk.payload).then((payload) => {
|
|
197
|
+
const chunkNew = chunk.clone();
|
|
198
|
+
chunkNew.payload = payload;
|
|
199
|
+
this.push(chunkNew);
|
|
200
|
+
callback();
|
|
201
|
+
}).catch((err) => {
|
|
202
|
+
callback(err);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
final(callback) {
|
|
208
|
+
this.push(null);
|
|
209
|
+
callback();
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
/* close node */
|
|
214
|
+
async close() {
|
|
215
|
+
/* close stream */
|
|
216
|
+
if (this.stream !== null) {
|
|
217
|
+
this.stream.destroy();
|
|
218
|
+
this.stream = null;
|
|
219
|
+
}
|
|
220
|
+
/* shutdown OpenAI */
|
|
221
|
+
if (this.openai !== null)
|
|
222
|
+
this.openai = null;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
exports.default = SpeechFlowNodeOpenAI;
|
|
@@ -87,7 +87,7 @@ class SpeechFlowNodeOPUS extends speechflow_node_1.default {
|
|
|
87
87
|
result[0].translation_text :
|
|
88
88
|
result.translation_text;
|
|
89
89
|
};
|
|
90
|
-
/* establish a duplex stream and connect it to
|
|
90
|
+
/* establish a duplex stream and connect it to OPUS */
|
|
91
91
|
this.stream = new node_stream_1.default.Transform({
|
|
92
92
|
readableObjectMode: true,
|
|
93
93
|
writableObjectMode: true,
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import SpeechFlowNode from "./speechflow-node";
|
|
2
|
+
export default class SpeechFlowNodeTransformers extends SpeechFlowNode {
|
|
3
|
+
static name: string;
|
|
4
|
+
private translator;
|
|
5
|
+
private generator;
|
|
6
|
+
private setup;
|
|
7
|
+
constructor(id: string, cfg: {
|
|
8
|
+
[id: string]: any;
|
|
9
|
+
}, opts: {
|
|
10
|
+
[id: string]: any;
|
|
11
|
+
}, args: any[]);
|
|
12
|
+
open(): Promise<void>;
|
|
13
|
+
close(): Promise<void>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
** SpeechFlow - Speech Processing Flow Graph
|
|
4
|
+
** Copyright (c) 2024-2025 Dr. Ralf S. Engelschall <rse@engelschall.com>
|
|
5
|
+
** Licensed under GPL 3.0 <https://spdx.org/licenses/GPL-3.0-only>
|
|
6
|
+
*/
|
|
7
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
8
|
+
if (k2 === undefined) k2 = k;
|
|
9
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
10
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
+
}
|
|
13
|
+
Object.defineProperty(o, k2, desc);
|
|
14
|
+
}) : (function(o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
}));
|
|
18
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
19
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
20
|
+
}) : function(o, v) {
|
|
21
|
+
o["default"] = v;
|
|
22
|
+
});
|
|
23
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
24
|
+
var ownKeys = function(o) {
|
|
25
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
26
|
+
var ar = [];
|
|
27
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
28
|
+
return ar;
|
|
29
|
+
};
|
|
30
|
+
return ownKeys(o);
|
|
31
|
+
};
|
|
32
|
+
return function (mod) {
|
|
33
|
+
if (mod && mod.__esModule) return mod;
|
|
34
|
+
var result = {};
|
|
35
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
36
|
+
__setModuleDefault(result, mod);
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
})();
|
|
40
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
41
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
42
|
+
};
|
|
43
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
/* standard dependencies */
|
|
45
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
46
|
+
const node_stream_1 = __importDefault(require("node:stream"));
|
|
47
|
+
/* external dependencies */
|
|
48
|
+
const Transformers = __importStar(require("@huggingface/transformers"));
|
|
49
|
+
/* internal dependencies */
|
|
50
|
+
const speechflow_node_1 = __importDefault(require("./speechflow-node"));
|
|
51
|
+
/* SpeechFlow node for Transformers text-to-text translation */
|
|
52
|
+
class SpeechFlowNodeTransformers extends speechflow_node_1.default {
|
|
53
|
+
/* declare official node name */
|
|
54
|
+
static name = "transformers";
|
|
55
|
+
/* internal state */
|
|
56
|
+
translator = null;
|
|
57
|
+
generator = null;
|
|
58
|
+
/* internal LLM setup */
|
|
59
|
+
setup = {
|
|
60
|
+
/* SmolLM3: English (EN) to German (DE) translation */
|
|
61
|
+
"SmolLM3:en-de": {
|
|
62
|
+
systemPrompt: "/no_think\n" +
|
|
63
|
+
"You are a translator.\n" +
|
|
64
|
+
"Output only the requested text.\n" +
|
|
65
|
+
"Do not use markdown.\n" +
|
|
66
|
+
"Do not chat.\n" +
|
|
67
|
+
"Do not show any explanations.\n" +
|
|
68
|
+
"Do not show any introduction.\n" +
|
|
69
|
+
"Do not show any preamble.\n" +
|
|
70
|
+
"Do not show any prolog.\n" +
|
|
71
|
+
"Do not show any epilog.\n" +
|
|
72
|
+
"Get to the point.\n" +
|
|
73
|
+
"Preserve the original meaning, tone, and nuance.\n" +
|
|
74
|
+
"Directly translate text from English (EN) to fluent and natural German (DE) language.\n",
|
|
75
|
+
chat: [
|
|
76
|
+
{ role: "user", content: "I love my wife." },
|
|
77
|
+
{ role: "assistant", content: "Ich liebe meine Frau." },
|
|
78
|
+
{ role: "user", content: "The weather is wonderful." },
|
|
79
|
+
{ role: "assistant", content: "Das Wetter ist wunderschön." },
|
|
80
|
+
{ role: "user", content: "The live is awesome." },
|
|
81
|
+
{ role: "assistant", content: "Das Leben ist einfach großartig." }
|
|
82
|
+
]
|
|
83
|
+
},
|
|
84
|
+
/* SmolLM3: German (DE) to English (EN) translation */
|
|
85
|
+
"SmolLM3:de-en": {
|
|
86
|
+
systemPrompt: "/no_think\n" +
|
|
87
|
+
"You are a translator.\n" +
|
|
88
|
+
"Output only the requested text.\n" +
|
|
89
|
+
"Do not use markdown.\n" +
|
|
90
|
+
"Do not chat.\n" +
|
|
91
|
+
"Do not show any explanations.\n" +
|
|
92
|
+
"Do not show any introduction.\n" +
|
|
93
|
+
"Do not show any preamble. \n" +
|
|
94
|
+
"Do not show any prolog. \n" +
|
|
95
|
+
"Do not show any epilog. \n" +
|
|
96
|
+
"Get to the point.\n" +
|
|
97
|
+
"Preserve the original meaning, tone, and nuance.\n" +
|
|
98
|
+
"Directly translate text from German (DE) to fluent and natural English (EN) language.\n",
|
|
99
|
+
chat: [
|
|
100
|
+
{ role: "user", content: "Ich liebe meine Frau." },
|
|
101
|
+
{ role: "assistant", content: "I love my wife." },
|
|
102
|
+
{ role: "user", content: "Das Wetter ist wunderschön." },
|
|
103
|
+
{ role: "assistant", content: "The weather is wonderful." },
|
|
104
|
+
{ role: "user", content: "Das Leben ist einfach großartig." },
|
|
105
|
+
{ role: "assistant", content: "The live is awesome." }
|
|
106
|
+
]
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
/* construct node */
|
|
110
|
+
constructor(id, cfg, opts, args) {
|
|
111
|
+
super(id, cfg, opts, args);
|
|
112
|
+
/* declare node configuration parameters */
|
|
113
|
+
this.configure({
|
|
114
|
+
src: { type: "string", pos: 0, val: "de", match: /^(?:de|en)$/ },
|
|
115
|
+
dst: { type: "string", pos: 1, val: "en", match: /^(?:de|en)$/ },
|
|
116
|
+
model: { type: "string", val: "OPUS", match: /^(?:OPUS|SmolLM3)$/ }
|
|
117
|
+
});
|
|
118
|
+
/* sanity check parameters */
|
|
119
|
+
if (this.params.src === this.params.dst)
|
|
120
|
+
throw new Error("source and destination languages cannot be the same");
|
|
121
|
+
/* declare node input/output format */
|
|
122
|
+
this.input = "text";
|
|
123
|
+
this.output = "text";
|
|
124
|
+
}
|
|
125
|
+
/* open node */
|
|
126
|
+
async open() {
|
|
127
|
+
/* instantiate Transformers engine and model */
|
|
128
|
+
let model = "";
|
|
129
|
+
const progressState = new Map();
|
|
130
|
+
const progressCallback = (progress) => {
|
|
131
|
+
let artifact = model;
|
|
132
|
+
if (typeof progress.file === "string")
|
|
133
|
+
artifact += `:${progress.file}`;
|
|
134
|
+
let percent = 0;
|
|
135
|
+
if (typeof progress.loaded === "number" && typeof progress.total === "number")
|
|
136
|
+
percent = progress.loaded / progress.total * 100;
|
|
137
|
+
else if (typeof progress.progress === "number")
|
|
138
|
+
percent = progress.progress;
|
|
139
|
+
if (percent > 0)
|
|
140
|
+
progressState.set(artifact, percent);
|
|
141
|
+
};
|
|
142
|
+
const interval = setInterval(() => {
|
|
143
|
+
for (const [artifact, percent] of progressState) {
|
|
144
|
+
this.log("info", `downloaded ${percent.toFixed(2)}% of artifact "${artifact}"`);
|
|
145
|
+
if (percent >= 1.0)
|
|
146
|
+
progressState.delete(artifact);
|
|
147
|
+
}
|
|
148
|
+
}, 1000);
|
|
149
|
+
if (this.params.model === "OPUS") {
|
|
150
|
+
model = `onnx-community/opus-mt-${this.params.src}-${this.params.dst}`;
|
|
151
|
+
this.translator = await Transformers.pipeline("translation", model, {
|
|
152
|
+
cache_dir: node_path_1.default.join(this.config.cacheDir, "opus"),
|
|
153
|
+
dtype: "q4",
|
|
154
|
+
device: "gpu",
|
|
155
|
+
progress_callback: progressCallback
|
|
156
|
+
});
|
|
157
|
+
clearInterval(interval);
|
|
158
|
+
if (this.translator === null)
|
|
159
|
+
throw new Error("failed to instantiate translator pipeline");
|
|
160
|
+
}
|
|
161
|
+
else if (this.params.model === "SmolLM3") {
|
|
162
|
+
model = "HuggingFaceTB/SmolLM3-3B-ONNX";
|
|
163
|
+
this.generator = await Transformers.pipeline("text-generation", model, {
|
|
164
|
+
cache_dir: node_path_1.default.join(this.config.cacheDir, "transformers"),
|
|
165
|
+
dtype: "q4",
|
|
166
|
+
device: "gpu",
|
|
167
|
+
progress_callback: progressCallback
|
|
168
|
+
});
|
|
169
|
+
clearInterval(interval);
|
|
170
|
+
if (this.generator === null)
|
|
171
|
+
throw new Error("failed to instantiate generator pipeline");
|
|
172
|
+
}
|
|
173
|
+
else
|
|
174
|
+
throw new Error("invalid model");
|
|
175
|
+
/* provide text-to-text translation */
|
|
176
|
+
const translate = async (text) => {
|
|
177
|
+
if (this.params.model === "OPUS") {
|
|
178
|
+
const result = await this.translator(text);
|
|
179
|
+
return Array.isArray(result) ?
|
|
180
|
+
result[0].translation_text :
|
|
181
|
+
result.translation_text;
|
|
182
|
+
}
|
|
183
|
+
else if (this.params.model === "SmolLM3") {
|
|
184
|
+
const key = `SmolLM3:${this.params.src}-${this.params.dst}`;
|
|
185
|
+
const cfg = this.setup[key];
|
|
186
|
+
const messages = [
|
|
187
|
+
{ role: "system", content: cfg.systemPrompt },
|
|
188
|
+
...cfg.chat,
|
|
189
|
+
{ role: "user", content: text }
|
|
190
|
+
];
|
|
191
|
+
const result = await this.generator(messages, {
|
|
192
|
+
max_new_tokens: 100,
|
|
193
|
+
temperature: 0.6,
|
|
194
|
+
top_p: 0.95,
|
|
195
|
+
streamer: new Transformers.TextStreamer(this.generator.tokenizer, {
|
|
196
|
+
skip_prompt: true,
|
|
197
|
+
skip_special_tokens: true
|
|
198
|
+
})
|
|
199
|
+
});
|
|
200
|
+
const generatedText = Array.isArray(result) ?
|
|
201
|
+
result[0].generated_text :
|
|
202
|
+
result.generated_text;
|
|
203
|
+
const response = typeof generatedText === "string" ?
|
|
204
|
+
generatedText :
|
|
205
|
+
generatedText.at(-1).content;
|
|
206
|
+
return response;
|
|
207
|
+
}
|
|
208
|
+
else
|
|
209
|
+
throw new Error("invalid model");
|
|
210
|
+
};
|
|
211
|
+
/* establish a duplex stream and connect it to Transformers */
|
|
212
|
+
this.stream = new node_stream_1.default.Transform({
|
|
213
|
+
readableObjectMode: true,
|
|
214
|
+
writableObjectMode: true,
|
|
215
|
+
decodeStrings: false,
|
|
216
|
+
transform(chunk, encoding, callback) {
|
|
217
|
+
if (Buffer.isBuffer(chunk.payload))
|
|
218
|
+
callback(new Error("invalid chunk payload type"));
|
|
219
|
+
else {
|
|
220
|
+
if (chunk.payload === "") {
|
|
221
|
+
this.push(chunk);
|
|
222
|
+
callback();
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
translate(chunk.payload).then((payload) => {
|
|
226
|
+
chunk = chunk.clone();
|
|
227
|
+
chunk.payload = payload;
|
|
228
|
+
this.push(chunk);
|
|
229
|
+
callback();
|
|
230
|
+
}).catch((err) => {
|
|
231
|
+
callback(err);
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
final(callback) {
|
|
237
|
+
this.push(null);
|
|
238
|
+
callback();
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
/* close node */
|
|
243
|
+
async close() {
|
|
244
|
+
/* close stream */
|
|
245
|
+
if (this.stream !== null) {
|
|
246
|
+
this.stream.destroy();
|
|
247
|
+
this.stream = null;
|
|
248
|
+
}
|
|
249
|
+
/* shutdown Transformers */
|
|
250
|
+
if (this.translator !== null) {
|
|
251
|
+
this.translator.dispose();
|
|
252
|
+
this.translator = null;
|
|
253
|
+
}
|
|
254
|
+
if (this.generator !== null) {
|
|
255
|
+
this.generator.dispose();
|
|
256
|
+
this.generator = null;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
exports.default = SpeechFlowNodeTransformers;
|
|
@@ -48,7 +48,7 @@ class SpeechFlowNodeTrace extends speechflow_node_1.default {
|
|
|
48
48
|
const fmt = (t) => t.toFormat("hh:mm:ss.SSS");
|
|
49
49
|
if (Buffer.isBuffer(chunk.payload)) {
|
|
50
50
|
if (type === "audio")
|
|
51
|
-
log("
|
|
51
|
+
log("debug", `writing ${type} chunk: start=${fmt(chunk.timestampStart)} ` +
|
|
52
52
|
`end=${fmt(chunk.timestampEnd)} kind=${chunk.kind} type=${chunk.type} ` +
|
|
53
53
|
`payload-type=Buffer payload-bytes=${chunk.payload.byteLength}`);
|
|
54
54
|
else
|
|
@@ -56,7 +56,7 @@ class SpeechFlowNodeTrace extends speechflow_node_1.default {
|
|
|
56
56
|
}
|
|
57
57
|
else {
|
|
58
58
|
if (type === "text")
|
|
59
|
-
log("
|
|
59
|
+
log("debug", `writing ${type} chunk: start=${fmt(chunk.timestampStart)} ` +
|
|
60
60
|
`end=${fmt(chunk.timestampEnd)} kind=${chunk.kind} type=${chunk.type}` +
|
|
61
61
|
`payload-type=String payload-length=${chunk.payload.length} ` +
|
|
62
62
|
`payload-encoding=${encoding} payload-content="${chunk.payload.toString()}"`);
|