2023-09-09 02:02:44 -07:00
|
|
|
import json
|
2023-10-31 03:02:04 -07:00
|
|
|
from typing import Any, Iterator
|
2023-06-27 16:21:33 -07:00
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import pytest
|
|
|
|
from fastapi.testclient import TestClient
|
2024-01-12 22:00:09 -07:00
|
|
|
from numpy.typing import NDArray
|
2023-06-27 16:21:33 -07:00
|
|
|
from PIL import Image
|
|
|
|
|
2023-12-14 12:51:24 -07:00
|
|
|
from .main import app
|
2023-06-27 16:21:33 -07:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def pil_image() -> Image.Image:
|
|
|
|
return Image.new("RGB", (600, 800))
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-01-12 22:00:09 -07:00
|
|
|
def cv_image(pil_image: Image.Image) -> NDArray[np.float32]:
|
2023-06-27 16:21:33 -07:00
|
|
|
return np.asarray(pil_image)[:, :, ::-1] # PIL uses RGB while cv2 uses BGR
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_get_model() -> Iterator[mock.Mock]:
|
2023-10-31 03:02:04 -07:00
|
|
|
with mock.patch("app.models.cache.from_model_type", autospec=True) as mocked:
|
2023-06-27 16:21:33 -07:00
|
|
|
yield mocked
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
2023-12-14 12:51:24 -07:00
|
|
|
def deployed_app() -> Iterator[TestClient]:
|
|
|
|
with TestClient(app) as client:
|
|
|
|
yield client
|
2023-09-09 02:02:44 -07:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def responses() -> dict[str, Any]:
|
2023-11-13 09:18:46 -07:00
|
|
|
responses: dict[str, Any] = json.load(open("responses.json", "r"))
|
|
|
|
return responses
|
2023-10-31 03:02:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def clip_model_cfg() -> dict[str, Any]:
|
|
|
|
return {
|
|
|
|
"embed_dim": 512,
|
|
|
|
"vision_cfg": {"image_size": 224, "layers": 12, "width": 768, "patch_size": 32},
|
|
|
|
"text_cfg": {"context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def clip_preprocess_cfg() -> dict[str, Any]:
|
|
|
|
return {
|
|
|
|
"size": [224, 224],
|
|
|
|
"mode": "RGB",
|
|
|
|
"mean": [0.48145466, 0.4578275, 0.40821073],
|
|
|
|
"std": [0.26862954, 0.26130258, 0.27577711],
|
|
|
|
"interpolation": "bicubic",
|
|
|
|
"resize_mode": "shortest",
|
|
|
|
"fill_color": 0,
|
|
|
|
}
|
2023-12-20 18:47:56 -07:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def clip_tokenizer_cfg() -> dict[str, Any]:
|
|
|
|
return {
|
|
|
|
"add_prefix_space": False,
|
|
|
|
"added_tokens_decoder": {
|
|
|
|
"49406": {
|
|
|
|
"content": "<|startoftext|>",
|
|
|
|
"lstrip": False,
|
|
|
|
"normalized": True,
|
|
|
|
"rstrip": False,
|
|
|
|
"single_word": False,
|
|
|
|
"special": True,
|
|
|
|
},
|
|
|
|
"49407": {
|
|
|
|
"content": "<|endoftext|>",
|
|
|
|
"lstrip": False,
|
|
|
|
"normalized": True,
|
|
|
|
"rstrip": False,
|
|
|
|
"single_word": False,
|
|
|
|
"special": True,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"bos_token": "<|startoftext|>",
|
|
|
|
"clean_up_tokenization_spaces": True,
|
|
|
|
"do_lower_case": True,
|
|
|
|
"eos_token": "<|endoftext|>",
|
|
|
|
"errors": "replace",
|
|
|
|
"model_max_length": 77,
|
|
|
|
"pad_token": "<|endoftext|>",
|
|
|
|
"tokenizer_class": "CLIPTokenizer",
|
|
|
|
"unk_token": "<|endoftext|>",
|
|
|
|
}
|
2024-01-21 16:22:39 -07:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def providers(request: pytest.FixtureRequest) -> Iterator[dict[str, Any]]:
|
|
|
|
marker = request.node.get_closest_marker("providers")
|
|
|
|
if marker is None:
|
|
|
|
raise ValueError("Missing marker 'providers'")
|
|
|
|
|
|
|
|
providers = marker.args[0]
|
|
|
|
with mock.patch("app.models.base.ort.get_available_providers") as mocked:
|
|
|
|
mocked.return_value = providers
|
|
|
|
yield providers
|