react-native-executorch
Version:
An easy way to run AI models in react native with ExecuTorch
48 lines (47 loc) • 1.29 kB
JavaScript
;
import { useEffect, useState } from 'react';
import { ETError, getError } from '../Error';
export const useModule = ({
module,
loadArgs,
preventLoad = false
}) => {
const [error, setError] = useState(null);
const [isReady, setIsReady] = useState(false);
const [isGenerating, setIsGenerating] = useState(false);
const [downloadProgress, setDownloadProgress] = useState(0);
useEffect(() => {
const loadModule = async () => {
try {
setIsReady(false);
module.onDownloadProgress(setDownloadProgress);
await module.load(...loadArgs);
setIsReady(true);
} catch (err) {
setError(err.message);
}
};
if (!preventLoad) {
loadModule();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [...loadArgs, preventLoad]);
const forward = async (...input) => {
if (!isReady) throw new Error(getError(ETError.ModuleNotLoaded));
if (isGenerating) throw new Error(getError(ETError.ModelGenerating));
try {
setIsGenerating(true);
return await module.forward(...input);
} finally {
setIsGenerating(false);
}
};
return {
error,
isReady,
isGenerating,
downloadProgress,
forward
};
};
//# sourceMappingURL=useModule.js.map