def test_incorrect_service(self): from mms.model_service.model_service import load_service path = os.getcwd() try: load_service( os.path.join( path, 'mms/tests/unit_tests/helper/incorrect_service.py')) except Exception as e: assert "No module" in str(e) print(e)
def validate_service(model_path, service_file, signature_file): """ Validate the service :param model_path: :param service_file: :param signature_file: :return: """ is_gluon_service = False if service_file: assert os.path.isfile(service_file) or os.path.isfile(os.path.join(model_path, service_file)), \ "Service File not found in %s or in %s." % (service_file, model_path) service_file = service_file if os.path.isfile(service_file) \ else glob.glob(model_path + service_file)[0] module = load_service(service_file) classes = [cls[1] for cls in inspect.getmembers(module, inspect.isclass)] # Check if subclass of MXNetBaseService or GluonImperativeBaseService # pylint: disable=deprecated-lambda service_classes_mxnet = list(filter(lambda cls: issubclass(cls, MXNetBaseService), classes)) service_classes_gluon = list(filter(lambda cls: issubclass(cls, GluonImperativeBaseService), classes)) is_mxnet_service = len(service_classes_mxnet) > 1 is_gluon_service = len(service_classes_gluon) > 1 assert (len(service_classes_mxnet) > 1 or len(service_classes_gluon) > 1), \ "The Service class should be derived from MXNetBaseService or GluonImperativeBaseService, " \ "found %s classes" % str(service_classes_mxnet) if is_gluon_service and is_mxnet_service: raise ValueError("Service file contains both symbolic and imperative sub-classes. The service file" "should contain either MXNetBaseSerivce class or GluonImperativeBaseService class") # remove the compiled python code if os.path.exists(service_file + 'c'): os.remove(service_file + 'c') else: input_type = None with open(signature_file) as js_file: input_type = json.load(js_file)['input_type'] if input_type not in VALID_MIME_TYPE: raise ValueError("input_type should be one of %s or have your own service file handling it" % str(VALID_MIME_TYPE)) service_file = MMS_SERVICE_FILES[input_type] if not os.path.exists(service_file): raise ValueError('Service File {} is missing in mms installation' .format(os.path.basename(service_file))) return is_gluon_service, service_file
def validate_service(model_path, service_file, signature_file): if service_file: assert os.path.isfile(service_file) or os.path.isfile(os.path.join(model_path, service_file)), \ "Service File not found in %s or in %s." % (service_file, model_path) service_file = service_file if os.path.isfile(service_file) \ else glob.glob(model_path + service_file)[0] module = load_service(service_file) classes = [ cls[1] for cls in inspect.getmembers(module, inspect.isclass) ] # Check if subclass of MXNetBaseService service_classes = list( filter(lambda cls: issubclass(cls, MXNetBaseService), classes)) assert len(service_classes) > 1, \ "The Service class should be derived from MXNetBaseService, found %s classes" % str(service_classes) #remove the compiled python code if os.path.exists(service_file + 'c'): os.remove(service_file + 'c') else: input_type = None with open(signature_file) as js_file: input_type = json.load(js_file)['input_type'] if input_type == 'image/jpeg': service_file = vision_service.__file__ elif input_type == 'application/json': service_file = base_service.__file__ else: assert input_type in VALID_MIME_TYPE, \ "input_type should be one of %s or have your own service file handling it" % str(VALID_MIME_TYPE) #if service_file is a compile file, get the corresponding python code file if service_file.endswith('.pyc'): service_file = service_file[:-1] assert os.path.isfile(service_file), \ "Vision Service File is missing in mms installation" return service_file
def parse_modelservices_from_module(self, service_file): """ Parse user defined module to get all model service classe in it. Parameters ---------- service_file : User defined module file path A python module which will be parsed by given name. Returns ---------- List of model service class definitions. Those parsed python class can be used to initialize model service. """ module = load_service(service_file) if service_file else mxnet_model_service # Parsing the module to get all defined classes classes = [cls[1] for cls in inspect.getmembers(module, inspect.isclass)] # Check if class is subclass of base ModelService class return list(filter(lambda cls: issubclass(cls, MXNetBaseService), classes))
def validate_service(model_path, service_file, signature_file): if service_file: assert os.path.isfile(service_file) or os.path.isfile(os.path.join(model_path, service_file)), \ "Service File not found in %s or in %s." % (service_file, model_path) service_file = service_file if os.path.isfile(service_file) \ else glob.glob(model_path + service_file)[0] module = load_service(service_file) classes = [ cls[1] for cls in inspect.getmembers(module, inspect.isclass) ] # Check if subclass of MXNetBaseService service_classes = list( filter(lambda cls: issubclass(cls, MXNetBaseService), classes)) assert len(service_classes) > 1, \ "The Service class should be derived from MXNetBaseService, found %s classes" % str(service_classes) # remove the compiled python code if os.path.exists(service_file + 'c'): os.remove(service_file + 'c') else: input_type = None with open(signature_file) as js_file: input_type = json.load(js_file)['input_type'] if input_type not in VALID_MIME_TYPE: raise ValueError( "input_type should be one of %s or have your own service file handling it" % str(VALID_MIME_TYPE)) service_file = MMS_SERVICE_FILES[input_type] if not os.path.exists(service_file): raise ValueError( 'Service File {} is missing in mms installation'.format( os.path.basename(service_file))) return service_file
def parse_modelservices_from_module(self, service_file): """ Parse user defined module to get all model service classe in it. Parameters ---------- service_file : User defined module file path A python module which will be parsed by given name. Returns ---------- List of model service class definitions. Those parsed python class can be used to initialize model service. """ module = load_service( service_file) if service_file else mxnet_model_service # Parsing the module to get all defined classes classes = [ cls[1] for cls in inspect.getmembers(module, inspect.isclass) ] # Check if class is subclass of base ModelService class return list( filter(lambda cls: issubclass(cls, MXNetBaseService), classes))
def validate_service(model_path, service_file, signature_file): if service_file: assert os.path.isfile(service_file) or os.path.isfile(os.path.join(model_path, service_file)), \ "Service File not found in %s or in %s." % (service_file, model_path) service_file = service_file if os.path.isfile(service_file) \ else glob.glob(model_path + service_file)[0] module = load_service(service_file) classes = [cls[1] for cls in inspect.getmembers(module, inspect.isclass)] # Check if subclass of MXNetBaseService service_classes = list(filter(lambda cls: issubclass(cls, MXNetBaseService), classes)) assert len(service_classes) > 1, \ "The Service class should be derived from MXNetBaseService, found %s classes" % str(service_classes) # remove the compiled python code if os.path.exists(service_file + 'c'): os.remove(service_file + 'c') else: input_type = None with open(signature_file) as js_file: input_type = json.load(js_file)['input_type'] if input_type not in VALID_MIME_TYPE: raise ValueError("input_type should be one of %s or have your own service file handling it" % str(VALID_MIME_TYPE)) service_file = MMS_SERVICE_FILES[input_type] if not os.path.exists(service_file): raise ValueError('Service File {} is missing in mms installation' .format(os.path.basename(service_file))) return service_file