test_idefics.py 1.57 KB
Newer Older
1
import pytest
2
import base64
3
4
5
6


@pytest.fixture(scope="module")
def idefics_handle(launcher):
OlivierDehaene's avatar
OlivierDehaene committed
7
8
9
    with launcher(
        "HuggingFaceM4/idefics-9b-instruct", num_shard=2, dtype="float16"
    ) as handle:
10
11
12
13
14
15
16
17
18
        yield handle


@pytest.fixture(scope="module")
async def idefics(idefics_handle):
    await idefics_handle.health(300)
    return idefics_handle.client


19
20
21
22
# TODO fix the server parsser to count inline image tokens correctly
def get_chicken():
    with open("integration-tests/images/chicken_on_money.png", "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read())
23
    return f"data:image/png;base64,{encoded_string.decode('utf-8')}"
24
25


26
27
@pytest.mark.asyncio
async def test_idefics(idefics, response_snapshot):
28
    chicken = get_chicken()
29
    response = await idefics.generate(
30
        f"User:![]({chicken})Can you tell me a very short story based on the image?",
31
32
33
34
35
36
37
38
39
40
        max_new_tokens=10,
        decoder_input_details=True,
    )

    assert response.details.generated_tokens == 10
    assert response == response_snapshot


@pytest.mark.asyncio
async def test_idefics_load(idefics, generate_load, response_snapshot):
41
    chicken = get_chicken()
42
43
    responses = await generate_load(
        idefics,
44
        f"User:![]({chicken})Can you tell me a very short story based on the image?",
45
46
47
48
49
50
51
52
53
54
55
56
        max_new_tokens=10,
        n=4,
    )

    generated_texts = [r.generated_text for r in responses]

    assert len(generated_texts) == 4
    assert generated_texts, all(
        [text == generated_texts[0] for text in generated_texts]
    )

    assert responses == response_snapshot