In the last article on “How to build a Watson cognitive app using node-JS” we learnt how to get the SPSS model created and make it ready for the machine learning service.In this article we will learn, how to call that machine learning model and get prediction scores on Blue-mix.
This is right time to export the model stream which we generated in the last tutorial, you need to take that .str file and import it into the Machine learning service, To do that you need to bind the machine learning instance
Using Machine learning Model on Blue-mix
Login to the Blue-mix dashboard and create an IOT platform starter app, you can search for the boiler plate from the catalog page.
* In case you don’t have blue-mix access, you can always get a 30 day free trial.
Once your app is created, let it start for a while and click on the URL.
Once you click the URL you will be able to see the Node-Red flow page for that app.
Please note that when we use boiler plate, by default one node-red app is booted in the node JS run time,default memory is 512mb.
We will be connecting our node-red app also to the prediction service and we will also be creating a custom node JS app which will bind to the prediction service.
Now lets download the sample Drug node-JS app from Github and we will use that to make prediction calls.
Github URL
> git clone https://github.com/pmservice/drug-selection
Go to app folder and execute > node app.js
( I am assuming you have node running on your machine, else download the node from the nodejs.org)
Now you are ready to make a prediction call, but you might need to update the API key and auth token for this app to work.
Please note that you will get this from the Watson platform page , you can open this page by clicking on your app which was created with platform starter and to to apps and generate keys.
Also if you plan to use a raw boiler plate instead of the drug app, you might be interested in the Samples repository from the pmservice, but please make sure you patch this repository with the “/pm/v1” addition as mentioned in this comment , else you won’t be able to call the prediction service.
Lets create a new machine learning service instance and bind it to our app which was created with the boilerplate. Please note that there are two apps, one which is a node-red app and the other is our Github cloned app and our app will call the machine learning service directly and nod-red app can call the prediction service via the http post call as shown below.
https://ibm-watson-ml.mybluemix.net/pm/v1/score/yourcontextid?accesskey=yourkey
Catch here is to use the new version of service(v1) and not the old version
You may need a function node in the node-red flow and might need to create the payload before you start posting your http post requests.
Now we can use prediction service in two ways :
- HTTP Post via node-red
- Via our Node JS app using the SDK
HTTP Post from Node red to the IBM Watson machine learning service.
To make http post call, you will need a function node to generate the payload, prediction service is going to need two components ( content type and the msg payload)
To create the payload, drag and double click the function payload from the node-red palate
msg.header= {
Content-type: “application/json” }
msg.payload={"tablename":"DRUG1n","header":[“Age”,”Sex”,”BP”,”Cholesterol”,”Na”,”K”:[["35","M","HIGH",”HIGH”,”24”,”1.2”]]}
}
}
return msg;
The above function node should go to the http post node and the result can be parsed by a JSON node and later you can call alerts or take smart decisions based of the prediction score.
Your output from the http post should have
This will come as a JSON object and you can use if else to route the flow.
Via NodeJS app using the SDK
You can also call the prediction calls from your node app controller.
If you are using express you can use router.post and call the http post to the prediction service, else if you are using angular, you can bind a few screen elements to the model and run an ng-click on a button element to call an angular controller method.
for Express
For angular you will create a method in the $scope.yourMethod = function (){ }
this yourMethod will be bind to the ng-click of the button , you can either get the input values from the page or hardcode them and call this method.
Keep Reading !
No comments:
Post a Comment