Search code examples
python-3.xmlflowmlops

Serve online learning models with mlflow


It is not clear to me if one could use mlflow to serve a model that is evolving continuously based on its previous predictions.

I need to be able to query a model in order to make a prediction on a sample of data which is the basic use of mlflow serve. However I also want the model to be updated internaly now that it has seen new data.

Is it possible or does it need a FR ?


Solution

  • I think that you should be able to do that by implementing the custom python model or custom flavor, as it's described in the documentation. In this case you need to create a class that is inherited from mlflow.pyfunc.PythonModel, and implement the predict method, and inside that method you're free to do anything. Here is just simple example from documentation:

    class AddN(mlflow.pyfunc.PythonModel):
    
        def __init__(self, n):
            self.n = n
    
        def predict(self, context, model_input):
            return model_input.apply(lambda column: column + self.n)
    

    and this model is then could be saved & loaded again just as normal models:

    # Construct and save the model
    model_path = "add_n_model"
    add5_model = AddN(n=5)
    mlflow.pyfunc.save_model(path=model_path, python_model=add5_model)
    
    # Load the model in `python_function` format
    loaded_model = mlflow.pyfunc.load_model(model_path)