File size: 1,857 Bytes
4c01182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# This script wraps the ML model and the vectorizer into a single Python model object and 
# prepares it for registeration in MLflow. 
# This implementation is deprecated and is not used in the current version of the API (V2)

import pandas as pd
from mlflow.pyfunc.model import PythonModel

# Custom model class to wrap the ML model and vectorizer
class CustomModel(PythonModel):
    def __init__(self, model, vectorizer):
        """

        Initializes the CustomModel instance with a machine learning model and a vectorizer.



        Args:

            model: The machine learning model to be used for prediction.

            vectorizer: The vectorizer to transform input data for the model.

        """
        self.model = model
        self.vectorizer = vectorizer

    def predict(self, context, model_input: pd.DataFrame):
        
        """

        Predicts the class probability scores and class labels for the given input data.



        Args:

            context (dict): Context containing additional information that may be useful for prediction.

            model_input (pd.DataFrame): Input data containing the text column.

        

        Returns:

            dict: A dictionary containing the class probability scores and class labels.

        """
        texts = model_input["text"]
        if self.vectorizer is not None and self.model is not None:
            X = self.vectorizer.transform(texts)
            class_label = self.model.predict(X)

        return class_label
    
    def predict_proba(self, context, model_input: pd.DataFrame):        
        text = model_input["text"]
        if self.vectorizer is not None and self.model is not None:
            X = self.vectorizer.transform(text)
            class_proba = self.model.predict_proba(X)
        
        return class_proba