shevchenko
Version:
JavaScript library for declension of Ukrainian anthroponyms
72 lines (68 loc) • 2.69 kB
JavaScript
/**
* @file JavaScript library for declension of Ukrainian anthroponyms
* @module shevchenko
* @version 3.2.2
* @author Oleksandr Tolochko <shevchenko-js@tooleks.com>
* @license MIT
* @copyright 2017-2026 Oleksandr Tolochko <shevchenko-js@tooleks.com>
* @see {@link git+https://github.com/tooleks/shevchenko-js.git}
*/
;
var model_bundle = require('./artifacts/model.bundle.json.js');
// Use Buffer on Node.js instead of Blob/atob/btoa
const useNodeBuffer = typeof Buffer !== 'undefined' &&
(typeof Blob === 'undefined' || typeof atob === 'undefined' || typeof btoa === 'undefined');
/**
* Decodes a base64 string as an ArrayBuffer.
*/
function base64StringToArrayBuffer(str) {
if (useNodeBuffer) {
const buf = Buffer.from(str, 'base64');
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
}
const s = atob(str);
const buffer = new Uint8Array(s.length);
for (let i = 0; i < s.length; ++i) {
buffer.set([s.charCodeAt(i)], i);
}
return buffer.buffer;
}
/**
* @see {@link https://github.com/tensorflow/tfjs/blob/57e0f212f7fc3643ec1f70130725c7d3c716ef7a/tfjs-core/src/io/local_storage.ts}
*/
// eslint-disable-next-line import/namespace
class ModelBundleLoader {
/**
* Saves the model artifacts from the artifacts directory in JSON format.
*/
async load() {
const out = {};
out.modelTopology = model_bundle.default.modelTopology;
out.weightSpecs = model_bundle.default.weightSpecs;
out.weightData = base64StringToArrayBuffer(model_bundle.default.weightData);
if (model_bundle.default.modelMetadata != null) {
const metadata = model_bundle.default.modelMetadata;
out.format = metadata.format;
out.generatedBy = metadata.generatedBy;
out.convertedBy = metadata.convertedBy;
if (metadata.signature != null) {
out.signature = metadata.signature;
}
if (metadata.userDefinedMetadata != null) {
out.userDefinedMetadata = metadata.userDefinedMetadata;
}
if (metadata.modelInitializer != null) {
out.modelInitializer = metadata.modelInitializer;
}
if (metadata.initializerSignature != null) {
out.initializerSignature = metadata.initializerSignature;
}
if (metadata.trainingConfig != null) {
out.trainingConfig = metadata.trainingConfig;
}
}
return out;
}
}
exports.ModelBundleLoader = ModelBundleLoader;
exports.base64StringToArrayBuffer = base64StringToArrayBuffer;