clustering-tfjs
Version:
High-performance TypeScript clustering algorithms (K-Means, Spectral, Agglomerative) with TensorFlow.js acceleration and scikit-learn compatibility
59 lines (58 loc) • 2.38 kB
JavaScript
;
/**
* TensorFlow.js adapter module
*
* This module provides a platform-agnostic interface to TensorFlow.js,
* allowing the library to work in both Node.js and browser environments.
*
* Phase 2 implementation: For now, we maintain backward compatibility
* by still importing tfjs-node directly, but the infrastructure for
* dynamic loading is ready in tf-backend.ts and loaders.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
// Handle Windows CI environment where native modules fail
let tf;
if (process.platform === 'win32' && process.env.CI) {
// Use pure JS implementation on Windows CI
try {
tf = require('@tensorflow/tfjs');
}
catch (error) {
console.error('tf-adapter: Failed to load @tensorflow/tfjs on Windows CI');
throw new Error(`Failed to load TensorFlow.js: ${error instanceof Error ? error.message : String(error)}`);
}
}
else {
try {
// Use Node.js backend for better performance
tf = require('@tensorflow/tfjs-node');
}
catch (error) {
// Fallback to pure JS if tfjs-node fails to load
console.warn('tf-adapter: Failed to load @tensorflow/tfjs-node, using pure JS fallback');
try {
tf = require('@tensorflow/tfjs');
}
catch (fallbackError) {
console.error('tf-adapter: Failed to load @tensorflow/tfjs fallback');
throw new Error(`Failed to load TensorFlow.js: ${fallbackError instanceof Error ? fallbackError.message : String(fallbackError)}`);
}
}
}
exports.default = tf;
// Re-export everything from the loaded module
__exportStar(require("@tensorflow/tfjs-core"), exports);