Kimi_VL_think.py 1.56 KB
Newer Older
wanglch's avatar
wanglch committed
1
2
3
4
from PIL import Image
import torch
from transformers import AutoModelForCausalLM, AutoProcessor

zzg_666's avatar
zhaozg  
zzg_666 committed
5
model_path = "../moonshotai/Kimi-VL-A3B-Thinking"
wanglch's avatar
wanglch committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
model = AutoModelForCausalLM.from_pretrained(
    model_path,
    torch_dtype=torch.bfloat16,
    device_map="auto",
    trust_remote_code=True,
)
# If flash-attn has been installed, it is recommended to set torch_dtype=torch.bfloat16 and attn_implementation="flash_attention_2"
# to save memory and speed up inference
# model = AutoModelForCausalLM.from_pretrained(
#     model_path,
#     torch_dtype=torch.bfloat16,
#     device_map="auto",
#     trust_remote_code=True,
#     attn_implementation="flash_attention_2"
# )
processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)

zzg_666's avatar
zhaozg  
zzg_666 committed
23
image_paths = ["./Pic/arch.png", "./Pic/theory.png"]
wanglch's avatar
wanglch committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
images = [Image.open(path) for path in image_paths]
messages = [
    {
        "role": "user",
        "content": [
            {"type": "image", "image": image_path} for image_path in image_paths
        ] + [{"type": "text", "text": "Ocr this image"}],
    },
]
text = processor.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
inputs = processor(images=images, text=text, return_tensors="pt", padding=True, truncation=True).to(model.device)
generated_ids = model.generate(**inputs, max_new_tokens=2048)
generated_ids_trimmed = [
    out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
response = processor.batch_decode(
    generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
print(response)