class ModelZeroRPCService(object):
    """Provides methods that implement functionality of Model ZeroRPC Service."""

    def __init__(self):
        """Initialize an instance of the service."""
        self.model_manager = ModelManager()
        self.model_manager.load_models(configuration=configuration.models)

        for model in self.model_manager.get_models():
            endpoint = MLModelZeroRPCCEndpoint(model_qualified_name=model["qualified_name"])
            operation_name = "{}_predict".format(model["qualified_name"])
            setattr(self, operation_name, endpoint)

    def get_models(self):
        """Return list of models hosted in this service."""
        # retrieving the list of models from the model manager
        models = self.model_manager.get_models()
        return models

    def get_model_metadata(self, qualified_name):
        """Return metadata about a model hosted by the service."""
        model_metadata = self.model_manager.get_model_metadata(qualified_name=qualified_name)
        if model_metadata is not None:
            return model_metadata
        else:
            raise ValueError("Metadata not found for this model.")
示例#2
0
    def test1(self):
        """ testing the load_models() method """
        # arrange
        # instantiating the model manager class
        model_manager = ModelManager()
        # loading the MLModel objects from configuration
        model_manager.load_models(configuration=[{
            "module_name": "tests.model_manager_test",
            "class_name": "MLModelMock"
        }])

        # act
        exception_raised = False
        model_object = None
        # accessing the MLModelMock model object
        try:
            model_object = model_manager.get_model(
                qualified_name="qualified_name")
        except Exception as e:
            exception_raised = True
            print_tb(e)

        # assert
        self.assertFalse(exception_raised)
        self.assertTrue(model_object is not None)
示例#3
0
    def test2(self):
        """testing the __init__() method with missing model"""
        # arrange
        # instantiating the model manager class
        model_manager = ModelManager()
        # loading the MLModel objects from configuration
        model_manager.load_models(configuration=[{
            "module_name": "tests.model_manager_test",
            "class_name": "MLModelMock"
        }])

        # act
        exception_raised = False
        exception_message = None
        try:
            endpoint = MLModelZeroRPCCEndpoint(model_qualified_name="asdf")
        except Exception as e:
            exception_message = str(e)
            print(exception_message)
            exception_raised = True

        # assert
        self.assertTrue(exception_raised)
        self.assertTrue(
            exception_message == "'asdf' not found in ModelManager instance.")
    def __init__(self):
        """Initialize an instance of the service."""
        self.model_manager = ModelManager()
        self.model_manager.load_models(configuration=configuration.models)

        for model in self.model_manager.get_models():
            endpoint = MLModelZeroRPCCEndpoint(model_qualified_name=model["qualified_name"])
            operation_name = "{}_predict".format(model["qualified_name"])
            setattr(self, operation_name, endpoint)
示例#5
0
    def test2(self):
        """testing that the ModelManager will return the same instance of an MLModel class from several different
        instances of ModelManager"""
        # arrange
        # instantiating the model manager class
        first_model_manager = ModelManager()

        # loading the MLModel objects from configuration
        first_model_manager.load_models(
            configuration=[{
                "module_name": "tests.model_manager_test",
                "class_name": "MLModelMock"
            }])

        # act
        first_model_object = first_model_manager.get_model(
            qualified_name="iris_model")

        # instantiating the ModelManager class again
        second_model_manager = ModelManager()

        second_model_object = second_model_manager.get_model(
            qualified_name="iris_model")

        # assert
        self.assertTrue(str(first_model_object) == str(second_model_object))
示例#6
0
    def test1(self):
        """testing the __init__() method"""
        # arrange
        # instantiating the model manager class
        model_manager = ModelManager()
        # loading the MLModel objects from configuration
        model_manager.load_models(configuration=[{
            "module_name": "tests.model_manager_test",
            "class_name": "MLModelMock"
        }])

        # act
        endpoint = MLModelZeroRPCCEndpoint(
            model_qualified_name="qualified_name")

        # assert
        self.assertTrue(
            str(type(endpoint._model)) ==
            "<class 'tests.model_manager_test.MLModelMock'>")
示例#7
0
    def test4(self):
        """ testing that the ModelManager returns None when a model is not found """
        # arrange
        model_manager = ModelManager()
        model_manager.load_models(configuration=[{
            "module_name": "tests.model_manager_test",
            "class_name": "MLModelMock"
        }])

        # act
        exception_raised = False
        exception_message = ""
        try:
            model = model_manager.get_model(qualified_name="asdf")
        except Exception as e:
            exception_raised = True
            exception_message = str(e)

        # assert
        self.assertFalse(exception_raised)
        self.assertTrue(model is None)
示例#8
0
    def test3(self):
        """ testing that the ModelManager only allows MLModel objects to be stored """
        # arrange
        model_manager = ModelManager()

        # act
        exception_raised = False
        exception_message = ""
        try:
            model_manager.load_models(configuration=[{
                "module_name": "tests.model_manager_test",
                "class_name":
                "SomeClass"  # using the class defined at the top of this file to test
            }])
        except Exception as e:
            exception_raised = True
            exception_message = str(e)

        # assert
        self.assertTrue(exception_raised)
        self.assertTrue(
            exception_message ==
            "The ModelManager can only hold references to objects of type MLModel."
        )
    def __init__(self, model_qualified_name):
        """Create a ZeroRPC endpoint for a model.

        :param model_qualified_name: The qualified name of the model that will be hosted in this endpoint.
        :type model_qualified_name: str
        :returns: An instance of MLModelZeroRPCCEndpoint.
        :rtype: MLModelZeroRPCCEndpoint

        """
        model_manager = ModelManager()

        model_instance = model_manager.get_model(model_qualified_name)

        if model_instance is None:
            raise ValueError("'{}' not found in ModelManager instance.".format(
                model_qualified_name))

        self._model = model_manager.get_model(model_qualified_name)

        # overriding the docstring of the object
        self.__doc__ = "Predict with the {}.".format(self._model.display_name)

        logger.info("Initializing endpoint for model: {}".format(
            self._model.qualified_name))