Enterprise-friendly Java client for Microsoft Machine Learning Server
Machine Learning Server Operationalization allows users to develop powerful R/Python machine learning models and publish them as 'web-services'. These web-services then can be consumed by different types of clients. Users can use mrsdeploy R package on their client machines to perform 'remote execution' to create remote R sessions on a Server with Machine Learning Server installed and develop their models. These models then can be published to the server with a single R function. For python users, azureml-model-management-sdk package enables users to publish Python models as a 'web-service' within minutes.
While we have R and Python client packages for the users, with this blog, we will see it is very easy to create a Java client as well. This Java client will make 'remote-execution' and model management features accessible for huge number of Enterprise customer and user base from within their Java applications. You can find the Java client code here - https://github.com/Microsoft/microsoft-r/tree/master/mlserver-java-clients. This Java client is not production-ready, but I hope it will be helpful to get you started with your Java applications using Microsoft Machine Learning Server.
1. Java Client for Accessing Machine Learning REST APIs:
Machine Learning Server expose powerful remote execution and model management features through these REST APIs. The swagger document on this page defines a contract for accessing these APIs. We will use this swagger document to generate our Java Client.
Copy the swagger document content
Go to https://editor.swagger.io and paste the content in the editor
Go to tab "Generate Client" and click Java
Extract and open contents of downloaded zip file "java-client-generated.zip"
As you will see, the downloaded file is a Java-gradle project.Inside the src/main/java folder:
The "io.swagger.client.api" package consists of all the classes representing sets of REST APIs. For example:
- SessionsAPIsApi.java contains methods to access REST APIs to create, close and run code inside a R/Python session.
- ServicesManagementAPIsApi.java contains methods to list, publish, update and delete the web-services and so on.
The "io.swagger.client.model" package consists of all the classes that represent input and output objects to the APIs.
For demo purposes, I will be adding 2 classes:
This GUI based app will have option to login to a remote server with Machine Learning installed and once logged in, users will be able to run R/Python code on remote server and see results on the window. Here are some screenshots:
Once you are happy with the model created in this remote session, you can capture this session as a "Snapshot". Click the "Get Snapshot Id" button to get the Snapshot ID. Click "Copy" to get this snapshot ID on clipboard.
Now that you have the snapshot ID, lets go ahead and launch the second app - "ServiceManagementApp" to publish our rxLogitModel as a web service.
- This GUI based app will have option to login to the same server, where we got the snapshot ID from. Once logged in, you can provide the service name, version, the code to execute the service, the input and output parameter definitions and publish the service.
- Click on the "Publish Service" button. Once the publish is successful, you will see success message as well as, you will see list of published services to the right.
2. Java Client to consume the published web service:
For consuming an R web-service, you can use mrsdeploy R package or use azureml-model-management-sdk Python package for a Python web-service. But here, we will see how we can generate a Java-Client to consume our "rxLogitService" web service.
- Go to https://editor.swagger.io and paste the swagger for the service that we got from "ServiceManagementApp" in the last step, in the web page's editor.
- Go to "Generate Client" tab and click on "Java".
- Extract and open contents of downloaded zip file "java-client-generated.zip"
- As you will see, the downloaded file is a Java-gradle project.Inside the src/main/java folder:
- The "io.swagger.client.api" folder contains "RxLogitServiceApi.java" class representing our published Service.
- The "io.swagger.client.model" folder contains classes managing authentication to the server and input/output parameters etc.
- Let's write our class with Main() method to consume the web-service. There are couple of things we would do before consuming the service in our class:
- Login the server first: The web-service can be consumed only by an authenticated client. We will use "io.swagger.client.model.LoginRequest" class and authenticate with the server.
- Once logged in, we will update the ApiClient to include acquired token as "Bearer <token>" in Authorization header for every subsequent requests.
- Now, lets create the input dataframe for the web-service. A dataframe in R/Python is a HashMap<string, object> in Java and Dictionary<string, object> in C#.