diff --git a/tests/integration/frameworks/models/detectron.py b/tests/integration/frameworks/models/detectron.py index 5c6b5faf1b0..9c87073f767 100644 --- a/tests/integration/frameworks/models/detectron.py +++ b/tests/integration/frameworks/models/detectron.py @@ -6,8 +6,8 @@ import detectron2.engine as E import detectron2.model_zoo as Mz import detectron2.modeling as M +import httpx import numpy as np -import requests import torch from detectron2.data import transforms as T from PIL import Image @@ -32,9 +32,8 @@ def prepare_input() -> torch.Tensor: aug = T.ResizeShortestEdge([800, 800], 1333) - im: NDArray[np.float32] = np.asarray( - Image.open(requests.get(url, stream=True).raw).convert("RGB") - ) + with httpx.stream("GET", url) as resp: + im: NDArray[np.float32] = np.asarray(Image.open(resp).convert("RGB")) arr: NDArray[np.float32] = aug.get_transform(im).apply_image(im) # NOTE: it is fine to copy here return torch.as_tensor(arr.transpose(2, 0, 1)) @@ -77,32 +76,29 @@ def check_expected(output: list[dict[str, t.Any]]) -> bool: ], ) -predictor = Model( - name="predictor-masked-rcnn", - model=E.DefaultPredictor(cfg), - configurations=[ - Config( - test_inputs={ - "__call__": [ - Input( - input_args=[ - np.asarray( - Image.open(requests.get(url, stream=True).raw).convert( - "RGB" +with httpx.stream("GET", url) as resp: + predictor = Model( + name="predictor-masked-rcnn", + model=E.DefaultPredictor(cfg), + configurations=[ + Config( + test_inputs={ + "__call__": [ + Input( + input_args=[ + np.asarray(Image.open(resp).convert("RGB")), + ], + expected=lambda output: all( + map( + lambda o: o > 0.5, + output["instances"].get("scores").tolist(), ) - ) - ], - expected=lambda output: all( - map( - lambda o: o > 0.5, - output["instances"].get("scores").tolist(), - ) - ), - ) - ] - } - ) - ], -) + ), + ) + ] + } + ) + ], + ) models = [rcnn, predictor] diff --git a/tests/integration/frameworks/models/easyocr.py b/tests/integration/frameworks/models/easyocr.py index c1cafcb05f7..571fe00a74c 100644 --- a/tests/integration/frameworks/models/easyocr.py +++ b/tests/integration/frameworks/models/easyocr.py @@ -3,8 +3,8 @@ import typing as t import easyocr +import httpx import numpy as np -import requests from PIL import Image import bentoml @@ -26,29 +26,26 @@ def check_output(output: list[tuple[list[t.Any], str, float]]): ) -en_reader = FrameworkTestModel( - name="en_reader", - model=easyocr.Reader(["en"]), - configurations=[ - Config( - test_inputs={ - "readtext": [ - Input( - input_args=( - [ - np.asarray( - Image.open( - requests.get(url, stream=True).raw - ).convert("RGB") - ) - ] - ), - expected=check_output, - ) - ] - } - ) - ], -) +with httpx.stream("GET", url) as resp: + en_reader = FrameworkTestModel( + name="en_reader", + model=easyocr.Reader(["en"]), + configurations=[ + Config( + test_inputs={ + "readtext": [ + Input( + input_args=( + [ + np.asarray(Image.open(resp).convert("RGB")), + ] + ), + expected=check_output, + ) + ] + } + ) + ], + ) models = [en_reader] diff --git a/tests/integration/frameworks/models/transformers.py b/tests/integration/frameworks/models/transformers.py index b521c97c088..a8ca9dba2b5 100644 --- a/tests/integration/frameworks/models/transformers.py +++ b/tests/integration/frameworks/models/transformers.py @@ -3,8 +3,8 @@ import importlib import typing as t +import httpx import numpy as np -import requests import tensorflow as tf import transformers from datasets import load_dataset @@ -162,44 +162,43 @@ def check_output(out: list[AnyDict | AnyList]) -> bool: tiny_image_task = "image-to-text" test_url = "http://images.cocodataset.org/val2017/000000039769.jpg" -image_classification: list[Model] = [ - Model( - name="image-to-text-pipeline", - model=model, - configurations=[ - Config( - load_kwargs={"task": tiny_image_task}, - test_inputs={ - "__call__": [ - Input( - input_args=[[test_url]], - expected=[ - [ - { - "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO" - }, - ] - ], - ), - Input( - input_args=[ - [Image.open(requests.get(test_url, stream=True).raw)] - ], - expected=[ - [ - { - "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO" - }, - ] - ], - ), - ], - }, - ), - ], - ) - for model in gen_task_pipeline(model=tiny_image_model, task=tiny_image_task) -] +with httpx.stream(test_url) as resp: + image_classification: list[Model] = [ + Model( + name="image-to-text-pipeline", + model=model, + configurations=[ + Config( + load_kwargs={"task": tiny_image_task}, + test_inputs={ + "__call__": [ + Input( + input_args=[[test_url]], + expected=[ + [ + { + "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO" + }, + ] + ], + ), + Input( + input_args=[[Image.open(resp)]], + expected=[ + [ + { + "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO" + }, + ] + ], + ), + ], + }, + ), + ], + ) + for model in gen_task_pipeline(model=tiny_image_model, task=tiny_image_task) + ] def gen_custom_pipeline_kwargs( diff --git a/tests/unit/_internal/utils/test_analytics.py b/tests/unit/_internal/utils/test_analytics.py index 01fc2718457..523737d99b1 100644 --- a/tests/unit/_internal/utils/test_analytics.py +++ b/tests/unit/_internal/utils/test_analytics.py @@ -67,7 +67,7 @@ def test_get_payload(event_properties: analytics.schemas.ModelSaveEvent): assert SCHEMA.validate(payload) -@patch("bentoml._internal.utils.analytics.usage_stats.requests.post") +@patch("bentoml._internal.utils.analytics.usage_stats.httpx.post") @patch("bentoml._internal.utils.analytics.usage_stats.do_not_track") @patch("bentoml._internal.utils.analytics.usage_stats._usage_event_debugging") def test_send_usage( @@ -91,7 +91,7 @@ def test_send_usage( assert "Tracking Payload" in caplog.text -@patch("bentoml._internal.utils.analytics.usage_stats.requests.post") +@patch("bentoml._internal.utils.analytics.usage_stats.httpx.post") @patch("bentoml._internal.utils.analytics.usage_stats.do_not_track") def test_do_not_track( mock_do_not_track: MagicMock, @@ -106,7 +106,7 @@ def test_do_not_track( @patch("bentoml._internal.utils.analytics.usage_stats.logger") -@patch("bentoml._internal.utils.analytics.usage_stats.requests.post") +@patch("bentoml._internal.utils.analytics.usage_stats.httpx.post") @patch("bentoml._internal.utils.analytics.usage_stats.do_not_track") def test_send_usage_failure( mock_do_not_track: MagicMock, @@ -123,7 +123,7 @@ def test_send_usage_failure( mock_logger.debug.assert_called_with("Tracking Error: %s", mock_post.side_effect) -@patch("bentoml._internal.utils.analytics.usage_stats.requests.post") +@patch("bentoml._internal.utils.analytics.usage_stats.httpx.post") @patch("bentoml._internal.utils.analytics.usage_stats.do_not_track") @patch("bentoml._internal.utils.analytics.usage_stats._usage_event_debugging") @pytest.mark.parametrize("production", [False, True]) @@ -324,7 +324,7 @@ def test_get_metrics_report( @patch("bentoml._internal.utils.analytics.usage_stats.do_not_track") -@patch("bentoml._internal.utils.analytics.usage_stats.requests.post") +@patch("bentoml._internal.utils.analytics.usage_stats.httpx.post") @patch("bentoml._internal.utils.analytics.usage_stats._track_serve_init") @patch("bentoml._internal.utils.analytics.usage_stats._usage_event_debugging") @patch("bentoml._internal.server.metrics.prometheus.PrometheusClient")