How to publish a Model as a Microservice (MaaS)?

Introduction

In this tutorial we are implementing a predictive model as microservice.

The aim of the tutorial is to show how to implement this kind of custom services.


Following the MVC architectural pattern, we provide a base ModelService class which is base reference frame of platform MaaS (Model as a Service). So we encourage to use this common structure.


In this case, the example is made in python language, but the platform connector is made in springboot. This kind of implamentation is a more complex one but offers people with low software developing knowledge, making models as simple as possible. Also, as the supervision functionalities are in a common springboot application, they just need to focus on model production.

Reference Framework of MaaS


We recomend that he base Model Service class implements, at least, the following methods in order to have a common MaaS service interface and to connect with supervisor:


Method nameTypeDescription
save_preprocessorI/OSave preprocessor object to file
save_modelI/OSave model object to file
save_labelsI/OSave labels to file
transform_inTransformationTransform data received by rest to model input data structure
predictPredictionWrap model object prediction to have a common method
transform_outTransformationTransform output data of model to rest output data structure


Also, it is possible to implements more custom functionalities or overwrite the existents.

The aim of the "train" method implemented is to update model weights by new actual data, so it should be used just for this purpose. If more changes or a more complex training is needed, please, create a new version of the model.

Keep in mind that reference framework is continuously developing, so future changes are possible.


The model deployment

In this case, the model corresponds to a binary raining prediction model which from some metheorological data it will ofer a response with a classification of raining or not raining.

As it is an example, we provide a Data Simulator class which create random data and send them to a onesait platform ontologia (via DigitalClient, see /wiki/spaces/PT/pages/181207059 for more info).


Graphic description


The main steps followed from creation of model to deployment as microservice are:

1. Create Microservice Template

First step is to create the microservice in the platform. It is done in Microservices > CREATE.

This step will create a git repository with the example, which you can modify to implement your own model in the reference framework with minimmum effort.

2. Download template

From Git Repo you can download and modify the example and make all changes desired.

The Spring Boot wrapper project contains the model implementation project (made with flask and sklearn in python in this case). It is not necessary to change nothing in the code of this project, just connection configurations parameters.

3. Make some changes and push

The Model Implementation project (/source/flask/microservice folder) inside Spring Boot project is an independent project, so they can be developed and deployed in separated way, but we recommend develop them as same microservice for the moment. As it is a REST service it can be implemented in any programming language (Python in this case).

Develop the model and export to file. The aim of this step is to analyze data and create a model to be exported as file (.pickle in this case), along with helper functions and classes.

Also, if necessary, it is possible to create a preprocessor file (for general and preprocessing data transformation before prediction). It is importat to understand that this preprocessor wil be used before each prediction (one simple example of this may be Vectorizer tf/idf object in NLP models or Standard Scaler object in regression or deep learning models).

If labels are present, export them as file.

This step can be done in a Notebook, IDLE, etc.

Once the model, preprocessor and labels (of are necessaries) are exported, we recommend to place the files in the /data folder from where they will be read at application start.

In this step, the common methods should be implemented and the helper functions and classes should be placed propperly in the model project.

The ModelService class (/sources/flask/microservice/rest/modelservice.py) wraps the model created, so it will be the class which implements the model service logic.

This class is the only one it is necessary to modify. Here, it should be implemented the basic common methods.

Here is an example of some of them:

Prediction common functions implemented
	@logged
    def transform_in(self, in_data: dict):
        sorted_cols = ['hPa', 'hum', 'tmp0', 'tmp1']
        assert set(sorted_cols) == (set(in_data.keys())), "Invalid input data"
        in_array = np.array([in_data[col] for col in sorted_cols]).reshape(1, 4)
        return self.preprocessor.transform(in_array)
    
    @logged
    def predict(self, X, names = None, meta=None):
        return self.model.predict(X)

    @logged
    def transform_out(self, X):
        prediction = int(X[0])
        return dict(prediction=prediction, label=self.labels[str(prediction)])


Once all changes are done, push to update the Git repo.

4. Build microservice

This may take several minutes.


5. Deploy your model and use it!

Once model is deployed, it is possible to make HTTP requests to its implemented endpoints.