krish
krish

Reputation: 21

I am trying to serve a custom function as a model using ML Flow in Databricks

Below is my source code:-

import mlflow
import mlflow.sklearn
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.preprocessing import FunctionTransformer
from sklearn.pipeline import Pipeline
from mlflow.models.signature import infer_signature

import mlflow.pyfunc


class AddN(mlflow.pyfunc.PythonModel):

    def __init__(self, n):
        self.n = n
    def fit(self, X, y=None):
        return self
    def transform(self, X, y=None):
        return self
    def predict(model_input):
        return model_input.apply(lambda column: column + self.n)


model_path = "add_n_model15"
add5_model = AddN(n=5)
#mlflow.pyfunc.save_model(path=model_path, python_model=add5_model)


loaded_model = mlflow.pyfunc.load_model(model_path)


import pandas as pd
#model_input = pd.DataFrame([range(10)])
#model_output = loaded_model.predict(model_input)


data={'a':1,'b':2,'c':3,'d':4}
model_input = pd.DataFrame(data, index=[0])
loaded_model.predict(model_input)




pipeline = Pipeline(steps=[("model", AddN)])

with mlflow.start_run():
    
    # log model
    signature = infer_signature(model_input, model_output)
    mlflow.sklearn.log_model(pipeline, "trial", signature=signature)

` After registering the model and serving it in databricks, when i call the model through model serving interface of databricks using the below input Request, i get an error:-

Input Request:- [{"a":20,"b":21,"c":22,"d":23}]

Error:-

BAD_REQUEST: Encountered an unexpected error while evaluating the model. Verify that the serialized input Dataframe is compatible with the model for inference.

Traceback (most recent call last): File "/databricks/conda/envs/model-1/lib/python3.8/site-packages/mlflow/pyfunc/scoring_server/init.py", line 306, in transformation raw_predictions = model.predict(data) File "/databricks/conda/envs/model-1/lib/python3.8/site-packages/mlflow/pyfunc/init.py", line 612, in predict return self._model_impl.predict(data) File "/databricks/conda/envs/model-1/lib/python3.8/site-packages/sklearn/utils/metaestimators.py", line 120, in out = lambda *args, **kwargs: self.fn(obj, *args, **kwargs) File "/databricks/conda/envs/model-1/lib/python3.8/site-packages/sklearn/pipeline.py", line 419, in predict return self.steps[-1][-1].predict(Xt, **predict_params) TypeError: predict() missing 2 required positional arguments: 'context' and 'model_input' `

Upvotes: 2

Views: 2344

Answers (1)

HuyNA
HuyNA

Reputation: 628

From the error message you are missing 2 required positional arguments: 'context' and 'model_input'

You should change your AddN class to below

class AddN(mlflow.pyfunc.PythonModel):

    def __init__(self, n):
        self.n = n
    def fit(self, X, y=None):
        return self
    def transform(self, X, y=None):
        return self
    def predict(self, context, model_input):
        return model_input.apply(lambda column: column + self.n)

Upvotes: 3

Related Questions