def test_image_input_cli(capsys, img_file): test_image_input = ImageInput() test_args = ["--input={}".format(img_file)] test_image_input.handle_cli(test_args, predict) out, err = capsys.readouterr() assert out.strip().endswith("(10, 10, 3)")
def test_image_input_http_request_post_binary(img_file): test_image_input = ImageInput() request = mock.MagicMock(spec=flask.Request) request.method = "POST" request.files = {} request.headers = {} request.get_data.return_value = open(str(img_file), 'rb') response = test_image_input.handle_request(request, predict) assert response.status_code == 200 assert "[10, 10, 3]" in str(response.response)
def test_image_input_http_request_malformatted_input_missing_image_file(): test_image_input = ImageInput(input_names=("my_image",)) request = mock.MagicMock(spec=flask.Request) request.method = "POST" request.files = {} request.headers = {} request.get_data.return_value = None with pytest.raises(BadInput) as e: test_image_input.handle_request(request, predict) assert "unexpected HTTP request format" in str(e.value)
def test_image_input_aws_lambda_event(img_file): test_image_input = ImageInput() with open(str(img_file), "rb") as image_file: content = image_file.read() try: image_bytes_encoded = base64.encodebytes(content) except AttributeError: image_bytes_encoded = base64.encodestring(str(img_file)) aws_lambda_event = { "body": image_bytes_encoded, "headers": {"Content-Type": "images/png"}, } aws_result = test_image_input.handle_aws_lambda_event(aws_lambda_event, predict) assert aws_result["statusCode"] == 200 assert aws_result["body"] == "[10, 10, 3]"
def test_image_input_http_request_single_image_different_name(img_file): test_image_input = ImageInput(input_names=("my_image",)) request = mock.MagicMock(spec=flask.Request) file_attr = { 'filename': 'test_img.png', 'read.return_value': open(str(img_file), 'rb').read(), } file = mock.Mock(**file_attr) request.method = "POST" request.files = {"a_differnt_name_used": file} request.headers = {} request.get_data.return_value = None response = test_image_input.handle_request(request, predict) assert response.status_code == 200 assert "[10, 10, 3]" in str(response.response)
def test_image_input_http_request_post_binary(make_api, img_file): api = make_api(LegacyImageInput(input_names=("image",)), predict) request = mock.MagicMock(spec=flask.Request) request.method = "POST" request.files = {} request.headers = {} request.get_data.return_value = open(str(img_file), 'rb').read() response = api.handle_request(request) assert response.status_code == 200 assert "[10, 10, 3]" in str(response.response)
class ExampleBentoService(bentoml.BentoService): """ Example BentoService class made for testing purpose """ @bentoml.api(input=JsonInput(), mb_max_latency=1000, mb_max_batch_size=2000, batch=True) def predict_with_sklearn(self, jsons): """predict_dataframe expects dataframe as input """ return self.artifacts.sk_model.predict(jsons) @bentoml.api( input=DataframeInput(dtype={"col1": "int"}), mb_max_latency=1000, mb_max_batch_size=2000, batch=True, ) def predict_dataframe(self, df): """predict_dataframe expects dataframe as input """ return self.artifacts.model.predict_dataframe(df) @bentoml.api(DataframeHandler, dtype={"col1": "int"}, batch=True) # deprecated def predict_dataframe_v1(self, df): """predict_dataframe expects dataframe as input """ return self.artifacts.model.predict_dataframe(df) @bentoml.api(input=ImageInput(), batch=True) def predict_image(self, images): return self.artifacts.model.predict_image(images) @bentoml.api(input=FileInput(), batch=True) def predict_file(self, files): return self.artifacts.model.predict_file(files) @bentoml.api(input=LegacyImageInput(input_names=('original', 'compared'))) def predict_legacy_images(self, original, compared): return self.artifacts.model.predict_legacy_images(original, compared) @bentoml.api(input=JsonInput(), batch=True) def predict_json(self, input_datas): return self.artifacts.model.predict_json(input_datas) @bentoml.api(input=JsonInput(), mb_max_latency=10000 * 1000, batch=True) def echo_with_delay(self, input_datas): data = input_datas[0] time.sleep(data['b'] + data['a'] * len(input_datas)) return input_datas
def test_image_input_http_request_malformatted_input_missing_image_file(make_api,): api = make_api(LegacyImageInput(input_names=("image",)), predict) request = mock.MagicMock(spec=flask.Request) request.method = "POST" request.files = {} request.headers = {} request.get_data.return_value = None response = api.handle_request(request) assert response.status_code == 400 assert response.data
def test_image_input_http_request_multipart_form(make_api, img_file): api = make_api(LegacyImageInput(input_names=("image",)), predict) with open(img_file, "rb") as f: img_bytes = f.read() body, content_type = encode_multipart_formdata(dict(image=("test.jpg", img_bytes),)) request = mock.MagicMock(spec=flask.Request) request.method = "POST" request.headers = {"Content-Type": content_type} request.get_data.return_value = body response = api.handle_request(request) assert response.status_code == 200 assert "[10, 10, 3]" in str(response.response)
def test_image_input_aws_lambda_event(make_api, img_file): api = make_api(LegacyImageInput(input_names=("image",)), predict) with open(str(img_file), "rb") as image_file: content = image_file.read() try: image_bytes_encoded = base64.encodebytes(content) except AttributeError: image_bytes_encoded = base64.encodebytes(img_file) aws_lambda_event = { "body": image_bytes_encoded, "headers": {"Content-Type": "images/jpeg"}, } aws_result = api.handle_aws_lambda_event(aws_lambda_event) assert aws_result["statusCode"] == 200 assert aws_result["body"] == "[10, 10, 3]"
class ExampleBentoService(bentoml.BentoService): """ Example BentoService class made for testing purpose """ @bentoml.api(input=DataframeInput(), mb_max_latency=1000, mb_max_batch_size=2000, batch=True) def predict(self, df): """An API for testing simple bento model service """ return self.artifacts.model.predict(df) @bentoml.api(input=DataframeInput(dtype={"col1": "int"}), batch=True) def predict_dataframe(self, df): """predict_dataframe expects dataframe as input """ return self.artifacts.model.predict_dataframe(df) @bentoml.api(DataframeHandler, dtype={"col1": "int"}, batch=True) # deprecated def predict_dataframe_v1(self, df): """predict_dataframe expects dataframe as input """ return self.artifacts.model.predict_dataframe(df) @bentoml.api(input=ImageInput(), batch=True) def predict_image(self, images): return self.artifacts.model.predict_image(images) @bentoml.api(input=LegacyImageInput(input_names=('original', 'compared')), batch=False) def predict_legacy_images(self, original, compared): return self.artifacts.model.predict_legacy_images(original, compared) @bentoml.api(input=JsonInput(), batch=True) def predict_json(self, input_data): return self.artifacts.model.predict_json(input_data) @bentoml.api(input=LegacyJsonInput(), batch=False) def predict_legacy_json(self, input_data): return self.artifacts.model.predict_legacy_json(input_data)
def test_image_input_cli(capsys, make_api, img_file): api = make_api(LegacyImageInput(input_names=("image",)), predict) test_args = ["--input-file-image", img_file] api.handle_cli(test_args) out, _ = capsys.readouterr() assert out.strip().endswith("[10, 10, 3]")