def test_auto_artifact_dependencies(): clf = svm.SVC(gamma='scale') iris = datasets.load_iris() X, y = iris.data, iris.target clf.fit(X, y) # Create a iris classifier service iris_classifier_service = IrisClassifier() # Pack it with the newly trained model artifact iris_classifier_service.pack('model', clf) # Save the prediction service to a BentoService bundle saved_path = iris_classifier_service.save() with open(os.path.join(saved_path, 'requirements.txt')) as f: requirements_txt_content = f.read() dependencies = requirements_txt_content.split('\n') dependencies = [dep.split('==')[0] for dep in dependencies] assert 'scikit-learn' in dependencies assert 'bentoml' in dependencies # Test that dependencies also wrote to BentoServiceMetadat config file bs_matadata = load_bento_service_metadata(saved_path) dependencies = bs_matadata.env.pip_dependencies.split('\n') dependencies = [dep.split('==')[0] for dep in dependencies] assert 'scikit-learn' in dependencies assert 'bentoml' in dependencies delete_saved_bento_service(iris_classifier_service.name, iris_classifier_service.version)
def run_test_with_bento_service_class(bento_service_class): clf = svm.SVC(gamma='scale') iris = datasets.load_iris() X, y = iris.data, iris.target clf.fit(X, y) # Create a bento service instance bento_service = bento_service_class() # Pack it with the newly trained model artifact bento_service.pack('model', clf) # Save the prediction service to a BentoService bundle bento_service.save() bento_name = f"{bento_service.name}:{bento_service.version}" run_command = [ "bentoml", "run", bento_name, "predict", "--input", "[[5.1, 3.5, 1.4, 0.2]]", "-q", ] print(f"running command {' '.join(run_command)}:") with subprocess.Popen(run_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc: output = proc.stdout.read().decode('utf-8') assert output.replace('\r\n', '\n') == '[0]\n' delete_saved_bento_service(bento_service.name, bento_service.version)
def run_predict(bento_service, input_data, is_file=False, options=[]): bento_name = f"{bento_service.name}:{bento_service.version}" run_predict_deployment_command = [ 'bentoml', 'run', bento_name, 'predict', *options, '--input-file' if is_file else '--input', input_data, ] logger.info(f'Run predict command: {run_predict_deployment_command}') try: with subprocess.Popen( run_predict_deployment_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=os.environ, ) as proc: stdout = proc.stdout.read().decode('utf-8') stderr = proc.stderr.read().decode('utf-8') logger.info(f'Got output: {stdout}') return stdout, stderr finally: delete_saved_bento_service(bento_service.name, bento_service.version)
def test_auto_artifact_dependencies(): clf = _fit_clf() # Create a iris classifier service iris_classifier_service = IrisClassifier() # Pack it with the newly trained model artifact iris_classifier_service.pack('model', clf) # Save the prediction service to a BentoService bundle saved_path = iris_classifier_service.save() # parse generated requirements.txt dependencies = _dependencies_to_requirements( _parse_dependencies(saved_path)) _assert_in_dependencies(['scikit-learn', 'bentoml'], dependencies) # Test that dependencies also wrote to BentoServiceMetadata config file bs_metadata = load_bento_service_metadata(saved_path) dependencies = bs_metadata.env.pip_packages dependencies = _dependencies_to_requirements(dependencies) _assert_in_dependencies(['scikit-learn', 'bentoml'], dependencies) # Clean up delete_saved_bento_service(iris_classifier_service.name, iris_classifier_service.version)
def test_requirements_txt_file(): clf = _fit_clf() iris_classifier_service = IrisClassifierPipEnv() iris_classifier_service.pack('model', clf) saved_path = iris_classifier_service.save() dependencies = _dependencies_to_requirements( _parse_dependencies(saved_path)) _assert_in_dependencies( ['scikit-learn', 'azure-cli', 'psycopg2-binary', 'bentoml'], dependencies) bs_metadata = load_bento_service_metadata(saved_path) requirements_txt = bs_metadata.env.requirements_txt requirements_content = open("./tests/pipenv_requirements.txt", "r").read() assert requirements_txt == requirements_content delete_saved_bento_service(iris_classifier_service.name, iris_classifier_service.version)
def test_save_duplicated_bento_exception_raised(example_bento_service_class): test_model = TestModel() svc = example_bento_service_class() svc.pack("model", test_model) saved_path = svc.save() svc_metadata = load_bento_service_metadata(saved_path) assert svc.version == svc_metadata.version with pytest.raises(BentoMLException): with patch.object(bentoml.BentoService, 'save_to_dir') as save_to_dir_method: # attempt to save again svc.save() save_to_dir_method.assert_not_called() # reset svc version svc.set_version() saved_path = svc.save() svc_metadata_new = load_bento_service_metadata(saved_path) assert svc.version == svc_metadata_new.version delete_saved_bento_service(svc_metadata.name, svc_metadata.version) delete_saved_bento_service(svc_metadata_new.name, svc_metadata_new.version)
def test_bundle_local_dependencies(): clf = svm.SVC(gamma='scale') iris = datasets.load_iris() X, y = iris.data, iris.target clf.fit(X, y) # Create a iris classifier service iris_classifier_service = IrisClassifier() # Pack it with the newly trained model artifact iris_classifier_service.pack('model', clf) # Save the prediction service to a BentoService bundle iris_classifier_service.save() bento_name = f"{iris_classifier_service.name}:{iris_classifier_service.version}" run_command = [ "bentoml", "run", bento_name, "predict", "--input", "[[5.1, 3.5, 1.4, 0.2]]", "-q", ] print(f"running command {' '.join(run_command)}:") with subprocess.Popen(run_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc: output = proc.stdout.read().decode('utf-8') assert output == '[0]\n' delete_saved_bento_service(iris_classifier_service.name, iris_classifier_service.version)