florence2_inference.py 1.46 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
"""
3
4
Demonstrate prompting of text-to-text
encoder/decoder models, specifically Florence-2
5
"""
6
# TODO(Isotr0py):
7
# Move to offline_inference/vision_language.py
8
# after porting vision backbone
9
from vllm import LLM, SamplingParams
10
from vllm.assets.image import ImageAsset
11
12
13

# Create a Florence-2 encoder/decoder model instance
llm = LLM(
14
15
16
    model="microsoft/Florence-2-large",
    tokenizer="facebook/bart-large",
    max_num_seqs=8,
17
18
19
20
    trust_remote_code=True,
)

prompts = [
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    {   # implicit prompt with task token
        "prompt": "<DETAILED_CAPTION>",
        "multi_modal_data": {
            "image": ImageAsset("stop_sign").pil_image
        },
    },
    {   # explicit encoder/decoder prompt
        "encoder_prompt": {
            "prompt": "Describe in detail what is shown in the image.",
            "multi_modal_data": {
                "image": ImageAsset("cherry_blossom").pil_image
            },
        },
        "decoder_prompt": "",
    },
36
37
38
39
40
41
]
# Create a sampling params object.
sampling_params = SamplingParams(
    temperature=0,
    top_p=1.0,
    min_tokens=0,
42
    max_tokens=128,
43
44
45
46
47
48
49
50
51
52
)

# Generate output tokens from the prompts. The output is a list of
# RequestOutput objects that contain the prompt, generated
# text, and other information.
outputs = llm.generate(prompts, sampling_params)

# Print the outputs.
for output in outputs:
    generated_text = output.outputs[0].text
53
    print(f"Generated text: {generated_text!r}")