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