/
predict.js
40 lines (32 loc) · 908 Bytes
/
predict.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';
const path = require('path');
const { cwd, pipeline, output } = require('./metadata.json');
function _requirePlugin(name) {
const mod = require(path.join(cwd, pipeline[name]));
if (mod && typeof mod.default === 'function') {
return mod.default;
}
return mod;
}
const modelDefineModule = _requirePlugin('modelDefine');
let dataProcessModule;
if (typeof pipeline.dataProcess === 'string') {
dataProcessModule = _requirePlugin('dataProcess');
}
const model = modelDefineModule(null, {
recoverPath: __dirname + '/model'
});
function predict(data) {
const sample = { data, label: null };
let future = model;
if (typeof dataProcessModule === 'function') {
future = future.then((m) => {
dataProcessModule(sample, {}, pipeline.dataProcessParams);
return m
});
}
return future.then((m) => {
m.predict(sample);
});
};
module.exports = predict;