Wasm Runner Browser
About the Project
Run wasm models on browser, on server, on mobile
Demo
Link: https://jeengine.s3.ap-southeast-1.amazonaws.com/wasm/index.html
Getting Started
Install
To use WasmRunner SDK, run
npm install "@coherentglobal/wasm-runner"
or
yarn add "@coherentglobal/wasm-runner"
Usage
Browser ( HTML )
First we call the SDK
<script src="https://wasm-runner-sdk.s3.ap-southeast-1.amazonaws.com/wasmrunner.min.js"></script>
Then create our javascript that initializes the model and runs it.
const param = {
id: "f30f5935-36c2-4155-aede-523ab2245fd6",
url: "<zip url>",
};
const payload = {
request_data: {
inputs: {
age: 60,
insure: "Insure",
medical: "Yes",
plan: "Plan 1",
},
},
request_meta: {
service_uri: "",
service_uuid: "",
version: "",
version_uuid: "f30f5935-36c2-4155-aede-523ab2245fd6",
transaction_date: "2021-08-18T04:17:17.142Z",
call_purpose: "postman_request",
source_system: "",
correlation_id: "",
requested_output: "",
},
};
const wasmRunner = new WasmRunner(param);
await wasmRunner.initialize();
const response = await wasmRunner
.execute(payload)
.catch((err) => ({ error: err.v3 }));
// Do something with the response
React
We can call the SDK directly, then follow the normal process of initializing a model then calling the execute method for calculation.
import { WasmRunner } from "@coherentglobal/wasm-runner";
const param = {
id: "f30f5935-36c2-4155-aede-523ab2245fd6",
url: "<zip url>",
};
const payload = {
request_data: {
inputs: {
age: 60,
insure: "Insure",
medical: "Yes",
plan: "Plan 1",
},
},
request_meta: {
service_uri: "",
service_uuid: "",
version: "",
version_uuid: "f30f5935-36c2-4155-aede-523ab2245fd6",
transaction_date: "2021-08-18T04:17:17.142Z",
call_purpose: "postman_request",
source_system: "",
correlation_id: "",
requested_output: "",
},
};
const wasmRunner = new WasmRunner(param);
await wasmRunner.initialize();
const response = await wasmRunner
.execute(payload)
.catch((err) => ({ error: err.v3 }));
// Do something the response
NODE JS
We can call the SDK directly, then follow the normal process of initializing a model then calling the execute method for calculation.
const { WasmRunner } = require("@coherentglobal/wasm-runner");
const param = {
id: "f30f5935-36c2-4155-aede-523ab2245fd6",
url: "<zip url>",
};
const payload = {
request_data: {
inputs: {
age: 60,
insure: "Insure",
medical: "Yes",
plan: "Plan 1",
},
},
request_meta: {
service_uri: "",
service_uuid: "",
version: "",
version_uuid: "f30f5935-36c2-4155-aede-523ab2245fd6",
transaction_date: "2021-08-18T04:17:17.142Z",
call_purpose: "postman_request",
source_system: "",
correlation_id: "",
requested_output: "",
},
};
const wasmRunner = new WasmRunner(param);
await wasmRunner.initialize();
const response = await wasmRunner
.execute(payload)
.catch((err) => ({ error: err.v3 }));
Different Scenarios to Initialize a Model
Scenario 1
Config is initialized directly on WasmRunner
instance.
const param = {
id: "f30f5935-36c2-4155-aede-523ab2245fd6",
url: "<zip url>",
};
const payload = {
request_data: {
inputs: {
age: 60,
insure: "Insure",
medical: "Yes",
plan: "Plan 1",
},
},
request_meta: {
service_uri: "",
service_uuid: "",
version: "",
version_uuid: "f30f5935-36c2-4155-aede-523ab2245fd6",
transaction_date: "2021-08-18T04:17:17.142Z",
call_purpose: "postman_request",
source_system: "",
correlation_id: "",
requested_output: "",
},
};
const wasmRunner = new WasmRunner(param);
await wasmRunner.initialize();
const response = await wasmRunner
.execute(payload)
.catch((err) => ({ error: err.v3 }));
Scenario 2
Config is initialized via the append
method.
const param = {
id: "f30f5935-36c2-4155-aede-523ab2245fd6",
url: "<zip url>",
};
const payload = {
request_data: {
inputs: {
age: 60,
insure: "Insure",
medical: "Yes",
plan: "Plan 1",
},
},
request_meta: {
service_uri: "",
service_uuid: "",
version: "",
version_uuid: "f30f5935-36c2-4155-aede-523ab2245fd6",
transaction_date: "2021-08-18T04:17:17.142Z",
call_purpose: "postman_request",
source_system: "",
correlation_id: "",
requested_output: "",
},
};
const wasmRunner = new WasmRunner();
await wasmRunner.append(param);
await wasmRunner.initialize();
const response = await wasmRunner
.execute(payload)
.catch((err) => ({ error: err.v3 }));
Scenario 3
Config is initialized directly on WasmRunner
instance and append a new model via the append
method.
const param = {
id: "f30f5935-36c2-4155-aede-523ab2245fd6",
url: "<zip url>",
};
const param2 = {
id: "e8ba9e7e-169c-4752-8a8e-d6eb0c78cb01",
url: "<zip url>",
};
const payload = {
request_data: {
inputs: {
age: 60,
insure: "Insure",
medical: "Yes",
plan: "Plan 1",
},
},
request_meta: {
service_uri: "",
service_uuid: "",
version: "",
version_uuid: "f30f5935-36c2-4155-aede-523ab2245fd6",
transaction_date: "2021-08-18T04:17:17.142Z",
call_purpose: "postman_request",
source_system: "",
correlation_id: "",
requested_output: "",
},
};
const wasmRunner = new WasmRunner(param);
await wasmRunner.initialize();
await wasmRunner.append(param2);
const response = await wasmRunner
.execute(payload)
.catch((err) => ({ error: err.v3 }));
Development
Setup
Clone the repo, then install the dependencies
$ git clone https://github.com/CoherentCapital/wasm-runner-js.git
$ yarn install
Methods
(async) append(modelConfig)
Append and initialized new model to runner
await wasmRunner.append(param);
(async) execute(input)
Perform calculation. The runner will use request_meta.version_uuid of input argument to locate model
const response = await wasmRunner.execute(payload).catch((err) => {
// Do something here
});
isExist(id)
Check if model existed
if (wasmRunner.isExist(id)) {
// Do something here
}
remove(id)
Remove model from runner
wasmRunner.remove(id);
Initialize Model
There are two ways to initialize a model. First is when you instantiating a class,
const wasmRunner = new WasmRunner(param);
And the second way is by using the append method.
await wasmRunner.append(param);
Limitations
Lower Level Error
Please note that if there's lower level error that occurs, wasm runner can't handle that kind of exception.
FootNote
Sample Payload
{
"request_data": {
"inputs": {
"age": 60,
"insure": "Insure",
"medical": "Yes",
"plan": "Plan 1"
}
},
"request_meta": {
"service_uri": "",
"service_uuid": "",
"version": "",
"version_uuid": "<model id>",
"transaction_date": "2021-08-18T04:17:17.142Z",
"call_purpose": "postman_request",
"source_system": "",
"correlation_id": "",
"requested_output": ""
}
}