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)
class ModelgRPCServiceServicer(model_service_pb2_grpc.ModelgRPCServiceServicer ): """Provides methods that implement functionality of Model gRPC Service.""" def __init__(self): """Initialize an instance of the service.""" self.model_manager = ModelManager() self.model_manager.load_models(configuration=Config.models) for model in self.model_manager.get_models(): endpoint = MLModelgRPCEndpoint( model_qualified_name=model["qualified_name"]) operation_name = "{}_predict".format(model["qualified_name"]) setattr(self, operation_name, endpoint) def get_models(self, request, context): """Return list of models hosted in this service.""" model_data = self.model_manager.get_models() models = [] for m in model_data: # creating a list of model protobufs from the model information returned by the model manager response_model = model( qualified_name=m["qualified_name"], display_name=m["display_name"], description=m["description"], major_version=m["major_version"], minor_version=m["minor_version"], input_type="{}_input".format(m["qualified_name"]), output_type="{}_output".format(m["qualified_name"]), predict_operation="{}_predict".format(m["qualified_name"])) models.append(response_model) # creating the response protobuf from the list created above response_models = model_collection() response_models.models.extend(models) return response_models
def __init__(self): """Initialize an instance of the service.""" self.model_manager = ModelManager() self.model_manager.load_models(configuration=Config.models) for model in self.model_manager.get_models(): endpoint = MLModelgRPCEndpoint( model_qualified_name=model["qualified_name"]) operation_name = "{}_predict".format(model["qualified_name"]) setattr(self, operation_name, endpoint)
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))
def __init__(self, model_qualified_name): """Create a gRPC 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 MLModelStreamProcessor. :rtype: MLModelStreamProcessor """ model_manager = ModelManager() self._model = model_manager.get_model(model_qualified_name) if self._model is None: raise ValueError("'{}' not found in ModelManager instance.".format( model_qualified_name)) logger.info("Initializing endpoint for model: {}".format( self._model.qualified_name))
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)
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." )
"""""" import argparse import jinja2 from model_grpc_service.config import Config from model_grpc_service.model_manager import ModelManager # instantiating the ModelManager singleton model_manager = ModelManager() model_manager.load_models(Config.models) # this dict maps the field types of the model schema to proto buffer types type_mappings = { "string": "string", "number": "float", "integer": "int64", "boolean": "bool", "null": "NullValue" } def main(output_file): template_loader = jinja2.FileSystemLoader(searchpath="./") template_env = jinja2.Environment(loader=template_loader) template = template_env.get_template("model_service_template.proto") # building a data structure to feed to the template from the models in ModelManager models = [] for model in model_manager.get_models(): model_details = model_manager.get_model_metadata( qualified_name=model["qualified_name"])