microsoft/kosmos-2.5-chat
26220
1import re2import torch3import requests4from PIL import Image, ImageDraw5from transformers import AutoProcessor, Kosmos2_5ForConditionalGeneration6 7repo = "microsoft/kosmos-2.5-chat"8device = "cuda:0"9dtype = torch.bfloat1610 11model = Kosmos2_5ForConditionalGeneration.from_pretrained(repo,12 device_map=device,13 torch_dtype=dtype,14 attn_implementation="flash_attention_2")15processor = AutoProcessor.from_pretrained(repo)16 17# sample image18url = "https://huggingface.co/microsoft/kosmos-2.5/resolve/main/receipt_00008.png"19 20image = Image.open(requests.get(url, stream=True).raw)21 22question = "What is the sub total of the receipt?"23template = "<md>A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. USER: {} ASSISTANT:"24prompt = template.format(question)25inputs = processor(text=prompt, images=image, return_tensors="pt")26 27height, width = inputs.pop("height"), inputs.pop("width")28raw_width, raw_height = image.size29scale_height = raw_height / height30scale_width = raw_width / width31 32inputs = {k: v.to(device) if v is not None else None for k, v in inputs.items()}33inputs["flattened_patches"] = inputs["flattened_patches"].to(dtype)34generated_ids = model.generate(35 **inputs,36 max_new_tokens=1024,37)38 39generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)40print(generated_text[0])41 