(self, load=True)
Load a model from disk.

        This function currently only supports sklearn models saved to disk using pickle.
        The version of Python and sklearn used to build the model must match this SSE.

        A YAML file describing the model as explained in this project's documentation needs to be placed at '../models/'

        e.g.
        ---
        path: ../pretrained/HR-Attrition-v1.pkl
        type: sklearn
        features:
            department : str
            age : int
        ...

        Optional properties of the YAML definition include:
        preprocessor : path to a pickled preprocessor object that implements the transform method
Load a model from disk.
Load a model from disk .
def _get_model(self, load=True):
        """
        Load a model from disk.

        This function currently only supports sklearn models saved to disk using pickle.
        The version of Python and sklearn used to build the model must match this SSE.

        A YAML file describing the model as explained in this project's documentation needs to be placed at '../models/'

        e.g.
        ---
        path: ../pretrained/HR-Attrition-v1.pkl
        type: sklearn
        features:
            department : str
            age : int
        ...

        Optional properties of the YAML definition include:
        preprocessor : path to a pickled preprocessor object that implements the transform method
        """

        # Get the model name from the request dataframe
        model_name = self.request_df.loc[0, 'model_name']
        self.name = model_name
        self.is_rest_api = False

        # Get model meta data from the YAML file
        try:
            with open(self.path + model_name + ".yaml", 'r') as stream:
                model_meta = yaml.safe_load(stream)
        except FileNotFoundError as fe:
            err = "Model definition file not found. A YAML file with the model path, type and features needs to be placed in ../models/"
            raise FileNotFoundError(err) from fe
        
        # Get model path, type, preprocessor (optional), and feature definitions
        model_path, model_type, model_features = model_meta['path'], model_meta['type'].lower(), model_meta['features']
        
        # Check that the model type is supported
        supported = ['sklearn', 'scikit-learn', 'keras', 'rest', 'restful']
        assert model_type in supported, "Unsupported model type: {}".format(model_meta['type'])

        if load:
            # Get the preprocessor if required
            if 'preprocessor' in model_meta:
                prep_path = model_meta['preprocessor']
                self.prep = self._get_model_sklearn(prep_path)
            else:
                self.prep = None
            
            # Load the model
            if model_type in ['sklearn', 'scikit-learn']:
                self.model = self._get_model_sklearn(model_path)
            elif model_type in ['keras']:
                self.model = self._get_model_keras(model_path)
            elif model_type in ['rest', 'restful']:
                # Set a flag if the model is to be called using a REST API
                self.is_rest_api = True
                # The JSON response may contain multiple objects under the root. A specific result section can be specified in the YAML file
                self.response_section = None if 'response_section' not in model_meta else model_meta['response_section']
                # The endpoint key or user can be passed through the user tag in the YAML file
                model_user = model_meta['user']
                # The endpoint password can be passed through the password tag in the YAML file
                model_password = '' if 'password' not in model_meta else model_meta['password']
                # The data/payload can be nested within a header before sending the request e.g. 'features'
                model_payload = {} if 'payload_header' not in model_meta else {model_meta['payload_header']: {}}

                # For REST endpoints, the model is a dictionary of inputs for the HTTP POST request
                self.model = {'url':model_path, 'auth':(model_user, model_password), 'json':model_payload}
            
            # Debug information is printed to the terminal and logs if the paramater debug = true
            if self.debug:
                self._print_log(6)
        
        # Set model feature names and data types
        self.features_df = pd.DataFrame([model_features.keys(), model_features.values()]).T
        self.features_df.columns = ['name', 'data_type']
        self.features_df = self.features_df.set_index('name', drop=False)
        self.features_df.loc[:,'variable_type'] = 'feature'

        # Debug information is printed to the terminal and logs if the paramater debug = true
        if self.debug:
            self._print_log(7)
def _get_model ( self , load = True ) : # Get the model name from the request dataframe model_name = self . request_df . loc [ 0 , 'model_name' ] self . name = model_name self . is_rest_api = False # Get model meta data from the YAML file try : with open ( self . path + model_name + ".yaml" , 'r' ) as stream : model_meta = yaml . safe_load ( stream ) except FileNotFoundError as fe : err = "Model definition file not found. A YAML file with the model path, type and features needs to be placed in ../models/" raise FileNotFoundError ( err ) from fe # Get model path, type, preprocessor (optional), and feature definitions model_path , model_type , model_features = model_meta [ 'path' ] , model_meta [ 'type' ] . lower ( ) , model_meta [ 'features' ] # Check that the model type is supported supported = [ 'sklearn' , 'scikit-learn' , 'keras' , 'rest' , 'restful' ] assert model_type in supported , "Unsupported model type: {}" . format ( model_meta [ 'type' ] ) if load : # Get the preprocessor if required if 'preprocessor' in model_meta : prep_path = model_meta [ 'preprocessor' ] self . prep = self . _get_model_sklearn ( prep_path ) else : self . prep = None # Load the model if model_type in [ 'sklearn' , 'scikit-learn' ] : self . model = self . _get_model_sklearn ( model_path ) elif model_type in [ 'keras' ] : self . model = self . _get_model_keras ( model_path ) elif model_type in [ 'rest' , 'restful' ] : # Set a flag if the model is to be called using a REST API self . is_rest_api = True # The JSON response may contain multiple objects under the root. A specific result section can be specified in the YAML file self . response_section = None if 'response_section' not in model_meta else model_meta [ 'response_section' ] # The endpoint key or user can be passed through the user tag in the YAML file model_user = model_meta [ 'user' ] # The endpoint password can be passed through the password tag in the YAML file model_password = '' if 'password' not in model_meta else model_meta [ 'password' ] # The data/payload can be nested within a header before sending the request e.g. 'features' model_payload = { } if 'payload_header' not in model_meta else { model_meta [ 'payload_header' ] : { } } # For REST endpoints, the model is a dictionary of inputs for the HTTP POST request self . model = { 'url' : model_path , 'auth' : ( model_user , model_password ) , 'json' : model_payload } # Debug information is printed to the terminal and logs if the paramater debug = true if self . debug : self . _print_log ( 6 ) # Set model feature names and data types self . features_df = pd . DataFrame ( [ model_features . keys ( ) , model_features . values ( ) ] ) . T self . features_df . columns = [ 'name' , 'data_type' ] self . features_df = self . features_df . set_index ( 'name' , drop = False ) self . features_df . loc [ : , 'variable_type' ] = 'feature' # Debug information is printed to the terminal and logs if the paramater debug = true if self . debug : self . _print_log ( 7 )