UNPKG

react-native-executorch

Version:

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

27 lines (24 loc) 937 B
import { ResourceFetcher } from '../../utils/ResourceFetcher'; import { ResourceSource } from '../../types/common'; import { ETError, getError } from '../../Error'; import { BaseModule } from '../BaseModule'; export class ImageEmbeddingsModule extends BaseModule { async load( model: { modelSource: ResourceSource }, onDownloadProgressCallback: (progress: number) => void = () => {} ): Promise<void> { const paths = await ResourceFetcher.fetch( onDownloadProgressCallback, model.modelSource ); if (paths === null || paths.length < 1) { throw new Error('Download interrupted.'); } this.nativeModule = global.loadImageEmbeddings(paths[0] || ''); } async forward(imageSource: string): Promise<Float32Array> { if (this.nativeModule == null) throw new Error(getError(ETError.ModuleNotLoaded)); return new Float32Array(await this.nativeModule.generate(imageSource)); } }