react-native-executorch 0.5.1 → 0.5.2
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/lib/Error.d.ts +30 -0
- package/lib/Error.js +50 -0
- package/lib/common/Logger.d.ts +8 -0
- package/lib/common/Logger.js +19 -0
- package/lib/constants/directories.d.ts +1 -0
- package/lib/constants/directories.js +2 -0
- package/lib/constants/llmDefaults.d.ts +6 -0
- package/lib/constants/llmDefaults.js +16 -0
- package/lib/constants/modelUrls.d.ts +223 -0
- package/lib/constants/modelUrls.js +322 -0
- package/lib/constants/ocr/models.d.ts +882 -0
- package/lib/constants/ocr/models.js +182 -0
- package/lib/constants/ocr/symbols.d.ts +75 -0
- package/lib/constants/ocr/symbols.js +139 -0
- package/lib/constants/sttDefaults.d.ts +28 -0
- package/lib/constants/sttDefaults.js +68 -0
- package/lib/controllers/LLMController.d.ts +47 -0
- package/lib/controllers/LLMController.js +213 -0
- package/lib/controllers/OCRController.d.ts +23 -0
- package/lib/controllers/OCRController.js +72 -0
- package/lib/controllers/SpeechToTextController.d.ts +56 -0
- package/lib/controllers/SpeechToTextController.js +349 -0
- package/lib/controllers/VerticalOCRController.d.ts +25 -0
- package/lib/controllers/VerticalOCRController.js +75 -0
- package/lib/hooks/computer_vision/useClassification.d.ts +15 -0
- package/lib/hooks/computer_vision/useClassification.js +7 -0
- package/lib/hooks/computer_vision/useImageEmbeddings.d.ts +15 -0
- package/lib/hooks/computer_vision/useImageEmbeddings.js +7 -0
- package/lib/hooks/computer_vision/useImageSegmentation.d.ts +38 -0
- package/lib/hooks/computer_vision/useImageSegmentation.js +7 -0
- package/lib/hooks/computer_vision/useOCR.d.ts +20 -0
- package/lib/hooks/computer_vision/useOCR.js +42 -0
- package/lib/hooks/computer_vision/useObjectDetection.d.ts +15 -0
- package/lib/hooks/computer_vision/useObjectDetection.js +7 -0
- package/lib/hooks/computer_vision/useStyleTransfer.d.ts +15 -0
- package/lib/hooks/computer_vision/useStyleTransfer.js +7 -0
- package/lib/hooks/computer_vision/useVerticalOCR.d.ts +21 -0
- package/lib/hooks/computer_vision/useVerticalOCR.js +45 -0
- package/lib/hooks/general/useExecutorchModule.d.ts +13 -0
- package/lib/hooks/general/useExecutorchModule.js +7 -0
- package/lib/hooks/natural_language_processing/useLLM.d.ts +10 -0
- package/lib/hooks/natural_language_processing/useLLM.js +78 -0
- package/lib/hooks/natural_language_processing/useSpeechToText.d.ts +27 -0
- package/lib/hooks/natural_language_processing/useSpeechToText.js +49 -0
- package/lib/hooks/natural_language_processing/useTextEmbeddings.d.ts +16 -0
- package/lib/hooks/natural_language_processing/useTextEmbeddings.js +7 -0
- package/lib/hooks/natural_language_processing/useTokenizer.d.ts +17 -0
- package/lib/hooks/natural_language_processing/useTokenizer.js +52 -0
- package/lib/hooks/useModule.d.ts +17 -0
- package/lib/hooks/useModule.js +45 -0
- package/lib/hooks/useNonStaticModule.d.ts +20 -0
- package/lib/hooks/useNonStaticModule.js +49 -0
- package/lib/index.d.ts +50 -0
- package/lib/index.js +60 -0
- package/lib/module/utils/ResourceFetcher.js +6 -8
- package/lib/module/utils/ResourceFetcher.js.map +1 -1
- package/lib/module/utils/ResourceFetcherUtils.js +20 -20
- package/lib/module/utils/ResourceFetcherUtils.js.map +1 -1
- package/lib/modules/BaseModule.d.ts +8 -0
- package/lib/modules/BaseModule.js +25 -0
- package/lib/modules/BaseNonStaticModule.d.ts +9 -0
- package/lib/modules/BaseNonStaticModule.js +14 -0
- package/lib/modules/computer_vision/ClassificationModule.d.ts +8 -0
- package/lib/modules/computer_vision/ClassificationModule.js +17 -0
- package/lib/modules/computer_vision/ImageEmbeddingsModule.d.ts +8 -0
- package/lib/modules/computer_vision/ImageEmbeddingsModule.js +17 -0
- package/lib/modules/computer_vision/ImageSegmentationModule.d.ts +11 -0
- package/lib/modules/computer_vision/ImageSegmentationModule.js +27 -0
- package/lib/modules/computer_vision/OCRModule.d.ts +15 -0
- package/lib/modules/computer_vision/OCRModule.js +20 -0
- package/lib/modules/computer_vision/ObjectDetectionModule.d.ts +9 -0
- package/lib/modules/computer_vision/ObjectDetectionModule.js +17 -0
- package/lib/modules/computer_vision/StyleTransferModule.d.ts +8 -0
- package/lib/modules/computer_vision/StyleTransferModule.js +17 -0
- package/lib/modules/computer_vision/VerticalOCRModule.d.ts +15 -0
- package/lib/modules/computer_vision/VerticalOCRModule.js +22 -0
- package/lib/modules/general/ExecutorchModule.d.ts +7 -0
- package/lib/modules/general/ExecutorchModule.js +14 -0
- package/lib/modules/natural_language_processing/LLMModule.d.ts +28 -0
- package/lib/modules/natural_language_processing/LLMModule.js +45 -0
- package/lib/modules/natural_language_processing/SpeechToTextModule.d.ts +24 -0
- package/lib/modules/natural_language_processing/SpeechToTextModule.js +36 -0
- package/lib/modules/natural_language_processing/TextEmbeddingsModule.d.ts +9 -0
- package/lib/modules/natural_language_processing/TextEmbeddingsModule.js +21 -0
- package/lib/modules/natural_language_processing/TokenizerModule.d.ts +12 -0
- package/lib/modules/natural_language_processing/TokenizerModule.js +30 -0
- package/lib/native/NativeETInstaller.d.ts +6 -0
- package/lib/native/NativeETInstaller.js +2 -0
- package/lib/native/NativeOCR.d.ts +8 -0
- package/lib/native/NativeOCR.js +2 -0
- package/lib/native/NativeVerticalOCR.d.ts +8 -0
- package/lib/native/NativeVerticalOCR.js +2 -0
- package/lib/native/RnExecutorchModules.d.ts +3 -0
- package/lib/native/RnExecutorchModules.js +16 -0
- package/lib/types/common.d.ts +31 -0
- package/lib/types/common.js +25 -0
- package/lib/types/imageSegmentation.d.ts +24 -0
- package/lib/types/imageSegmentation.js +26 -0
- package/lib/types/llm.d.ts +46 -0
- package/lib/types/llm.js +9 -0
- package/lib/types/objectDetection.d.ts +104 -0
- package/lib/types/objectDetection.js +94 -0
- package/lib/types/ocr.d.ts +11 -0
- package/lib/types/ocr.js +1 -0
- package/lib/types/stt.d.ts +94 -0
- package/lib/types/stt.js +85 -0
- package/lib/typescript/utils/ResourceFetcher.d.ts.map +1 -1
- package/lib/typescript/utils/ResourceFetcherUtils.d.ts.map +1 -1
- package/lib/utils/ResourceFetcher.d.ts +24 -0
- package/lib/utils/ResourceFetcher.js +305 -0
- package/lib/utils/ResourceFetcherUtils.d.ts +54 -0
- package/lib/utils/ResourceFetcherUtils.js +128 -0
- package/lib/utils/llm.d.ts +6 -0
- package/lib/utils/llm.js +73 -0
- package/lib/utils/stt.d.ts +1 -0
- package/lib/utils/stt.js +21 -0
- package/package.json +1 -1
- package/src/utils/ResourceFetcher.ts +9 -7
- package/src/utils/ResourceFetcherUtils.ts +15 -17
- package/ios/RnExecutorch.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
- package/ios/RnExecutorch.xcodeproj/project.xcworkspace/xcuserdata/jakubchmura.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
- package/ios/RnExecutorch.xcodeproj/xcuserdata/jakubchmura.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
- package/lib/tsconfig.tsbuildinfo +0 -1
- package/third-party/ios/ExecutorchLib/ExecutorchLib.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
- package/third-party/ios/ExecutorchLib/ExecutorchLib.xcodeproj/project.xcworkspace/xcuserdata/jakubchmura.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
- package/third-party/ios/ExecutorchLib/ExecutorchLib.xcodeproj/xcuserdata/jakubchmura.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { ResourceFetcher } from '../utils/ResourceFetcher';
|
|
2
|
+
import { ETError, getError } from '../Error';
|
|
3
|
+
import { Template } from '@huggingface/jinja';
|
|
4
|
+
import { DEFAULT_CHAT_CONFIG } from '../constants/llmDefaults';
|
|
5
|
+
import { readAsStringAsync } from 'expo-file-system';
|
|
6
|
+
import { SPECIAL_TOKENS, } from '../types/llm';
|
|
7
|
+
import { parseToolCall } from '../utils/llm';
|
|
8
|
+
import { Logger } from '../common/Logger';
|
|
9
|
+
export class LLMController {
|
|
10
|
+
nativeModule;
|
|
11
|
+
chatConfig = DEFAULT_CHAT_CONFIG;
|
|
12
|
+
toolsConfig;
|
|
13
|
+
tokenizerConfig;
|
|
14
|
+
onToken;
|
|
15
|
+
_response = '';
|
|
16
|
+
_isReady = false;
|
|
17
|
+
_isGenerating = false;
|
|
18
|
+
_messageHistory = [];
|
|
19
|
+
// User callbacks
|
|
20
|
+
tokenCallback;
|
|
21
|
+
responseCallback;
|
|
22
|
+
messageHistoryCallback;
|
|
23
|
+
isReadyCallback;
|
|
24
|
+
isGeneratingCallback;
|
|
25
|
+
constructor({ tokenCallback, responseCallback, messageHistoryCallback, isReadyCallback, isGeneratingCallback, }) {
|
|
26
|
+
if (responseCallback !== undefined) {
|
|
27
|
+
Logger.warn('Passing response callback is deprecated and will be removed in 0.6.0');
|
|
28
|
+
}
|
|
29
|
+
this.tokenCallback = (token) => {
|
|
30
|
+
tokenCallback?.(token);
|
|
31
|
+
};
|
|
32
|
+
this.responseCallback = (response) => {
|
|
33
|
+
this._response = response;
|
|
34
|
+
responseCallback?.(response);
|
|
35
|
+
};
|
|
36
|
+
this.messageHistoryCallback = (messageHistory) => {
|
|
37
|
+
this._messageHistory = messageHistory;
|
|
38
|
+
messageHistoryCallback?.(messageHistory);
|
|
39
|
+
};
|
|
40
|
+
this.isReadyCallback = (isReady) => {
|
|
41
|
+
this._isReady = isReady;
|
|
42
|
+
isReadyCallback?.(isReady);
|
|
43
|
+
};
|
|
44
|
+
this.isGeneratingCallback = (isGenerating) => {
|
|
45
|
+
this._isGenerating = isGenerating;
|
|
46
|
+
isGeneratingCallback?.(isGenerating);
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
get response() {
|
|
50
|
+
return this._response;
|
|
51
|
+
}
|
|
52
|
+
get isReady() {
|
|
53
|
+
return this._isReady;
|
|
54
|
+
}
|
|
55
|
+
get isGenerating() {
|
|
56
|
+
return this._isGenerating;
|
|
57
|
+
}
|
|
58
|
+
get messageHistory() {
|
|
59
|
+
return this._messageHistory;
|
|
60
|
+
}
|
|
61
|
+
async load({ modelSource, tokenizerSource, tokenizerConfigSource, onDownloadProgressCallback, }) {
|
|
62
|
+
// reset inner state when loading new model
|
|
63
|
+
this.responseCallback('');
|
|
64
|
+
this.messageHistoryCallback(this.chatConfig.initialMessageHistory);
|
|
65
|
+
this.isGeneratingCallback(false);
|
|
66
|
+
this.isReadyCallback(false);
|
|
67
|
+
try {
|
|
68
|
+
const tokenizersPromise = ResourceFetcher.fetch(undefined, tokenizerSource, tokenizerConfigSource);
|
|
69
|
+
const modelPromise = ResourceFetcher.fetch(onDownloadProgressCallback, modelSource);
|
|
70
|
+
const [tokenizersResults, modelResult] = await Promise.all([
|
|
71
|
+
tokenizersPromise,
|
|
72
|
+
modelPromise,
|
|
73
|
+
]);
|
|
74
|
+
const tokenizerPath = tokenizersResults?.[0];
|
|
75
|
+
const tokenizerConfigPath = tokenizersResults?.[1];
|
|
76
|
+
const modelPath = modelResult?.[0];
|
|
77
|
+
if (!tokenizerPath || !tokenizerConfigPath || !modelPath) {
|
|
78
|
+
throw new Error('Download interrupted!');
|
|
79
|
+
}
|
|
80
|
+
this.tokenizerConfig = JSON.parse(await readAsStringAsync('file://' + tokenizerConfigPath));
|
|
81
|
+
this.nativeModule = global.loadLLM(modelPath, tokenizerPath);
|
|
82
|
+
this.isReadyCallback(true);
|
|
83
|
+
this.onToken = (data) => {
|
|
84
|
+
if (!data ||
|
|
85
|
+
(SPECIAL_TOKENS.EOS_TOKEN in this.tokenizerConfig &&
|
|
86
|
+
data === this.tokenizerConfig.eos_token) ||
|
|
87
|
+
(SPECIAL_TOKENS.PAD_TOKEN in this.tokenizerConfig &&
|
|
88
|
+
data === this.tokenizerConfig.pad_token)) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
this.tokenCallback(data);
|
|
92
|
+
this.responseCallback(this._response + data);
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
catch (e) {
|
|
96
|
+
this.isReadyCallback(false);
|
|
97
|
+
throw new Error(getError(e));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
setTokenCallback(tokenCallback) {
|
|
101
|
+
this.tokenCallback = tokenCallback;
|
|
102
|
+
}
|
|
103
|
+
configure({ chatConfig, toolsConfig, }) {
|
|
104
|
+
this.chatConfig = { ...DEFAULT_CHAT_CONFIG, ...chatConfig };
|
|
105
|
+
this.toolsConfig = toolsConfig;
|
|
106
|
+
// reset inner state when loading new configuration
|
|
107
|
+
this.responseCallback('');
|
|
108
|
+
this.messageHistoryCallback(this.chatConfig.initialMessageHistory);
|
|
109
|
+
this.isGeneratingCallback(false);
|
|
110
|
+
}
|
|
111
|
+
delete() {
|
|
112
|
+
if (this._isGenerating) {
|
|
113
|
+
throw new Error(getError(ETError.ModelGenerating) +
|
|
114
|
+
'You cannot delete the model now. You need to interrupt first.');
|
|
115
|
+
}
|
|
116
|
+
this.onToken = () => { };
|
|
117
|
+
this.nativeModule.unload();
|
|
118
|
+
this.isReadyCallback(false);
|
|
119
|
+
this.isGeneratingCallback(false);
|
|
120
|
+
}
|
|
121
|
+
async forward(input) {
|
|
122
|
+
if (!this._isReady) {
|
|
123
|
+
throw new Error(getError(ETError.ModuleNotLoaded));
|
|
124
|
+
}
|
|
125
|
+
if (this._isGenerating) {
|
|
126
|
+
throw new Error(getError(ETError.ModelGenerating));
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
this.responseCallback('');
|
|
130
|
+
this.isGeneratingCallback(true);
|
|
131
|
+
await this.nativeModule.generate(input, this.onToken);
|
|
132
|
+
}
|
|
133
|
+
catch (e) {
|
|
134
|
+
throw new Error(getError(e));
|
|
135
|
+
}
|
|
136
|
+
finally {
|
|
137
|
+
this.isGeneratingCallback(false);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
interrupt() {
|
|
141
|
+
this.nativeModule.interrupt();
|
|
142
|
+
}
|
|
143
|
+
async generate(messages, tools) {
|
|
144
|
+
if (!this._isReady) {
|
|
145
|
+
throw new Error(getError(ETError.ModuleNotLoaded));
|
|
146
|
+
}
|
|
147
|
+
if (messages.length === 0) {
|
|
148
|
+
throw new Error(`Empty 'messages' array!`);
|
|
149
|
+
}
|
|
150
|
+
if (messages[0] && messages[0].role !== 'system') {
|
|
151
|
+
Logger.warn(`You are not providing system prompt. You can pass it in the first message using { role: 'system', content: YOUR_PROMPT }. Otherwise prompt from your model's chat template will be used.`);
|
|
152
|
+
}
|
|
153
|
+
const renderedChat = this.applyChatTemplate(messages, this.tokenizerConfig, tools,
|
|
154
|
+
// eslint-disable-next-line camelcase
|
|
155
|
+
{ tools_in_user_message: false, add_generation_prompt: true });
|
|
156
|
+
await this.forward(renderedChat);
|
|
157
|
+
}
|
|
158
|
+
async sendMessage(message) {
|
|
159
|
+
this.messageHistoryCallback([
|
|
160
|
+
...this._messageHistory,
|
|
161
|
+
{ content: message, role: 'user' },
|
|
162
|
+
]);
|
|
163
|
+
const messageHistoryWithPrompt = [
|
|
164
|
+
{ content: this.chatConfig.systemPrompt, role: 'system' },
|
|
165
|
+
...this._messageHistory.slice(-this.chatConfig.contextWindowLength),
|
|
166
|
+
];
|
|
167
|
+
await this.generate(messageHistoryWithPrompt, this.toolsConfig?.tools);
|
|
168
|
+
if (!this.toolsConfig || this.toolsConfig.displayToolCalls) {
|
|
169
|
+
this.messageHistoryCallback([
|
|
170
|
+
...this._messageHistory,
|
|
171
|
+
{ content: this._response, role: 'assistant' },
|
|
172
|
+
]);
|
|
173
|
+
}
|
|
174
|
+
if (!this.toolsConfig) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
const toolCalls = parseToolCall(this._response);
|
|
178
|
+
for (const toolCall of toolCalls) {
|
|
179
|
+
this.toolsConfig
|
|
180
|
+
.executeToolCallback(toolCall)
|
|
181
|
+
.then((toolResponse) => {
|
|
182
|
+
if (toolResponse) {
|
|
183
|
+
this.messageHistoryCallback([
|
|
184
|
+
...this._messageHistory,
|
|
185
|
+
{ content: toolResponse, role: 'assistant' },
|
|
186
|
+
]);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
deleteMessage(index) {
|
|
192
|
+
// we delete referenced message and all messages after it
|
|
193
|
+
// so the model responses that used them are deleted as well
|
|
194
|
+
const newMessageHistory = this._messageHistory.slice(0, index);
|
|
195
|
+
this.messageHistoryCallback(newMessageHistory);
|
|
196
|
+
}
|
|
197
|
+
applyChatTemplate(messages, tokenizerConfig, tools, templateFlags) {
|
|
198
|
+
if (!tokenizerConfig.chat_template) {
|
|
199
|
+
throw Error("Tokenizer config doesn't include chat_template");
|
|
200
|
+
}
|
|
201
|
+
const template = new Template(tokenizerConfig.chat_template);
|
|
202
|
+
const specialTokens = Object.fromEntries(Object.keys(SPECIAL_TOKENS)
|
|
203
|
+
.filter((key) => key in tokenizerConfig)
|
|
204
|
+
.map((key) => [key, tokenizerConfig[key]]));
|
|
205
|
+
const result = template.render({
|
|
206
|
+
messages,
|
|
207
|
+
tools,
|
|
208
|
+
...templateFlags,
|
|
209
|
+
...specialTokens,
|
|
210
|
+
});
|
|
211
|
+
return result;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ResourceSource } from '../types/common';
|
|
2
|
+
import { OCRLanguage } from '../types/ocr';
|
|
3
|
+
export declare class OCRController {
|
|
4
|
+
private nativeModule;
|
|
5
|
+
isReady: boolean;
|
|
6
|
+
isGenerating: boolean;
|
|
7
|
+
error: string | null;
|
|
8
|
+
private isReadyCallback;
|
|
9
|
+
private isGeneratingCallback;
|
|
10
|
+
private errorCallback;
|
|
11
|
+
constructor({ isReadyCallback, isGeneratingCallback, errorCallback, }?: {
|
|
12
|
+
isReadyCallback?: ((_isReady: boolean) => void) | undefined;
|
|
13
|
+
isGeneratingCallback?: ((_isGenerating: boolean) => void) | undefined;
|
|
14
|
+
errorCallback?: ((_error: string) => void) | undefined;
|
|
15
|
+
});
|
|
16
|
+
load: (detectorSource: ResourceSource, recognizerSources: {
|
|
17
|
+
recognizerLarge: ResourceSource;
|
|
18
|
+
recognizerMedium: ResourceSource;
|
|
19
|
+
recognizerSmall: ResourceSource;
|
|
20
|
+
}, language: OCRLanguage, onDownloadProgressCallback?: (downloadProgress: number) => void) => Promise<void>;
|
|
21
|
+
forward: (input: string) => Promise<any>;
|
|
22
|
+
delete(): void;
|
|
23
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { symbols } from '../constants/ocr/symbols';
|
|
2
|
+
import { ETError, getError } from '../Error';
|
|
3
|
+
import { ResourceFetcher } from '../utils/ResourceFetcher';
|
|
4
|
+
export class OCRController {
|
|
5
|
+
nativeModule;
|
|
6
|
+
isReady = false;
|
|
7
|
+
isGenerating = false;
|
|
8
|
+
error = null;
|
|
9
|
+
isReadyCallback;
|
|
10
|
+
isGeneratingCallback;
|
|
11
|
+
errorCallback;
|
|
12
|
+
constructor({ isReadyCallback = (_isReady) => { }, isGeneratingCallback = (_isGenerating) => { }, errorCallback = (_error) => { }, } = {}) {
|
|
13
|
+
this.isReadyCallback = isReadyCallback;
|
|
14
|
+
this.isGeneratingCallback = isGeneratingCallback;
|
|
15
|
+
this.errorCallback = errorCallback;
|
|
16
|
+
}
|
|
17
|
+
load = async (detectorSource, recognizerSources, language, onDownloadProgressCallback) => {
|
|
18
|
+
try {
|
|
19
|
+
if (!detectorSource || Object.keys(recognizerSources).length !== 3)
|
|
20
|
+
return;
|
|
21
|
+
if (!symbols[language]) {
|
|
22
|
+
throw new Error(getError(ETError.LanguageNotSupported));
|
|
23
|
+
}
|
|
24
|
+
this.isReady = false;
|
|
25
|
+
this.isReadyCallback(false);
|
|
26
|
+
const paths = await ResourceFetcher.fetch(onDownloadProgressCallback, detectorSource, recognizerSources.recognizerLarge, recognizerSources.recognizerMedium, recognizerSources.recognizerSmall);
|
|
27
|
+
if (paths === null || paths?.length < 4) {
|
|
28
|
+
throw new Error('Download interrupted!');
|
|
29
|
+
}
|
|
30
|
+
this.nativeModule = global.loadOCR(paths[0], paths[1], paths[2], paths[3], symbols[language]);
|
|
31
|
+
this.isReady = true;
|
|
32
|
+
this.isReadyCallback(this.isReady);
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
if (this.errorCallback) {
|
|
36
|
+
this.errorCallback(getError(e));
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
throw new Error(getError(e));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
forward = async (input) => {
|
|
44
|
+
if (!this.isReady) {
|
|
45
|
+
throw new Error(getError(ETError.ModuleNotLoaded));
|
|
46
|
+
}
|
|
47
|
+
if (this.isGenerating) {
|
|
48
|
+
throw new Error(getError(ETError.ModelGenerating));
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
this.isGenerating = true;
|
|
52
|
+
this.isGeneratingCallback(this.isGenerating);
|
|
53
|
+
return await this.nativeModule.generate(input);
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
throw new Error(getError(e));
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
this.isGenerating = false;
|
|
60
|
+
this.isGeneratingCallback(this.isGenerating);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
delete() {
|
|
64
|
+
if (this.isGenerating) {
|
|
65
|
+
throw new Error(getError(ETError.ModelGenerating) +
|
|
66
|
+
'You cannot delete the model. You must wait until the generating is finished.');
|
|
67
|
+
}
|
|
68
|
+
this.nativeModule.unload();
|
|
69
|
+
this.isReadyCallback(false);
|
|
70
|
+
this.isGeneratingCallback(false);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { MODES, STREAMING_ACTION } from '../constants/sttDefaults';
|
|
2
|
+
import { AvailableModels } from '../types/stt';
|
|
3
|
+
import { ResourceSource } from '../types/common';
|
|
4
|
+
import { SpeechToTextLanguage } from '../types/stt';
|
|
5
|
+
export declare class SpeechToTextController {
|
|
6
|
+
private speechToTextNativeModule;
|
|
7
|
+
sequence: number[];
|
|
8
|
+
isReady: boolean;
|
|
9
|
+
isGenerating: boolean;
|
|
10
|
+
private tokenizerModule;
|
|
11
|
+
private overlapSeconds;
|
|
12
|
+
private windowSize;
|
|
13
|
+
private chunks;
|
|
14
|
+
private seqs;
|
|
15
|
+
private prevSeq;
|
|
16
|
+
private waveform;
|
|
17
|
+
private numOfChunks;
|
|
18
|
+
private streaming;
|
|
19
|
+
private decodedTranscribeCallback;
|
|
20
|
+
private isReadyCallback;
|
|
21
|
+
private isGeneratingCallback;
|
|
22
|
+
private onErrorCallback;
|
|
23
|
+
private config;
|
|
24
|
+
constructor({ transcribeCallback, isReadyCallback, isGeneratingCallback, onErrorCallback, overlapSeconds, windowSize, streamingConfig, }: {
|
|
25
|
+
transcribeCallback: (sequence: string) => void;
|
|
26
|
+
isReadyCallback?: (isReady: boolean) => void;
|
|
27
|
+
isGeneratingCallback?: (isGenerating: boolean) => void;
|
|
28
|
+
onErrorCallback?: (error: Error | undefined) => void;
|
|
29
|
+
overlapSeconds?: number;
|
|
30
|
+
windowSize?: number;
|
|
31
|
+
streamingConfig?: keyof typeof MODES;
|
|
32
|
+
});
|
|
33
|
+
load({ modelName, encoderSource, decoderSource, tokenizerSource, onDownloadProgressCallback, }: {
|
|
34
|
+
modelName: AvailableModels;
|
|
35
|
+
encoderSource?: ResourceSource;
|
|
36
|
+
decoderSource?: ResourceSource;
|
|
37
|
+
tokenizerSource?: ResourceSource;
|
|
38
|
+
onDownloadProgressCallback?: (downloadProgress: number) => void;
|
|
39
|
+
}): Promise<void>;
|
|
40
|
+
configureStreaming(overlapSeconds?: number, windowSize?: number, streamingConfig?: keyof typeof MODES): void;
|
|
41
|
+
private chunkWaveform;
|
|
42
|
+
private resetState;
|
|
43
|
+
private expectedChunkLength;
|
|
44
|
+
private getStartingTokenIds;
|
|
45
|
+
private decodeChunk;
|
|
46
|
+
private handleOverlaps;
|
|
47
|
+
private trimLeft;
|
|
48
|
+
private trimRight;
|
|
49
|
+
private trimSequences;
|
|
50
|
+
private validateAndFixLastChunk;
|
|
51
|
+
private tokenIdsToText;
|
|
52
|
+
transcribe(waveform: number[], audioLanguage?: SpeechToTextLanguage): Promise<string>;
|
|
53
|
+
streamingTranscribe(streamAction: STREAMING_ACTION, waveform?: number[], audioLanguage?: SpeechToTextLanguage): Promise<string>;
|
|
54
|
+
encode(waveform: Float32Array): Promise<null>;
|
|
55
|
+
decode(seq: number[]): Promise<number>;
|
|
56
|
+
}
|