File size: 1,460 Bytes
e3cb0cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import inference.apply_vllm_monet # the patch must be applied before importing vllm
import PIL.Image
from inference.load_and_gen_vllm import *
import os
import PIL
import re
model_path = 'Path/to/your/model'
def replace_abs_vis_token_content(s: str) -> str:
    pattern = re.compile(r'(<abs_vis_token>)(.*?)(</abs_vis_token>)', flags=re.DOTALL)
    return pattern.sub(r'\1<latent>\3', s)


def main():

    mllm, sampling_params = vllm_mllm_init(model_path, tp=1, gpu_memory_utilization=0.8)
    processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)

    conversations = [
        [
            {
                "role": "user",
                "content": [
                    {"type": "text", "text": "Question:  Which car has the longest rental period? The choices are listed below:\n(A)DB11 COUPE.\n(B) V12 VANTAGES COUPES.\n(C) VANQUISH VOLANTE.\n(D) V12 VOLANTE.\n(E) The image does not feature the time. Put your final answer in \\boxed{}."},
                    {"type": "image", "image": PIL.Image.open('images/example_question.png').convert("RGB")}
                ]
            }
        ]
    ]

    inputs = vllm_mllm_process_batch_from_messages(conversations, processor)
    output = vllm_generate(inputs, sampling_params, mllm)
    raw_output_text = output[0].outputs[0].text
    cleaned_output_text = replace_abs_vis_token_content(raw_output_text)
    print(cleaned_output_text)



if __name__ == '__main__':
    main()