UNPKG

react-native-executorch

Version:

An easy way to run AI models in React Native with ExecuTorch

22 lines (21 loc) 977 B
import { ResourceFetcher } from '../../utils/ResourceFetcher'; import { BaseNonStaticModule } from '../BaseNonStaticModule'; export class TextEmbeddingsModule extends BaseNonStaticModule { async load(model, onDownloadProgressCallback = () => { }) { const modelPromise = ResourceFetcher.fetch(onDownloadProgressCallback, model.modelSource); const tokenizerPromise = ResourceFetcher.fetch(undefined, model.tokenizerSource); const [modelResult, tokenizerResult] = await Promise.all([ modelPromise, tokenizerPromise, ]); const modelPath = modelResult?.[0]; const tokenizerPath = tokenizerResult?.[0]; if (!modelPath || !tokenizerPath) { throw new Error('Download interrupted.'); } this.nativeModule = global.loadTextEmbeddings(modelPath, tokenizerPath); } async forward(input) { return new Float32Array(await this.nativeModule.generate(input)); } }