File size: 2,352 Bytes
4807d4b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | """The HTTP server on a tiny offline model."""
import pytest
from fastapi.testclient import TestClient
import discerna
from discerna.contract import DecisionRequest, DecisionResponse, check_response
from discerna.contract.images import to_data_url
from discerna.server import create_app
from probes import TEXT_PROBES, circles
from tiny import tiny_folder
@pytest.fixture(scope="module")
def text_client(tmp_path_factory):
model = discerna.load(tiny_folder(tmp_path_factory.mktemp("text")), device="cpu")
return TestClient(create_app(model)), model
def test_health_and_model_list(text_client):
client, _ = text_client
assert client.get("/healthz").json() == {"ok": True}
data = client.get("/v1/models").json()["data"][0]
assert data["id"] == "tiny" and data["modalities"] == ["text"]
def test_decide_matches_the_engine(text_client):
client, model = text_client
body = TEXT_PROBES["ticket"]
reply = client.post("/v1/decide", json=body)
assert reply.status_code == 200
response = DecisionResponse.model_validate(reply.json())
request = DecisionRequest.model_validate(body)
assert check_response(request, response) == []
assert response.answers == model.decide(request).answers
def test_invalid_body_is_422(text_client):
client, _ = text_client
assert client.post("/v1/decide", json={"state": "x", "questions": {}}).status_code == 422
def test_images_to_a_text_only_model_are_413(text_client):
client, _ = text_client
body = {"state": None, "images": [to_data_url(circles(2))],
"questions": {"n": {"type": "noul", "instructions": "Two circles?"}}}
reply = client.post("/v1/decide", json=body)
assert reply.status_code == 413 and reply.json()["detail"]["error"] == "unsupported"
def test_images_are_answered_by_a_vision_model(tmp_path):
client = TestClient(create_app(discerna.load(tiny_folder(tmp_path / "v", vision=True), device="cpu")))
body = {"state": None, "images": [to_data_url(circles(3))],
"questions": {"count": {"type": "choice", "instructions": "How many circles?",
"criteria": {"two": None, "three": None}}}}
reply = client.post("/v1/decide", json=body)
assert reply.status_code == 200
assert set(reply.json()["answers"]["count"]["probabilities"]) == {"two", "three"}
|