Skip to content

Commit

Permalink
use httpx for tests
Browse files Browse the repository at this point in the history
Co-Authored-By: Judah Rand <[email protected]>
  • Loading branch information
sauyon and judahrand committed Oct 10, 2023
1 parent d645d67 commit 567dc11
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 99 deletions.
56 changes: 26 additions & 30 deletions tests/integration/frameworks/models/detectron.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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]
47 changes: 22 additions & 25 deletions tests/integration/frameworks/models/easyocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import typing as t

import easyocr
import httpx
import numpy as np
import requests
from PIL import Image

import bentoml
Expand All @@ -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]
77 changes: 38 additions & 39 deletions tests/integration/frameworks/models/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/_internal/utils/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.httx.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(
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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])
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 567dc11

Please sign in to comment.