Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use httpx for testing #4227

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4,733 changes: 2,352 additions & 2,381 deletions pdm.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Expand Up @@ -34,6 +34,7 @@ dependencies = [
"cloudpickle>=2.0.0",
"deepmerge",
"fs",
"httpx",
"inflection",
"numpy",
# OpenTelemetry is the server dependencies, rather than SDK
Expand Down
56 changes: 26 additions & 30 deletions tests/integration/frameworks/models/detectron.py
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
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
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
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")
aarnphm marked this conversation as resolved.
Show resolved Hide resolved
@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