from PIL import Image import torch from transformers import AutoModelForCausalLM, AutoProcessor model_path = "moonshotai/Kimi-VL-A3B-Instruct" model = AutoModelForCausalLM.from_pretrained( model_path, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True, ) processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True) image_path = "/XXX/XXX.jpg" image = Image.open(image_path) messages = [ {"role": "user", "content": [{"type": "image", "image": image_path}, {"type": "text", "text": "Ocr this image."}]} ] text = processor.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt") inputs = processor(images=image, text=text, return_tensors="pt", padding=True, truncation=True).to(model.device) generated_ids = model.generate(**inputs, max_new_tokens=1024) 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)