-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload-model.js
61 lines (53 loc) · 1.72 KB
/
upload-model.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node
/**
* @file upload-model.js
* Upload a model based on a model descriptor json file
*
* @author Ryan Saweczko, [email protected]
*/
'use strict';
const fs = require('fs');
const path = require('path');
function usage()
{
console.log(`upload-model: upload a machine learning model to use with DCP.
Usage:
node upload-model.js /path/to/model.json`);
process.exit(1);
}
if (process.argv.length !== 3)
usage();
const modelInfo = JSON.parse(fs.readFileSync(process.argv[2], { encoding: 'utf-8' }));
const dirname = path.dirname(process.argv[2]);
const model = fs.readFileSync(dirname + '/' + modelInfo.model).toString('base64');
const preprocess = fs.readFileSync(dirname + '/' + modelInfo.preprocess).toString('base64');
const postprocess = fs.readFileSync(dirname + '/' + modelInfo.postprocess).toString('base64');
const bravojsModule = `
module.declare([], async function bootstrap_decl (require, exports, module) {
exports.model = "${model}";
exports.preprocess = "${preprocess}";
exports.postprocess = "${postprocess}";
exports.packages = ${JSON.stringify(modelInfo.packages)};
});
`
const packageText = `{
"name": "${modelInfo.name}",
"version": "${modelInfo.version}",
"files": {
"module.js": "module.js"
}
}
`
const dir = `upload-${Date.now()}`
fs.mkdirSync(dir);
fs.writeFileSync(dir+'/module.js', bravojsModule);
fs.writeFileSync(dir+'/package.dcp', packageText);
require('dcp-client').init().then(async () => {
try
{
await require('dcp/publish').publish(dir+'/package.dcp');
console.log('Model is published, may run a job using it');
}
catch (e) {/* publish wil log if error occurs, no need to double log */}
fs.rmSync(dir, { recursive: true });
})