def test_model_joblib(): sklearn_model, data = _train_sample_model() model_file = os.path.join(JOBLIB_FILE[0], JOBLIB_FILE[1]) joblib.dump(value=sklearn_model, filename=model_file) model = SKLearnModel("model", JOBLIB_FILE[0]) model.load() request = data[0:1].tolist() response = model.predict({"instances": request}) assert response["predictions"] == [0]
def _run_pickle_model(model_dir, model_name): sklearn_model, data = _train_sample_model() model_file = os.path.join(model_dir, model_name) pickle.dump(sklearn_model, open(model_file, 'wb')) model = SKLearnModel("model", model_dir) model.load() request = data[0:1].tolist() response = model.predict({"instances": request}) assert response["predictions"] == [0]
def test_model(): iris = datasets.load_iris() X, y = iris.data, iris.target sklearn_model = svm.SVC(gamma='scale') sklearn_model.fit(X, y) model_file = os.path.join((model_dir), JOBLIB_FILE) joblib.dump(value=sklearn_model, filename=model_file) server = SKLearnModel("sklearnmodel", model_dir) server.load() request = X[0:1].tolist() response = server.predict(request) assert response == [0]
def test_mixedtype_model_joblib(): model = SKLearnModel("model", MIXEDTYPE_DIR) model.load() request = [{ 'MSZoning': 'RL', 'LotArea': 8450, 'LotShape': 'Reg', 'Utilities': 'AllPub', 'YrSold': 2008, 'Neighborhood': 'CollgCr', 'OverallQual': 7, 'YearBuilt': 2003, 'SaleType': 'WD', 'GarageArea': 548 }] response = model.predict({"instances": request}) assert response["predictions"] == [12.202832815138274]
async def load(self, name: str) -> bool: model = SKLearnModel(name, os.path.join(self.models_dir, name)) if model.load(): self.update(model) return model.ready
# limitations under the License. import argparse import logging import sys import kfserving from .kfserver import KFServer from sklearnserver import SKLearnModel, SKLearnModelRepository DEFAULT_MODEL_NAME = "model" DEFAULT_LOCAL_MODEL_DIR = "/tmp/model" parser = argparse.ArgumentParser(parents=[kfserving.kfserver.parser]) parser.add_argument('--model_dir', required=True, help='A URI pointer to the model binary') parser.add_argument('--model_name', default=DEFAULT_MODEL_NAME, help='The name that the model is served under.') args, _ = parser.parse_known_args() if __name__ == "__main__": model = SKLearnModel(args.model_name, args.model_dir) try: model.load() except Exception: ex_type, ex_value, _ = sys.exc_info() logging.error(f"fail to load model {args.model_name} from dir {args.model_dir}. " f"exception type {ex_type}, exception msg: {ex_value}") model.ready = False KFServer(registered_models=SKLearnModelRepository(args.model_dir)).start([model] if model.ready else [])
def test_dir_with_two_models(): model = SKLearnModel("model", MULTI_DIR) with pytest.raises(RuntimeError) as e: model.load() assert 'More than one model file is detected' in str(e.value)
def test_dir_with_incompatible_model(): model = SKLearnModel("model", _MODEL_DIR + "/pkl") with pytest.raises(ModuleNotFoundError) as e: model.load() assert 'No module named' in str(e.value)
def test_dir_with_no_model(): model = SKLearnModel("model", _MODEL_DIR) with pytest.raises(ModelMissingError): model.load()
def test_dir_with_no_model(): model = SKLearnModel("model", _MODEL_DIR) with pytest.raises(RuntimeError) as e: model.load() assert 'Missing Model File' in str(e.value)