cactus-react-native 1.2.1 → 1.5.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/README.md +765 -33
- package/android/CMakeLists.txt +4 -3
- package/android/src/main/java/com/margelo/nitro/cactus/HybridCactusFileSystem.kt +20 -1
- package/android/src/main/jniLibs/arm64-v8a/libcactus.a +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libcactus_util.a +0 -0
- package/cpp/HybridCactus.cpp +231 -19
- package/cpp/HybridCactus.hpp +25 -3
- package/cpp/HybridCactusIndex.cpp +325 -0
- package/cpp/HybridCactusIndex.hpp +43 -0
- package/cpp/HybridCactusUtil.cpp +3 -3
- package/cpp/HybridCactusUtil.hpp +2 -1
- package/cpp/cactus_ffi.h +107 -2
- package/cpp/cactus_util.h +1 -1
- package/ios/HybridCactusFileSystem.swift +23 -2
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus.h +2 -0
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus_ffi.h +107 -2
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus_telemetry.h +656 -0
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/{ffi_utils.h → cactus_utils.h} +145 -18
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/engine.h +135 -7
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/gemma_tools.h +549 -0
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/graph.h +193 -26
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/kernel.h +54 -195
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/kernel_utils.h +399 -140
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/Info.plist +0 -0
- package/ios/cactus.xcframework/ios-arm64/cactus.framework/cactus +0 -0
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus.h +2 -0
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus_ffi.h +107 -2
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus_telemetry.h +656 -0
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/{ffi_utils.h → cactus_utils.h} +145 -18
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/engine.h +135 -7
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/gemma_tools.h +549 -0
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/graph.h +193 -26
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/kernel.h +54 -195
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/kernel_utils.h +399 -140
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Info.plist +0 -0
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/_CodeSignature/CodeResources +1 -1
- package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/cactus +0 -0
- package/ios/cactus_util.xcframework/Info.plist +4 -4
- package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/cactus_util.h +1 -1
- package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/database.h +27 -0
- package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Info.plist +0 -0
- package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/cactus_util +0 -0
- package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/cactus_util.h +1 -1
- package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/database.h +27 -0
- package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Info.plist +0 -0
- package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/_CodeSignature/CodeResources +3 -3
- package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/cactus_util +0 -0
- package/lib/module/api/Database.js +12 -95
- package/lib/module/api/Database.js.map +1 -1
- package/lib/module/classes/CactusIndex.js +45 -0
- package/lib/module/classes/CactusIndex.js.map +1 -0
- package/lib/module/classes/CactusLM.js +65 -17
- package/lib/module/classes/CactusLM.js.map +1 -1
- package/lib/module/classes/CactusSTT.js +104 -17
- package/lib/module/classes/CactusSTT.js.map +1 -1
- package/lib/module/config/CactusConfig.js +2 -0
- package/lib/module/config/CactusConfig.js.map +1 -1
- package/lib/module/constants/packageVersion.js +1 -1
- package/lib/module/hooks/useCactusIndex.js +175 -0
- package/lib/module/hooks/useCactusIndex.js.map +1 -0
- package/lib/module/hooks/useCactusLM.js +68 -7
- package/lib/module/hooks/useCactusLM.js.map +1 -1
- package/lib/module/hooks/useCactusSTT.js +102 -6
- package/lib/module/hooks/useCactusSTT.js.map +1 -1
- package/lib/module/index.js +2 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/models.js +336 -0
- package/lib/module/models.js.map +1 -0
- package/lib/module/native/Cactus.js +61 -13
- package/lib/module/native/Cactus.js.map +1 -1
- package/lib/module/native/CactusFileSystem.js +3 -0
- package/lib/module/native/CactusFileSystem.js.map +1 -1
- package/lib/module/native/CactusIndex.js +32 -0
- package/lib/module/native/CactusIndex.js.map +1 -0
- package/lib/module/native/CactusUtil.js +16 -3
- package/lib/module/native/CactusUtil.js.map +1 -1
- package/lib/module/native/index.js +1 -0
- package/lib/module/native/index.js.map +1 -1
- package/lib/module/specs/CactusIndex.nitro.js +4 -0
- package/lib/module/specs/CactusIndex.nitro.js.map +1 -0
- package/lib/module/telemetry/Telemetry.js +3 -1
- package/lib/module/telemetry/Telemetry.js.map +1 -1
- package/lib/module/types/CactusIndex.js +2 -0
- package/lib/module/types/{CactusModel.js.map → CactusIndex.js.map} +1 -1
- package/lib/module/types/CactusLM.js +2 -0
- package/lib/module/types/CactusSTT.js +2 -0
- package/lib/module/types/common.js +2 -0
- package/lib/module/types/{CactusSTTModel.js.map → common.js.map} +1 -1
- package/lib/typescript/src/api/Database.d.ts +4 -7
- package/lib/typescript/src/api/Database.d.ts.map +1 -1
- package/lib/typescript/src/classes/CactusIndex.d.ts +15 -0
- package/lib/typescript/src/classes/CactusIndex.d.ts.map +1 -0
- package/lib/typescript/src/classes/CactusLM.d.ts +12 -5
- package/lib/typescript/src/classes/CactusLM.d.ts.map +1 -1
- package/lib/typescript/src/classes/CactusSTT.d.ts +15 -5
- package/lib/typescript/src/classes/CactusSTT.d.ts.map +1 -1
- package/lib/typescript/src/config/CactusConfig.d.ts +1 -0
- package/lib/typescript/src/config/CactusConfig.d.ts.map +1 -1
- package/lib/typescript/src/constants/packageVersion.d.ts +1 -1
- package/lib/typescript/src/hooks/useCactusIndex.d.ts +14 -0
- package/lib/typescript/src/hooks/useCactusIndex.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useCactusLM.d.ts +6 -4
- package/lib/typescript/src/hooks/useCactusLM.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useCactusSTT.d.ts +13 -5
- package/lib/typescript/src/hooks/useCactusSTT.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +6 -4
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/models.d.ts +6 -0
- package/lib/typescript/src/models.d.ts.map +1 -0
- package/lib/typescript/src/native/Cactus.d.ts +10 -3
- package/lib/typescript/src/native/Cactus.d.ts.map +1 -1
- package/lib/typescript/src/native/CactusFileSystem.d.ts +1 -0
- package/lib/typescript/src/native/CactusFileSystem.d.ts.map +1 -1
- package/lib/typescript/src/native/CactusIndex.d.ts +12 -0
- package/lib/typescript/src/native/CactusIndex.d.ts.map +1 -0
- package/lib/typescript/src/native/CactusUtil.d.ts.map +1 -1
- package/lib/typescript/src/native/index.d.ts +1 -0
- package/lib/typescript/src/native/index.d.ts.map +1 -1
- package/lib/typescript/src/specs/Cactus.nitro.d.ts +9 -2
- package/lib/typescript/src/specs/Cactus.nitro.d.ts.map +1 -1
- package/lib/typescript/src/specs/CactusFileSystem.nitro.d.ts +1 -0
- package/lib/typescript/src/specs/CactusFileSystem.nitro.d.ts.map +1 -1
- package/lib/typescript/src/specs/CactusIndex.nitro.d.ts +24 -0
- package/lib/typescript/src/specs/CactusIndex.nitro.d.ts.map +1 -0
- package/lib/typescript/src/specs/CactusUtil.nitro.d.ts +1 -1
- package/lib/typescript/src/specs/CactusUtil.nitro.d.ts.map +1 -1
- package/lib/typescript/src/types/CactusIndex.d.ts +34 -0
- package/lib/typescript/src/types/CactusIndex.d.ts.map +1 -0
- package/lib/typescript/src/types/CactusLM.d.ts +19 -0
- package/lib/typescript/src/types/CactusLM.d.ts.map +1 -1
- package/lib/typescript/src/types/CactusSTT.d.ts +21 -1
- package/lib/typescript/src/types/CactusSTT.d.ts.map +1 -1
- package/lib/typescript/src/types/common.d.ts +28 -0
- package/lib/typescript/src/types/common.d.ts.map +1 -0
- package/nitro.json +3 -0
- package/nitrogen/generated/android/c++/JDeviceInfo.hpp +1 -1
- package/nitrogen/generated/android/c++/JFunc_void_double.hpp +1 -1
- package/nitrogen/generated/android/c++/JHybridCactusCryptoSpec.cpp +1 -1
- package/nitrogen/generated/android/c++/JHybridCactusCryptoSpec.hpp +1 -1
- package/nitrogen/generated/android/c++/JHybridCactusDeviceInfoSpec.cpp +1 -1
- package/nitrogen/generated/android/c++/JHybridCactusDeviceInfoSpec.hpp +1 -1
- package/nitrogen/generated/android/c++/JHybridCactusFileSystemSpec.cpp +17 -1
- package/nitrogen/generated/android/c++/JHybridCactusFileSystemSpec.hpp +2 -1
- package/nitrogen/generated/android/c++/JHybridCactusImageSpec.cpp +1 -1
- package/nitrogen/generated/android/c++/JHybridCactusImageSpec.hpp +1 -1
- package/nitrogen/generated/android/cactus+autolinking.cmake +2 -1
- package/nitrogen/generated/android/cactus+autolinking.gradle +1 -1
- package/nitrogen/generated/android/cactusOnLoad.cpp +11 -1
- package/nitrogen/generated/android/cactusOnLoad.hpp +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/DeviceInfo.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/Func_void_double.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusCryptoSpec.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusDeviceInfoSpec.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusFileSystemSpec.kt +5 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusImageSpec.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/cactusOnLoad.kt +1 -1
- package/nitrogen/generated/ios/Cactus+autolinking.rb +1 -1
- package/nitrogen/generated/ios/Cactus-Swift-Cxx-Bridge.cpp +1 -1
- package/nitrogen/generated/ios/Cactus-Swift-Cxx-Bridge.hpp +1 -1
- package/nitrogen/generated/ios/Cactus-Swift-Cxx-Umbrella.hpp +1 -1
- package/nitrogen/generated/ios/CactusAutolinking.mm +11 -1
- package/nitrogen/generated/ios/CactusAutolinking.swift +1 -1
- package/nitrogen/generated/ios/c++/HybridCactusCryptoSpecSwift.cpp +1 -1
- package/nitrogen/generated/ios/c++/HybridCactusCryptoSpecSwift.hpp +1 -1
- package/nitrogen/generated/ios/c++/HybridCactusDeviceInfoSpecSwift.cpp +1 -1
- package/nitrogen/generated/ios/c++/HybridCactusDeviceInfoSpecSwift.hpp +1 -1
- package/nitrogen/generated/ios/c++/HybridCactusFileSystemSpecSwift.cpp +1 -1
- package/nitrogen/generated/ios/c++/HybridCactusFileSystemSpecSwift.hpp +9 -1
- package/nitrogen/generated/ios/c++/HybridCactusImageSpecSwift.cpp +1 -1
- package/nitrogen/generated/ios/c++/HybridCactusImageSpecSwift.hpp +1 -1
- package/nitrogen/generated/ios/swift/DeviceInfo.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void_DeviceInfo.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void_bool.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void_double.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void_std__optional_std__string_.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void_std__string.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridCactusCryptoSpec.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridCactusCryptoSpec_cxx.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridCactusDeviceInfoSpec.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridCactusDeviceInfoSpec_cxx.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridCactusFileSystemSpec.swift +2 -1
- package/nitrogen/generated/ios/swift/HybridCactusFileSystemSpec_cxx.swift +20 -1
- package/nitrogen/generated/ios/swift/HybridCactusImageSpec.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridCactusImageSpec_cxx.swift +1 -1
- package/nitrogen/generated/shared/c++/CactusIndexGetResult.hpp +84 -0
- package/nitrogen/generated/shared/c++/CactusIndexQueryResult.hpp +79 -0
- package/nitrogen/generated/shared/c++/DeviceInfo.hpp +1 -1
- package/nitrogen/generated/shared/c++/HybridCactusCryptoSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridCactusCryptoSpec.hpp +1 -1
- package/nitrogen/generated/shared/c++/HybridCactusDeviceInfoSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridCactusDeviceInfoSpec.hpp +1 -1
- package/nitrogen/generated/shared/c++/HybridCactusFileSystemSpec.cpp +2 -1
- package/nitrogen/generated/shared/c++/HybridCactusFileSystemSpec.hpp +2 -1
- package/nitrogen/generated/shared/c++/HybridCactusImageSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridCactusImageSpec.hpp +1 -1
- package/nitrogen/generated/shared/c++/HybridCactusIndexSpec.cpp +27 -0
- package/nitrogen/generated/shared/c++/HybridCactusIndexSpec.hpp +76 -0
- package/nitrogen/generated/shared/c++/HybridCactusSpec.cpp +8 -1
- package/nitrogen/generated/shared/c++/HybridCactusSpec.hpp +11 -3
- package/nitrogen/generated/shared/c++/HybridCactusUtilSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridCactusUtilSpec.hpp +2 -2
- package/package.json +2 -2
- package/src/api/Database.ts +14 -135
- package/src/classes/CactusIndex.ts +58 -0
- package/src/classes/CactusLM.ts +87 -19
- package/src/classes/CactusSTT.ts +134 -20
- package/src/config/CactusConfig.ts +3 -0
- package/src/constants/packageVersion.ts +1 -1
- package/src/hooks/useCactusIndex.ts +195 -0
- package/src/hooks/useCactusLM.ts +88 -8
- package/src/hooks/useCactusSTT.ts +119 -7
- package/src/index.tsx +22 -2
- package/src/models.ts +344 -0
- package/src/native/Cactus.ts +95 -13
- package/src/native/CactusFileSystem.ts +4 -0
- package/src/native/CactusIndex.ts +54 -0
- package/src/native/CactusUtil.ts +19 -3
- package/src/native/index.ts +1 -0
- package/src/specs/Cactus.nitro.ts +18 -2
- package/src/specs/CactusFileSystem.nitro.ts +2 -0
- package/src/specs/CactusIndex.nitro.ts +31 -0
- package/src/specs/CactusUtil.nitro.ts +1 -1
- package/src/telemetry/Telemetry.ts +1 -1
- package/src/types/CactusIndex.ts +40 -0
- package/src/types/CactusLM.ts +24 -0
- package/src/types/CactusSTT.ts +27 -1
- package/src/types/common.ts +28 -0
- package/android/src/main/jniLibs/arm64-v8a/libcactus_util.so +0 -0
- package/lib/module/types/CactusModel.js +0 -2
- package/lib/module/types/CactusSTTModel.js +0 -2
- package/lib/typescript/src/types/CactusModel.d.ts +0 -13
- package/lib/typescript/src/types/CactusModel.d.ts.map +0 -1
- package/lib/typescript/src/types/CactusSTTModel.d.ts +0 -8
- package/lib/typescript/src/types/CactusSTTModel.d.ts.map +0 -1
- package/src/types/CactusModel.ts +0 -15
- package/src/types/CactusSTTModel.ts +0 -10
package/src/hooks/useCactusLM.ts
CHANGED
|
@@ -4,23 +4,31 @@ import { CactusFileSystem } from '../native';
|
|
|
4
4
|
import { getErrorMessage } from '../utils/error';
|
|
5
5
|
import type {
|
|
6
6
|
CactusLMParams,
|
|
7
|
+
CactusLMCompleteParams,
|
|
7
8
|
CactusLMCompleteResult,
|
|
9
|
+
CactusLMTokenizeParams,
|
|
10
|
+
CactusLMTokenizeResult,
|
|
11
|
+
CactusLMScoreWindowParams,
|
|
12
|
+
CactusLMScoreWindowResult,
|
|
8
13
|
CactusLMEmbedParams,
|
|
9
14
|
CactusLMEmbedResult,
|
|
10
15
|
CactusLMImageEmbedParams,
|
|
11
16
|
CactusLMImageEmbedResult,
|
|
12
|
-
CactusLMCompleteParams,
|
|
13
17
|
CactusLMDownloadParams,
|
|
14
18
|
} from '../types/CactusLM';
|
|
15
|
-
import type { CactusModel } from '../types/
|
|
19
|
+
import type { CactusModel } from '../types/common';
|
|
16
20
|
|
|
17
21
|
export const useCactusLM = ({
|
|
18
|
-
model = 'qwen3-0.
|
|
22
|
+
model = 'qwen3-0.6b',
|
|
19
23
|
contextSize = 2048,
|
|
20
24
|
corpusDir = undefined,
|
|
25
|
+
options: modelOptions = {
|
|
26
|
+
quantization: undefined,
|
|
27
|
+
pro: false,
|
|
28
|
+
},
|
|
21
29
|
}: CactusLMParams = {}) => {
|
|
22
30
|
const [cactusLM, setCactusLM] = useState(
|
|
23
|
-
() => new CactusLM({ model, contextSize, corpusDir })
|
|
31
|
+
() => new CactusLM({ model, contextSize, corpusDir, options: modelOptions })
|
|
24
32
|
);
|
|
25
33
|
|
|
26
34
|
// State
|
|
@@ -40,7 +48,17 @@ export const useCactusLM = ({
|
|
|
40
48
|
}, [model]);
|
|
41
49
|
|
|
42
50
|
useEffect(() => {
|
|
43
|
-
setCactusLM(
|
|
51
|
+
setCactusLM(
|
|
52
|
+
new CactusLM({
|
|
53
|
+
model,
|
|
54
|
+
contextSize,
|
|
55
|
+
corpusDir,
|
|
56
|
+
options: {
|
|
57
|
+
quantization: modelOptions.quantization,
|
|
58
|
+
pro: modelOptions.pro,
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
);
|
|
44
62
|
|
|
45
63
|
setCompletion('');
|
|
46
64
|
setIsGenerating(false);
|
|
@@ -69,7 +87,13 @@ export const useCactusLM = ({
|
|
|
69
87
|
return () => {
|
|
70
88
|
mounted = false;
|
|
71
89
|
};
|
|
72
|
-
}, [
|
|
90
|
+
}, [
|
|
91
|
+
model,
|
|
92
|
+
contextSize,
|
|
93
|
+
corpusDir,
|
|
94
|
+
modelOptions.quantization,
|
|
95
|
+
modelOptions.pro,
|
|
96
|
+
]);
|
|
73
97
|
|
|
74
98
|
useEffect(() => {
|
|
75
99
|
return () => {
|
|
@@ -201,8 +225,62 @@ export const useCactusLM = ({
|
|
|
201
225
|
[cactusLM, isGenerating]
|
|
202
226
|
);
|
|
203
227
|
|
|
228
|
+
const tokenize = useCallback(
|
|
229
|
+
async ({
|
|
230
|
+
text,
|
|
231
|
+
}: CactusLMTokenizeParams): Promise<CactusLMTokenizeResult> => {
|
|
232
|
+
if (isGenerating) {
|
|
233
|
+
const message = 'CactusLM is already generating';
|
|
234
|
+
setError(message);
|
|
235
|
+
throw new Error(message);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
setError(null);
|
|
239
|
+
setIsGenerating(true);
|
|
240
|
+
try {
|
|
241
|
+
return await cactusLM.tokenize({ text });
|
|
242
|
+
} catch (e) {
|
|
243
|
+
setError(getErrorMessage(e));
|
|
244
|
+
throw e;
|
|
245
|
+
} finally {
|
|
246
|
+
setIsGenerating(false);
|
|
247
|
+
}
|
|
248
|
+
},
|
|
249
|
+
[cactusLM, isGenerating]
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
const scoreWindow = useCallback(
|
|
253
|
+
async ({
|
|
254
|
+
tokens,
|
|
255
|
+
start,
|
|
256
|
+
end,
|
|
257
|
+
context,
|
|
258
|
+
}: CactusLMScoreWindowParams): Promise<CactusLMScoreWindowResult> => {
|
|
259
|
+
if (isGenerating) {
|
|
260
|
+
const message = 'CactusLM is already generating';
|
|
261
|
+
setError(message);
|
|
262
|
+
throw new Error(message);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
setError(null);
|
|
266
|
+
setIsGenerating(true);
|
|
267
|
+
try {
|
|
268
|
+
return await cactusLM.scoreWindow({ tokens, start, end, context });
|
|
269
|
+
} catch (e) {
|
|
270
|
+
setError(getErrorMessage(e));
|
|
271
|
+
throw e;
|
|
272
|
+
} finally {
|
|
273
|
+
setIsGenerating(false);
|
|
274
|
+
}
|
|
275
|
+
},
|
|
276
|
+
[cactusLM, isGenerating]
|
|
277
|
+
);
|
|
278
|
+
|
|
204
279
|
const embed = useCallback(
|
|
205
|
-
async ({
|
|
280
|
+
async ({
|
|
281
|
+
text,
|
|
282
|
+
normalize = false,
|
|
283
|
+
}: CactusLMEmbedParams): Promise<CactusLMEmbedResult> => {
|
|
206
284
|
if (isGenerating) {
|
|
207
285
|
const message = 'CactusLM is already generating';
|
|
208
286
|
setError(message);
|
|
@@ -212,7 +290,7 @@ export const useCactusLM = ({
|
|
|
212
290
|
setError(null);
|
|
213
291
|
setIsGenerating(true);
|
|
214
292
|
try {
|
|
215
|
-
return await cactusLM.embed({ text });
|
|
293
|
+
return await cactusLM.embed({ text, normalize });
|
|
216
294
|
} catch (e) {
|
|
217
295
|
setError(getErrorMessage(e));
|
|
218
296
|
throw e;
|
|
@@ -303,6 +381,8 @@ export const useCactusLM = ({
|
|
|
303
381
|
download,
|
|
304
382
|
init,
|
|
305
383
|
complete,
|
|
384
|
+
tokenize,
|
|
385
|
+
scoreWindow,
|
|
306
386
|
embed,
|
|
307
387
|
imageEmbed,
|
|
308
388
|
reset,
|
|
@@ -9,20 +9,32 @@ import type {
|
|
|
9
9
|
CactusSTTDownloadParams,
|
|
10
10
|
CactusSTTAudioEmbedParams,
|
|
11
11
|
CactusSTTAudioEmbedResult,
|
|
12
|
+
CactusSTTStreamTranscribeInsertParams,
|
|
13
|
+
CactusSTTStreamTranscribeProcessParams,
|
|
14
|
+
CactusSTTStreamTranscribeProcessResult,
|
|
15
|
+
CactusSTTStreamTranscribeFinalizeResult,
|
|
12
16
|
} from '../types/CactusSTT';
|
|
13
|
-
import type {
|
|
17
|
+
import type { CactusModel } from '../types/common';
|
|
14
18
|
|
|
15
19
|
export const useCactusSTT = ({
|
|
16
20
|
model = 'whisper-small',
|
|
17
21
|
contextSize = 2048,
|
|
22
|
+
options: modelOptions = {
|
|
23
|
+
quantization: undefined,
|
|
24
|
+
pro: false,
|
|
25
|
+
},
|
|
18
26
|
}: CactusSTTParams = {}) => {
|
|
19
27
|
const [cactusSTT, setCactusSTT] = useState(
|
|
20
|
-
() => new CactusSTT({ model, contextSize })
|
|
28
|
+
() => new CactusSTT({ model, contextSize, options: modelOptions })
|
|
21
29
|
);
|
|
22
30
|
|
|
23
31
|
// State
|
|
24
32
|
const [transcription, setTranscription] = useState('');
|
|
33
|
+
const [streamTranscribeConfirmed, setStreamTranscribeConfirmed] =
|
|
34
|
+
useState('');
|
|
35
|
+
const [streamTranscribePending, setStreamTranscribePending] = useState('');
|
|
25
36
|
const [isGenerating, setIsGenerating] = useState(false);
|
|
37
|
+
const [isStreamTranscribing, setIsStreamTranscribing] = useState(false);
|
|
26
38
|
const [isInitializing, setIsInitializing] = useState(false);
|
|
27
39
|
const [isDownloaded, setIsDownloaded] = useState(false);
|
|
28
40
|
const [isDownloading, setIsDownloading] = useState(false);
|
|
@@ -37,10 +49,22 @@ export const useCactusSTT = ({
|
|
|
37
49
|
}, [model]);
|
|
38
50
|
|
|
39
51
|
useEffect(() => {
|
|
40
|
-
setCactusSTT(
|
|
52
|
+
setCactusSTT(
|
|
53
|
+
new CactusSTT({
|
|
54
|
+
model,
|
|
55
|
+
contextSize,
|
|
56
|
+
options: {
|
|
57
|
+
quantization: modelOptions.quantization,
|
|
58
|
+
pro: modelOptions.pro,
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
);
|
|
41
62
|
|
|
42
63
|
setTranscription('');
|
|
64
|
+
setStreamTranscribeConfirmed('');
|
|
65
|
+
setStreamTranscribePending('');
|
|
43
66
|
setIsGenerating(false);
|
|
67
|
+
setIsStreamTranscribing(false);
|
|
44
68
|
setIsInitializing(false);
|
|
45
69
|
setIsDownloaded(false);
|
|
46
70
|
setIsDownloading(false);
|
|
@@ -66,7 +90,7 @@ export const useCactusSTT = ({
|
|
|
66
90
|
return () => {
|
|
67
91
|
mounted = false;
|
|
68
92
|
};
|
|
69
|
-
}, [model, contextSize]);
|
|
93
|
+
}, [model, contextSize, modelOptions.quantization, modelOptions.pro]);
|
|
70
94
|
|
|
71
95
|
useEffect(() => {
|
|
72
96
|
return () => {
|
|
@@ -162,7 +186,7 @@ export const useCactusSTT = ({
|
|
|
162
186
|
|
|
163
187
|
const transcribe = useCallback(
|
|
164
188
|
async ({
|
|
165
|
-
|
|
189
|
+
audio,
|
|
166
190
|
prompt,
|
|
167
191
|
options,
|
|
168
192
|
onToken,
|
|
@@ -178,7 +202,7 @@ export const useCactusSTT = ({
|
|
|
178
202
|
setIsGenerating(true);
|
|
179
203
|
try {
|
|
180
204
|
return await cactusSTT.transcribe({
|
|
181
|
-
|
|
205
|
+
audio,
|
|
182
206
|
prompt,
|
|
183
207
|
options,
|
|
184
208
|
onToken: (token) => {
|
|
@@ -220,6 +244,83 @@ export const useCactusSTT = ({
|
|
|
220
244
|
[cactusSTT, isGenerating]
|
|
221
245
|
);
|
|
222
246
|
|
|
247
|
+
const streamTranscribeInit = useCallback(async () => {
|
|
248
|
+
if (isStreamTranscribing) {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
setError(null);
|
|
253
|
+
setStreamTranscribeConfirmed('');
|
|
254
|
+
setStreamTranscribePending('');
|
|
255
|
+
setIsStreamTranscribing(true);
|
|
256
|
+
try {
|
|
257
|
+
await cactusSTT.streamTranscribeInit();
|
|
258
|
+
} catch (e) {
|
|
259
|
+
setError(getErrorMessage(e));
|
|
260
|
+
setIsStreamTranscribing(false);
|
|
261
|
+
throw e;
|
|
262
|
+
}
|
|
263
|
+
}, [cactusSTT, isStreamTranscribing]);
|
|
264
|
+
|
|
265
|
+
const streamTranscribeInsert = useCallback(
|
|
266
|
+
async ({ audio }: CactusSTTStreamTranscribeInsertParams): Promise<void> => {
|
|
267
|
+
setError(null);
|
|
268
|
+
try {
|
|
269
|
+
await cactusSTT.streamTranscribeInsert({ audio });
|
|
270
|
+
} catch (e) {
|
|
271
|
+
setError(getErrorMessage(e));
|
|
272
|
+
throw e;
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
[cactusSTT]
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
const streamTranscribeProcess = useCallback(
|
|
279
|
+
async ({
|
|
280
|
+
options,
|
|
281
|
+
}: CactusSTTStreamTranscribeProcessParams = {}): Promise<CactusSTTStreamTranscribeProcessResult> => {
|
|
282
|
+
setError(null);
|
|
283
|
+
try {
|
|
284
|
+
const result = await cactusSTT.streamTranscribeProcess({ options });
|
|
285
|
+
setStreamTranscribeConfirmed((prev) => prev + result.confirmed);
|
|
286
|
+
setStreamTranscribePending(result.pending);
|
|
287
|
+
return result;
|
|
288
|
+
} catch (e) {
|
|
289
|
+
setError(getErrorMessage(e));
|
|
290
|
+
throw e;
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
[cactusSTT]
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
const streamTranscribeFinalize =
|
|
297
|
+
useCallback(async (): Promise<CactusSTTStreamTranscribeFinalizeResult> => {
|
|
298
|
+
setError(null);
|
|
299
|
+
try {
|
|
300
|
+
const result = await cactusSTT.streamTranscribeFinalize();
|
|
301
|
+
setStreamTranscribeConfirmed((prev) => prev + result.confirmed);
|
|
302
|
+
setStreamTranscribePending('');
|
|
303
|
+
setIsStreamTranscribing(false);
|
|
304
|
+
return result;
|
|
305
|
+
} catch (e) {
|
|
306
|
+
setError(getErrorMessage(e));
|
|
307
|
+
throw e;
|
|
308
|
+
}
|
|
309
|
+
}, [cactusSTT]);
|
|
310
|
+
|
|
311
|
+
const streamTranscribeDestroy = useCallback(async (): Promise<void> => {
|
|
312
|
+
setError(null);
|
|
313
|
+
try {
|
|
314
|
+
await cactusSTT.streamTranscribeDestroy();
|
|
315
|
+
} catch (e) {
|
|
316
|
+
setError(getErrorMessage(e));
|
|
317
|
+
throw e;
|
|
318
|
+
} finally {
|
|
319
|
+
setIsStreamTranscribing(false);
|
|
320
|
+
setStreamTranscribePending('');
|
|
321
|
+
}
|
|
322
|
+
}, [cactusSTT]);
|
|
323
|
+
|
|
223
324
|
const stop = useCallback(async () => {
|
|
224
325
|
setError(null);
|
|
225
326
|
try {
|
|
@@ -251,10 +352,13 @@ export const useCactusSTT = ({
|
|
|
251
352
|
throw e;
|
|
252
353
|
} finally {
|
|
253
354
|
setTranscription('');
|
|
355
|
+
setStreamTranscribeConfirmed('');
|
|
356
|
+
setStreamTranscribePending('');
|
|
357
|
+
setIsStreamTranscribing(false);
|
|
254
358
|
}
|
|
255
359
|
}, [cactusSTT]);
|
|
256
360
|
|
|
257
|
-
const getModels = useCallback(async (): Promise<
|
|
361
|
+
const getModels = useCallback(async (): Promise<CactusModel[]> => {
|
|
258
362
|
setError(null);
|
|
259
363
|
try {
|
|
260
364
|
return await cactusSTT.getModels();
|
|
@@ -266,7 +370,10 @@ export const useCactusSTT = ({
|
|
|
266
370
|
|
|
267
371
|
return {
|
|
268
372
|
transcription,
|
|
373
|
+
streamTranscribeConfirmed,
|
|
374
|
+
streamTranscribePending,
|
|
269
375
|
isGenerating,
|
|
376
|
+
isStreamTranscribing,
|
|
270
377
|
isInitializing,
|
|
271
378
|
isDownloaded,
|
|
272
379
|
isDownloading,
|
|
@@ -277,6 +384,11 @@ export const useCactusSTT = ({
|
|
|
277
384
|
init,
|
|
278
385
|
transcribe,
|
|
279
386
|
audioEmbed,
|
|
387
|
+
streamTranscribeInit,
|
|
388
|
+
streamTranscribeInsert,
|
|
389
|
+
streamTranscribeProcess,
|
|
390
|
+
streamTranscribeFinalize,
|
|
391
|
+
streamTranscribeDestroy,
|
|
280
392
|
reset,
|
|
281
393
|
stop,
|
|
282
394
|
destroy,
|
package/src/index.tsx
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
// Classes
|
|
2
2
|
export { CactusLM } from './classes/CactusLM';
|
|
3
3
|
export { CactusSTT } from './classes/CactusSTT';
|
|
4
|
+
export { CactusIndex } from './classes/CactusIndex';
|
|
4
5
|
|
|
5
6
|
// Hooks
|
|
6
7
|
export { useCactusLM } from './hooks/useCactusLM';
|
|
7
8
|
export { useCactusSTT } from './hooks/useCactusSTT';
|
|
9
|
+
export { useCactusIndex } from './hooks/useCactusIndex';
|
|
8
10
|
|
|
9
11
|
// Types
|
|
10
|
-
export type { CactusModel } from './types/
|
|
11
|
-
export type { CactusSTTModel } from './types/CactusSTTModel';
|
|
12
|
+
export type { CactusModel, ModelOptions } from './types/common';
|
|
12
13
|
export type {
|
|
13
14
|
CactusLMParams,
|
|
14
15
|
CactusLMDownloadParams,
|
|
@@ -17,6 +18,10 @@ export type {
|
|
|
17
18
|
Tool,
|
|
18
19
|
CactusLMCompleteParams,
|
|
19
20
|
CactusLMCompleteResult,
|
|
21
|
+
CactusLMTokenizeParams,
|
|
22
|
+
CactusLMTokenizeResult,
|
|
23
|
+
CactusLMScoreWindowParams,
|
|
24
|
+
CactusLMScoreWindowResult,
|
|
20
25
|
CactusLMEmbedParams,
|
|
21
26
|
CactusLMEmbedResult,
|
|
22
27
|
CactusLMImageEmbedParams,
|
|
@@ -30,7 +35,22 @@ export type {
|
|
|
30
35
|
CactusSTTTranscribeResult,
|
|
31
36
|
CactusSTTAudioEmbedParams,
|
|
32
37
|
CactusSTTAudioEmbedResult,
|
|
38
|
+
CactusSTTStreamTranscribeInsertParams,
|
|
39
|
+
StreamTranscribeProcessOptions,
|
|
40
|
+
CactusSTTStreamTranscribeProcessParams,
|
|
41
|
+
CactusSTTStreamTranscribeProcessResult,
|
|
42
|
+
CactusSTTStreamTranscribeFinalizeResult,
|
|
33
43
|
} from './types/CactusSTT';
|
|
44
|
+
export type {
|
|
45
|
+
CactusIndexParams,
|
|
46
|
+
CactusIndexAddParams,
|
|
47
|
+
CactusIndexGetParams,
|
|
48
|
+
CactusIndexGetResult,
|
|
49
|
+
IndexQueryOptions,
|
|
50
|
+
CactusIndexQueryParams,
|
|
51
|
+
CactusIndexQueryResult,
|
|
52
|
+
CactusIndexDeleteParams,
|
|
53
|
+
} from './types/CactusIndex';
|
|
34
54
|
|
|
35
55
|
// Config
|
|
36
56
|
export { CactusConfig } from './config/CactusConfig';
|