@sconedev/ai_toolkit
Version:
Simplify AI integration in web apps with local and offline model support
93 lines (92 loc) • 4.13 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.downloadModel = downloadModel;
exports.preloadModelInBrowser = preloadModelInBrowser;
const browser_check_1 = require("../utils/browser-check");
const onnxModels_1 = require("./onnxModels");
// Only import Node.js modules when not in browser
let fs;
let path;
let MODEL_CACHE_DIR = '';
// Safely initialize Node.js components
if (!(0, browser_check_1.isBrowser)()) {
try {
fs = require('fs');
path = require('path');
MODEL_CACHE_DIR = path.join(process.cwd(), '.model-cache');
}
catch (e) {
// Handle potential import errors
console.warn('Node.js modules not available in this environment');
}
}
// Function to download and cache a model in Node.js
function downloadModel(modelConfig) {
return __awaiter(this, void 0, void 0, function* () {
if ((0, browser_check_1.isBrowser)()) {
throw new Error('This function is for Node.js only');
}
if (!fs || !path) {
throw new Error('Node.js file system modules not available');
}
try {
// Create cache directory if it doesn't exist
if (!fs.existsSync(MODEL_CACHE_DIR)) {
fs.mkdirSync(MODEL_CACHE_DIR, { recursive: true });
}
const modelFileName = `${modelConfig.name}.onnx`;
const modelPath = path.join(MODEL_CACHE_DIR, modelFileName);
// Check if model already exists in cache
if (fs.existsSync(modelPath)) {
return modelPath;
}
// Model doesn't exist, we need to download it
console.log(`Downloading model ${modelConfig.name} from ${modelConfig.modelUrl}`);
const response = yield fetch(modelConfig.modelUrl);
if (!response.ok) {
throw new Error(`Failed to download model: ${response.status} ${response.statusText}`);
}
const buffer = Buffer.from(yield response.arrayBuffer());
fs.writeFileSync(modelPath, buffer);
console.log(`Model ${modelConfig.name} downloaded and saved to ${modelPath}`);
return modelPath;
}
catch (error) {
throw new Error(`Error downloading model: ${error instanceof Error ? error.message : String(error)}`);
}
});
}
// Function to download and cache a model in browser
function preloadModelInBrowser(modelName) {
return __awaiter(this, void 0, void 0, function* () {
if (!(0, browser_check_1.isBrowser)()) {
throw new Error('This function is for browser only');
}
const modelConfig = onnxModels_1.predefinedModels[modelName];
if (!modelConfig) {
throw new Error(`Model '${modelName}' not found in predefined models`);
}
try {
// Simply fetching the model and storing in browser cache
const response = yield fetch(modelConfig.modelUrl, { cache: 'force-cache' });
if (!response.ok) {
throw new Error(`Failed to preload model: ${response.status} ${response.statusText}`);
}
// We don't need to do anything with the response, just trigger the fetch
return true;
}
catch (error) {
console.error(`Error preloading model ${modelName}:`, error);
return false;
}
});
}