Last active
September 22, 2021 18:21
-
-
Save PennyQ/99d5869b73af83fcc2334bf97e5cb63d to your computer and use it in GitHub Desktop.
A wrapper example for fasttext models in MLflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fasttext | |
import mlflow.pyfunc | |
class FastTextWrapper(mlflow.pyfunc.PythonModel): | |
""" | |
Class to train and use FastText Models | |
""" | |
def load_context(self, context): | |
"""This method is called when loading an MLflow model with pyfunc.load_model(), as soon as the Python Model is constructed. | |
Args: | |
context: MLflow context where the model artifact is stored. | |
""" | |
import fasttext | |
self.model = fasttext.load_model(context.artifacts["fasttext_model_path"]) | |
def predict(self, context, model_input): | |
"""This is an abstract function. We customized it into a method to fetch the FastText model. | |
Args: | |
context ([type]): MLflow context where the model artifact is stored. | |
model_input ([type]): the input data to fit into the model. | |
Returns: | |
[type]: the loaded model artifact. | |
""" | |
return self.model |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment