echogarden 1.4.4 → 1.6.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/data/schemas/options.json +310 -25
- package/dist/alignment/DTWMfccSequenceAlignment.d.ts +1 -1
- package/dist/alignment/DTWMfccSequenceAlignment.js +5 -5
- package/dist/alignment/DTWSequenceAlignmentWindowed.js +1 -3
- package/dist/alignment/DTWSequenceAlignmentWindowed.js.map +1 -1
- package/dist/alignment/{TextAlignment.d.ts → SemanticTextAlignment.d.ts} +4 -2
- package/dist/alignment/SemanticTextAlignment.js +336 -0
- package/dist/alignment/SemanticTextAlignment.js.map +1 -0
- package/dist/alignment/SpeechAlignment.d.ts +4 -3
- package/dist/alignment/SpeechAlignment.js +130 -39
- package/dist/alignment/SpeechAlignment.js.map +1 -1
- package/dist/api/API.d.ts +7 -3
- package/dist/api/API.js +7 -2
- package/dist/api/API.js.map +1 -1
- package/dist/api/APIOptions.d.ts +4 -1
- package/dist/api/Alignment.d.ts +1 -1
- package/dist/api/Alignment.js +13 -5
- package/dist/api/Alignment.js.map +1 -1
- package/dist/api/LanguageDetectionCommon.d.ts +6 -0
- package/dist/api/LanguageDetectionCommon.js +2 -0
- package/dist/api/LanguageDetectionCommon.js.map +1 -0
- package/dist/api/Recognition.js.map +1 -1
- package/dist/api/{LanguageDetection.d.ts → SpeechLanguageDetection.d.ts} +1 -25
- package/dist/api/{LanguageDetection.js → SpeechLanguageDetection.js} +1 -68
- package/dist/api/SpeechLanguageDetection.js.map +1 -0
- package/dist/api/{Translation.js → SpeechTranslation.js} +3 -3
- package/dist/api/SpeechTranslation.js.map +1 -0
- package/dist/api/Synthesis.d.ts +0 -1
- package/dist/api/Synthesis.js +4 -4
- package/dist/api/TextLanguageDetection.d.ts +21 -0
- package/dist/api/TextLanguageDetection.js +67 -0
- package/dist/api/TextLanguageDetection.js.map +1 -0
- package/dist/api/TextTranslation.d.ts +25 -0
- package/dist/api/TextTranslation.js +101 -0
- package/dist/api/TextTranslation.js.map +1 -0
- package/dist/api/TimelineTranslationAlignment.d.ts +23 -0
- package/dist/api/TimelineTranslationAlignment.js +92 -0
- package/dist/api/TimelineTranslationAlignment.js.map +1 -0
- package/dist/api/TranscriptAndTranslationAlignment.d.ts +35 -0
- package/dist/api/TranscriptAndTranslationAlignment.js +78 -0
- package/dist/api/TranscriptAndTranslationAlignment.js.map +1 -0
- package/dist/api/TranslationAlignment.d.ts +4 -3
- package/dist/api/TranslationAlignment.js +9 -8
- package/dist/api/TranslationAlignment.js.map +1 -1
- package/dist/api/VoiceActivityDetection.js +16 -1
- package/dist/api/VoiceActivityDetection.js.map +1 -1
- package/dist/audio/AudioBufferConversion.d.ts +0 -1
- package/dist/audio/AudioPlayer.d.ts +0 -1
- package/dist/audio/AudioPlayer.js +62 -41
- package/dist/audio/AudioPlayer.js.map +1 -1
- package/dist/audio/AudioUtilities.d.ts +0 -1
- package/dist/cli/CLI.d.ts +28 -7
- package/dist/cli/CLI.js +265 -37
- package/dist/cli/CLI.js.map +1 -1
- package/dist/codecs/FFMpegTranscoder.d.ts +0 -1
- package/dist/codecs/FFMpegTranscoder.js +7 -0
- package/dist/codecs/FFMpegTranscoder.js.map +1 -1
- package/dist/codecs/TIMITCodec.d.ts +0 -1
- package/dist/codecs/WaveCodec.d.ts +0 -1
- package/dist/dsp/FFT.d.ts +1 -1
- package/dist/dsp/FFT.js +6 -0
- package/dist/dsp/FFT.js.map +1 -1
- package/dist/dsp/KWeightingFilter.js +1 -1
- package/dist/dsp/KWeightingFilter.js.map +1 -1
- package/dist/dsp/MelSpectogram.d.ts +3 -2
- package/dist/dsp/MelSpectogram.js +14 -8
- package/dist/dsp/MelSpectogram.js.map +1 -1
- package/dist/math/VectorMath.d.ts +9 -9
- package/dist/math/VectorMath.js +10 -10
- package/dist/math/VectorMath.js.map +1 -1
- package/dist/nlp/ChineseSegmentation.js +4 -4
- package/dist/nlp/ChineseSegmentation.js.map +1 -1
- package/dist/nlp/Segmentation.d.ts +2 -2
- package/dist/nlp/Segmentation.js +20 -13
- package/dist/nlp/Segmentation.js.map +1 -1
- package/dist/recognition/OpenAICloudSTT.d.ts +2 -1
- package/dist/recognition/OpenAICloudSTT.js +30 -19
- package/dist/recognition/OpenAICloudSTT.js.map +1 -1
- package/dist/recognition/SileroSTT.d.ts +0 -1
- package/dist/recognition/WhisperCppSTT.d.ts +3 -3
- package/dist/recognition/WhisperCppSTT.js +21 -9
- package/dist/recognition/WhisperCppSTT.js.map +1 -1
- package/dist/recognition/WhisperSTT.d.ts +9 -6
- package/dist/recognition/WhisperSTT.js +227 -46
- package/dist/recognition/WhisperSTT.js.map +1 -1
- package/dist/server/Client.d.ts +3 -4
- package/dist/server/Client.js.map +1 -1
- package/dist/server/Worker.d.ts +3 -3
- package/dist/server/Worker.js +3 -2
- package/dist/server/Worker.js.map +1 -1
- package/dist/source-separation/MDXNetSourceSeparation.d.ts +0 -1
- package/dist/source-separation/MDXNetSourceSeparation.js +1 -1
- package/dist/source-separation/MDXNetSourceSeparation.js.map +1 -1
- package/dist/speech-embeddings/WavToVec2BertFeatureEmbeddings.d.ts +12 -0
- package/dist/speech-embeddings/WavToVec2BertFeatureEmbeddings.js +68 -0
- package/dist/speech-embeddings/WavToVec2BertFeatureEmbeddings.js.map +1 -0
- package/dist/speech-language-detection/SileroLanguageDetection.d.ts +1 -2
- package/dist/speech-language-detection/SileroLanguageDetection.js +1 -1
- package/dist/speech-language-detection/SileroLanguageDetection.js.map +1 -1
- package/dist/subtitles/Subtitles.js +2 -2
- package/dist/subtitles/Subtitles.js.map +1 -1
- package/dist/synthesis/GoogleCloudTTS.d.ts +0 -1
- package/dist/synthesis/GoogleTranslateTTS.d.ts +0 -1
- package/dist/synthesis/GoogleTranslateTTS.js +6 -21
- package/dist/synthesis/GoogleTranslateTTS.js.map +1 -1
- package/dist/synthesis/StreamlabsPollyTTS.d.ts +0 -1
- package/dist/synthesis/VitsTTS.d.ts +0 -1
- package/dist/synthesis/VitsTTS.js +30 -0
- package/dist/synthesis/VitsTTS.js.map +1 -1
- package/dist/tests/Test.js +0 -31
- package/dist/tests/Test.js.map +1 -1
- package/dist/text-language-detection/FastTextLanguageDetection.d.ts +1 -1
- package/dist/text-language-detection/FastTextLanguageDetection.js.map +1 -1
- package/dist/text-language-detection/TinyLDLanguageDetection.d.ts +1 -1
- package/dist/text-language-detection/TinyLDLanguageDetection.js.map +1 -1
- package/dist/text-translation/DeepLTextTranslation.d.ts +2 -0
- package/dist/text-translation/DeepLTextTranslation.js +67 -0
- package/dist/text-translation/DeepLTextTranslation.js.map +1 -0
- package/dist/text-translation/GoogleTranslateTextTranslation.d.ts +10 -0
- package/dist/text-translation/GoogleTranslateTextTranslation.js +554 -0
- package/dist/text-translation/GoogleTranslateTextTranslation.js.map +1 -0
- package/dist/text-translation/NLLBTextTranslation.d.ts +2 -1
- package/dist/text-translation/NLLBTextTranslation.js +249 -19
- package/dist/text-translation/NLLBTextTranslation.js.map +1 -1
- package/dist/utilities/BinaryArrayConversion.d.ts +0 -1
- package/dist/utilities/BrowserRequestHeaders.d.ts +6 -0
- package/dist/utilities/BrowserRequestHeaders.js +52 -0
- package/dist/utilities/BrowserRequestHeaders.js.map +1 -0
- package/dist/utilities/BufferFileReadStream.d.ts +20 -0
- package/dist/utilities/BufferFileReadStream.js +81 -0
- package/dist/utilities/BufferFileReadStream.js.map +1 -0
- package/dist/utilities/DynamicUint8Array.d.ts +9 -0
- package/dist/utilities/DynamicUint8Array.js +31 -0
- package/dist/utilities/DynamicUint8Array.js.map +1 -0
- package/dist/utilities/FileSystem.d.ts +0 -2
- package/dist/utilities/Hashing.d.ts +3 -10
- package/dist/utilities/Hashing.js +10 -127
- package/dist/utilities/Hashing.js.map +1 -1
- package/dist/utilities/LEB128.d.ts +15 -5
- package/dist/utilities/LEB128.js +199 -119
- package/dist/utilities/LEB128.js.map +1 -1
- package/dist/utilities/LPVarInt.d.ts +11 -0
- package/dist/utilities/LPVarInt.js +187 -0
- package/dist/utilities/LPVarInt.js.map +1 -0
- package/dist/utilities/Locale.d.ts +1 -1
- package/dist/utilities/Locale.js +1 -1
- package/dist/utilities/OnnxUtilities.d.ts +1 -2
- package/dist/utilities/PVarInt.d.ts +4 -0
- package/dist/utilities/PVarInt.js +166 -0
- package/dist/utilities/PVarInt.js.map +1 -0
- package/dist/utilities/PackageManager.js +48 -25
- package/dist/utilities/PackageManager.js.map +1 -1
- package/dist/utilities/RandomGenerator.d.ts +3 -17
- package/dist/utilities/RandomGenerator.js +12 -81
- package/dist/utilities/RandomGenerator.js.map +1 -1
- package/dist/utilities/Timeline.d.ts +2 -0
- package/dist/utilities/Timeline.js +129 -20
- package/dist/utilities/Timeline.js.map +1 -1
- package/dist/utilities/Utilities.d.ts +1 -3
- package/dist/utilities/Utilities.js +30 -3
- package/dist/utilities/Utilities.js.map +1 -1
- package/dist/utilities/VarInt.d.ts +4 -0
- package/dist/utilities/VarInt.js +166 -0
- package/dist/utilities/VarInt.js.map +1 -0
- package/dist/utilities/VirtualFileReadStream.d.ts +20 -0
- package/dist/utilities/VirtualFileReadStream.js +79 -0
- package/dist/utilities/VirtualFileReadStream.js.map +1 -0
- package/dist/utilities/WebReader.js +7 -23
- package/dist/utilities/WebReader.js.map +1 -1
- package/dist/voice-activity-detection/SileroVAD.d.ts +0 -1
- package/docs/API.md +105 -3
- package/docs/CLI.md +51 -1
- package/docs/Engines.md +32 -3
- package/docs/Options.md +53 -12
- package/docs/Tasklist.md +1 -13
- package/package.json +20 -24
- package/src/alignment/DTWMfccSequenceAlignment.ts +5 -5
- package/src/alignment/DTWSequenceAlignmentWindowed.ts +1 -3
- package/src/alignment/SemanticTextAlignment.ts +467 -0
- package/src/alignment/SpeechAlignment.ts +214 -56
- package/src/api/API.ts +18 -2
- package/src/api/APIOptions.ts +14 -1
- package/src/api/Alignment.ts +31 -9
- package/src/api/LanguageDetectionCommon.ts +7 -0
- package/src/api/Recognition.ts +2 -0
- package/src/api/{LanguageDetection.ts → SpeechLanguageDetection.ts} +1 -119
- package/src/api/{Translation.ts → SpeechTranslation.ts} +2 -2
- package/src/api/Synthesis.ts +4 -4
- package/src/api/TextLanguageDetection.ts +116 -0
- package/src/api/TextTranslation.ts +177 -0
- package/src/api/TimelineTranslationAlignment.ts +162 -0
- package/src/api/TranscriptAndTranslationAlignment.ts +164 -0
- package/src/api/TranslationAlignment.ts +12 -10
- package/src/api/VoiceActivityDetection.ts +24 -3
- package/src/audio/AudioPlayer.ts +2 -0
- package/src/cli/CLI.ts +376 -40
- package/src/codecs/FFMpegTranscoder.ts +6 -0
- package/src/dsp/FFT.ts +8 -2
- package/src/dsp/KWeightingFilter.ts +1 -1
- package/src/dsp/MelSpectogram.ts +17 -8
- package/src/math/VectorMath.ts +15 -15
- package/src/nlp/ChineseSegmentation.ts +6 -4
- package/src/nlp/Segmentation.ts +18 -13
- package/src/recognition/OpenAICloudSTT.ts +47 -29
- package/src/recognition/WhisperCppSTT.ts +26 -11
- package/src/recognition/WhisperSTT.ts +364 -49
- package/src/server/Client.ts +3 -2
- package/src/server/Worker.ts +3 -2
- package/src/source-separation/MDXNetSourceSeparation.ts +1 -1
- package/src/speech-embeddings/WavToVec2BertFeatureEmbeddings.ts +107 -0
- package/src/speech-language-detection/SileroLanguageDetection.ts +2 -1
- package/src/subtitles/Subtitles.ts +2 -2
- package/src/synthesis/GoogleTranslateTTS.ts +7 -21
- package/src/synthesis/VitsTTS.ts +31 -3
- package/src/tests/Test.ts +1 -38
- package/src/text-language-detection/FastTextLanguageDetection.ts +1 -1
- package/src/text-language-detection/TinyLDLanguageDetection.ts +1 -1
- package/src/text-translation/DeepLTextTranslation.ts +88 -0
- package/src/text-translation/GoogleTranslateTextTranslation.ts +667 -0
- package/src/text-translation/NLLBTextTranslation.ts +261 -21
- package/src/typings/Fillers.d.ts +25 -2
- package/src/utilities/BrowserRequestHeaders.ts +59 -0
- package/src/utilities/DynamicUint8Array.ts +39 -0
- package/src/utilities/Hashing.ts +14 -167
- package/src/utilities/LEB128.ts +273 -148
- package/src/utilities/LPVarInt.ts +292 -0
- package/src/utilities/Locale.ts +1 -1
- package/src/utilities/OnnxUtilities.ts +1 -1
- package/src/utilities/PackageManager.ts +51 -30
- package/src/utilities/RandomGenerator.ts +12 -113
- package/src/utilities/Timeline.ts +162 -23
- package/src/utilities/Utilities.ts +40 -3
- package/src/utilities/VirtualFileReadStream.ts +109 -0
- package/src/utilities/WebReader.ts +9 -23
- package/dist/alignment/TextAlignment.js +0 -156
- package/dist/alignment/TextAlignment.js.map +0 -1
- package/dist/api/LanguageDetection.js.map +0 -1
- package/dist/api/Translation.js.map +0 -1
- package/src/alignment/TextAlignment.ts +0 -234
- /package/dist/api/{Translation.d.ts → SpeechTranslation.d.ts} +0 -0
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import { type PreTrainedModel, type PreTrainedTokenizer } from '@echogarden/transformers-nodejs-lite'
|
|
2
|
-
import { Logger } from '../utilities/Logger.js'
|
|
3
|
-
import { loadPackage } from '../utilities/PackageManager.js'
|
|
4
|
-
import { alignDTWWindowed } from './DTWSequenceAlignmentWindowed.js'
|
|
5
|
-
import { cosineDistance } from '../math/VectorMath.js'
|
|
6
|
-
import { isPunctuation, splitToWords } from '../nlp/Segmentation.js'
|
|
7
|
-
|
|
8
|
-
export async function alignText(text1: string, langCode1: string, text2: string, langCode2: string) {
|
|
9
|
-
const logger = new Logger()
|
|
10
|
-
|
|
11
|
-
text1 = text1.replaceAll(/\s+/g, ' ')
|
|
12
|
-
text2 = text2.replaceAll(/\s+/g, ' ')
|
|
13
|
-
|
|
14
|
-
const words1 = await splitToWords(text1, langCode1)
|
|
15
|
-
const words2 = await splitToWords(text2, langCode2)
|
|
16
|
-
|
|
17
|
-
const wordAlignment = await alignWords(words1, words2)
|
|
18
|
-
|
|
19
|
-
const x = 0
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export async function alignWords(words1: string[], words2: string[]) {
|
|
23
|
-
const logger = new Logger()
|
|
24
|
-
|
|
25
|
-
// Join words and get offsets
|
|
26
|
-
const { joinedText: text1, offsets: text1Offsets } = joinAndGetOffsets(words1)
|
|
27
|
-
const { joinedText: text2, offsets: text2Offsets } = joinAndGetOffsets(words2)
|
|
28
|
-
|
|
29
|
-
// Load embedding model
|
|
30
|
-
const modelPath = await loadPackage(`xenova-multilingual-e5-base-quantized`)
|
|
31
|
-
|
|
32
|
-
const embeddingModel = new E5TextEmbedding(modelPath)
|
|
33
|
-
|
|
34
|
-
// Process text 1:
|
|
35
|
-
logger.start(`Initialize E5 embedding model`)
|
|
36
|
-
await embeddingModel.initializeIfNeeded()
|
|
37
|
-
|
|
38
|
-
logger.start(`Tokenize text 1`)
|
|
39
|
-
const inputs1 = await embeddingModel.tokenizeToModelInputs(text1)
|
|
40
|
-
|
|
41
|
-
logger.start(`Infer embeddings for text 1`)
|
|
42
|
-
const embeddings1 = await embeddingModel.inferTokenEmbeddings(inputs1)
|
|
43
|
-
|
|
44
|
-
logger.start(`Compute token to word mapping for text 1`)
|
|
45
|
-
const filteredEmbeddings1 = embeddings1.filter((embedding) => embedding.text !== '▁' && embedding.text !== '<s>' && embedding.text !== '</s>')
|
|
46
|
-
const tokenToWordIndexMapping1 = mapTokenEmbeddingsToWordIndexes(filteredEmbeddings1, text1, text1Offsets)
|
|
47
|
-
|
|
48
|
-
// Process text 2:
|
|
49
|
-
logger.start(`Tokenize text 2`)
|
|
50
|
-
const inputs2 = await embeddingModel.tokenizeToModelInputs(text2)
|
|
51
|
-
|
|
52
|
-
logger.start(`Infer embeddings for text 2`)
|
|
53
|
-
const embeddings2 = await embeddingModel.inferTokenEmbeddings(inputs2)
|
|
54
|
-
|
|
55
|
-
logger.start(`Compute token to word mapping for text 2`)
|
|
56
|
-
const filteredEmbeddings2 = embeddings2.filter((embedding) => embedding.text !== '▁' && embedding.text !== '<s>' && embedding.text !== '</s>')
|
|
57
|
-
const tokenToWordIndexMapping2 = mapTokenEmbeddingsToWordIndexes(filteredEmbeddings2, text2, text2Offsets)
|
|
58
|
-
|
|
59
|
-
logger.end()
|
|
60
|
-
|
|
61
|
-
// Align
|
|
62
|
-
function costFunction(a: TokenEmbeddingData, b: TokenEmbeddingData) {
|
|
63
|
-
const aIsPunctuation = isPunctuation(a.text)
|
|
64
|
-
const bIsPunctuation = isPunctuation(b.text)
|
|
65
|
-
|
|
66
|
-
if (aIsPunctuation === bIsPunctuation) {
|
|
67
|
-
return cosineDistance(a.embeddingVector, b.embeddingVector)
|
|
68
|
-
} else {
|
|
69
|
-
return 1.0
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const { path } = alignDTWWindowed(filteredEmbeddings1, filteredEmbeddings2, costFunction, 1000 * 1000)
|
|
74
|
-
|
|
75
|
-
// Use alignment path to words to words
|
|
76
|
-
const wordMapping: WordMapping[] = []
|
|
77
|
-
|
|
78
|
-
for (let i = 0; i < path.length; i++) {
|
|
79
|
-
const pathEntry = path[i]
|
|
80
|
-
|
|
81
|
-
const sourceTokenIndex = pathEntry.source
|
|
82
|
-
const destTokenIndex = pathEntry.dest
|
|
83
|
-
|
|
84
|
-
const mappedWordIndex1 = tokenToWordIndexMapping1[sourceTokenIndex]
|
|
85
|
-
const mappedWordIndex2 = tokenToWordIndexMapping2[destTokenIndex]
|
|
86
|
-
|
|
87
|
-
wordMapping.push({
|
|
88
|
-
wordIndex1: mappedWordIndex1,
|
|
89
|
-
word1: words1[mappedWordIndex1],
|
|
90
|
-
wordIndex2: mappedWordIndex2,
|
|
91
|
-
word2: words2[mappedWordIndex2],
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
const embedding1 = filteredEmbeddings1[sourceTokenIndex]
|
|
95
|
-
const embedding2 = filteredEmbeddings2[destTokenIndex]
|
|
96
|
-
|
|
97
|
-
logger.log(`${embedding1.text} -> ${embedding2.text}`)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return wordMapping
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
function mapTokenEmbeddingsToWordIndexes(embeddings: TokenEmbeddingData[], text: string, textWordOffsets: number[]) {
|
|
105
|
-
const tokenToWordIndex: number[] = []
|
|
106
|
-
|
|
107
|
-
let currentTextOffset = 0
|
|
108
|
-
|
|
109
|
-
for (let i = 0; i < embeddings.length; i++) {
|
|
110
|
-
const embedding = embeddings[i]
|
|
111
|
-
let tokenText = embedding.text
|
|
112
|
-
|
|
113
|
-
if (tokenText === '<s>' || tokenText === '</s>') {
|
|
114
|
-
tokenToWordIndex.push(-1)
|
|
115
|
-
|
|
116
|
-
continue
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (tokenText.startsWith('▁')) {
|
|
120
|
-
tokenText = tokenText.substring(1)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const matchPosition = text.indexOf(tokenText, currentTextOffset)
|
|
124
|
-
|
|
125
|
-
if (matchPosition === -1) {
|
|
126
|
-
throw new Error(`Token '${tokenText}' not found in text`)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
currentTextOffset = matchPosition + tokenText.length
|
|
130
|
-
|
|
131
|
-
let tokenMatchingWordIndex = textWordOffsets.findIndex((index) => index > matchPosition)
|
|
132
|
-
|
|
133
|
-
if (tokenMatchingWordIndex === -1) {
|
|
134
|
-
throw new Error(`Token '${tokenText}' not found in text`)
|
|
135
|
-
} else {
|
|
136
|
-
tokenMatchingWordIndex = Math.max(tokenMatchingWordIndex - 1, 0)
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
tokenToWordIndex.push(tokenMatchingWordIndex)
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return tokenToWordIndex
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
function joinAndGetOffsets(words: string[]) {
|
|
147
|
-
let joinedText = ''
|
|
148
|
-
const offsets: number[] = []
|
|
149
|
-
|
|
150
|
-
let offset = 0
|
|
151
|
-
|
|
152
|
-
for (const word of words) {
|
|
153
|
-
joinedText += word
|
|
154
|
-
|
|
155
|
-
offsets.push(offset)
|
|
156
|
-
|
|
157
|
-
offset += word.length
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
offsets.push(joinedText.length)
|
|
161
|
-
|
|
162
|
-
return { joinedText, offsets }
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export class E5TextEmbedding {
|
|
166
|
-
tokenizer?: PreTrainedTokenizer
|
|
167
|
-
model?: PreTrainedModel
|
|
168
|
-
|
|
169
|
-
constructor(public readonly modelPath: string) {
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
async tokenizeToModelInputs(text: string) {
|
|
173
|
-
await this.initializeIfNeeded()
|
|
174
|
-
|
|
175
|
-
const inputs = await this.tokenizer!(text)
|
|
176
|
-
|
|
177
|
-
return inputs
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
async inferTokenEmbeddings(inputs: any) {
|
|
181
|
-
await this.initializeIfNeeded()
|
|
182
|
-
|
|
183
|
-
const tokensText = this.tokenizer!.model.convert_ids_to_tokens(Array.from(inputs.input_ids.data))
|
|
184
|
-
|
|
185
|
-
const result = await this.model!(inputs)
|
|
186
|
-
|
|
187
|
-
const lastHiddenState = result.last_hidden_state
|
|
188
|
-
|
|
189
|
-
const tokenCount = lastHiddenState.dims[1]
|
|
190
|
-
const embeddingSize = lastHiddenState.dims[2]
|
|
191
|
-
|
|
192
|
-
const tokenEmbeddings: TokenEmbeddingData[] = []
|
|
193
|
-
|
|
194
|
-
for (let i = 0; i < tokenCount; i++) {
|
|
195
|
-
const tokenEmbeddingVector = lastHiddenState.data.slice(i * embeddingSize, (i + 1) * embeddingSize)
|
|
196
|
-
|
|
197
|
-
const tokenId = Number(inputs.input_ids.data[i])
|
|
198
|
-
const tokenText = tokensText[i]
|
|
199
|
-
|
|
200
|
-
tokenEmbeddings.push({
|
|
201
|
-
id: tokenId,
|
|
202
|
-
text: tokenText,
|
|
203
|
-
embeddingVector: tokenEmbeddingVector
|
|
204
|
-
})
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
return tokenEmbeddings
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
async initializeIfNeeded() {
|
|
211
|
-
if (this.tokenizer && this.model) {
|
|
212
|
-
return
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
const { AutoTokenizer, AutoModel } = await import('@echogarden/transformers-nodejs-lite')
|
|
216
|
-
|
|
217
|
-
this.tokenizer = await AutoTokenizer.from_pretrained(this.modelPath)
|
|
218
|
-
this.model = await AutoModel.from_pretrained(this.modelPath)
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
export interface TokenEmbeddingData {
|
|
223
|
-
id: number
|
|
224
|
-
text: string
|
|
225
|
-
embeddingVector: Float32Array
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
export interface WordMapping {
|
|
229
|
-
wordIndex1: number
|
|
230
|
-
word1: string
|
|
231
|
-
|
|
232
|
-
wordIndex2: number
|
|
233
|
-
word2: string
|
|
234
|
-
}
|
|
File without changes
|