-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCallPythonMLCodes.js
83 lines (76 loc) · 3.07 KB
/
CallPythonMLCodes.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* @License Starts
*
* Copyright © 2015 - present. MongoExpUser
*
* License: MIT - See: https://github.com/MongoExpUser/Shale-Reservoir-DNN-and-Drilling-Rare-Events-Graph/blob/master/README.md
*
* @License Ends
*
*
* ...Ecotert's CallPythonMLCodes.js (released as open-source under MIT License) implements:
*
*
* Calling of machine learning codes (or any other code) written in Python from Node.js.
*
* The motivation, for calling of machine learning codes written in Python from Node.js,
* is to prevent re-inventing/re-creating of existing codes in Python.
* This way, existing machine learning codes written in Python can easily be used within
* asynchronous Node.js server and integrated with TensorFlow.js codes.
*
*
*/
class CallPythonMLCodes
{
constructor()
{
return null;
}
callPythonMLScriptFromNodeJS(pyFile, pyScriptPath, pyVersionPath, pyMode)
{
var {PythonShell} = require('python-shell'); // ver 1.0.8 or above
pyFile = pyFile
pyScriptPath = pyScriptPath;
pyVersionPath = pyVersionPath;
pyMode = pyMode;
var value1 = 'build_ext'; // command arg 1: for cython_C_extension
var value2 = '--inplace'; // command arg 2: for cython_C_extension
var value = 0; // general arg: any valid datatype/object can be passed in
//var pyArgs = [value1, value2]; // arguments to python script: use this for cython
//var pyArgs = [value, value, value]; // arguments to python script: use this for pure python with args
var pyArgs = []; // arguments to python script: use this for pure python with no args
var options = {mode: pyMode, pythonPath: pyVersionPath, scriptPath: pyScriptPath, args: pyArgs, pythonOptions: ["-u"]};
var pyShell = PythonShell.run(pyFile, options, function (error)
{
if(error)
{
console.log("Error running Script:", error);
return;
}
});
pyShell.on('message', function (message)
{
console.log(message);
});
pyShell.end(function (error, code, signal)
{
if(error) {console.log(error);}
console.log("end of script");
});
}
}
class TestMLCall
{
constructor(test=true)
{
if(test === true)
{
var pyFile = 'fileName.py'
var pyScriptPath = "./"; // or any other path to file
var pyVersionPath = "/usr/bin/python3.7"; // or any other path e.g ".../miniconda3/bin/python3.7
var pyMode = "text"; // or "json" or "binary"
var cpml = new CallPythonMLCodes();
cpml.callPythonMLScriptFromNodeJS(pyFile, pyScriptPath, pyVersionPath, pyMode);
}
}
}
module.exports = {CallPythonMLCodes};