feat(board): python_pyodide下添加对tensorflow的支持 (待完善)

This commit is contained in:
王立帮
2025-07-22 17:54:19 +08:00
parent 24b5bc4304
commit 418cbd53b0
9 changed files with 1212 additions and 1 deletions

View File

@@ -1,3 +1,6 @@
import NavExt from './nav-ext';
import * as tf from '@tensorflow/tfjs';
import './tensorflow';
NavExt.init();
NavExt.init();
window.tf = tf;

View File

@@ -0,0 +1,58 @@
import { Registry } from 'mixly';
import * as tf from '@tensorflow/tfjs';
const modelsValueRegistry = new Registry();
const customFetch = function (path) {
let result = {
ok: false,
buffer: null,
json: function () {
const decoder = new TextDecoder('utf-8');
const jsonText = decoder.decode(this.buffer);
return JSON.parse(jsonText);
},
arrayBuffer: function () {
return this.buffer;
}
}
if (!modelsValueRegistry.hasKey(path)) {
return result;
}
result.ok = true;
result.buffer = modelsValueRegistry.getItem(path);
return result;
};
const tensorflow = {};
tensorflow.modelsValue = {};
tensorflow.loadGraphModel = async function (path) {
const model = await tf.loadGraphModel(path, {
fromTFHub: false,
fetchFunc: (...args) => {
return customFetch(args[0]);
}
});
return model;
};
tensorflow.loadLayersModel = async function (path) {
const model = await tf.loadLayersModel(path, {
fromTFHub: false,
fetchFunc: (...args) => {
return customFetch(args[0]);
}
});
return model;
};
tensorflow.setModelsValue = function (path, value) {
if (modelsValueRegistry.hasKey(path)) {
modelsValueRegistry.unregister(path);
}
modelsValueRegistry.register(path, value);
tensorflow.modelsValue[path] = value;
};
window.tensorflow = tensorflow;