from PIL import Imageimport torchfrom transformers import AutoModel, AutoTokenizer model_path = 'openbmb/MiniCPM-V-4'model = AutoModel.from_pretrained(model_path, trust_remote_code=True, # sdpa or flash_attention_2, no eager attn_implementation='sdpa', torch_dtype=torch.bfloat16)model = model.eval().cuda()tokenizer = AutoTokenizer.from_pretrained( model_path, trust_remote_code=True) image = Image.open('./assets/single.png').convert('RGB') # First round chat question = "What is the landform in the picture?"msgs = [{'role': 'user', 'content': [image, question]}] answer = model.chat( msgs=msgs, image=image, tokenizer=tokenizer)print(answer) # Second round chat, pass history context of multi-turn conversationmsgs.append({"role": "assistant", "content": [answer]})msgs.append({"role": "user", "content": [ "What should I pay attention to when traveling here?"]}) answer = model.chat( msgs=msgs, image=None, tokenizer=tokenizer)print(answer)