Add code snippets, metadata tags (#3)
Browse files- Add code snippets, metadata tags (2428a783737ceece641c973c90ccbcfcdd1c822a)
- Update README.md (609c6f20d6f10b2f7f7f8e967b3d4e13047ae748)
Co-authored-by: Niels Rogge <[email protected]>
    	
        README.md
    CHANGED
    
    | @@ -1,6 +1,8 @@ | |
| 1 | 
             
            ---
         | 
| 2 | 
             
            language: en
         | 
| 3 | 
             
            license: mit
         | 
|  | |
|  | |
| 4 | 
             
            ---
         | 
| 5 | 
             
            # Kosmos-2.5-chat
         | 
| 6 |  | 
| @@ -13,6 +15,53 @@ Kosmos-2.5-chat is a model specifically trained for Visual Question Answering (V | |
| 13 |  | 
| 14 | 
             
            [Kosmos-2.5: A Multimodal Literate Model](https://arxiv.org/abs/2309.11419)
         | 
| 15 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 16 | 
             
            ## NOTE:
         | 
| 17 | 
             
            Since this is a generative model, there is a risk of **hallucination** during the generation process, and it **CAN NOT** guarantee the accuracy of all results in the images.
         | 
| 18 |  | 
|  | |
| 1 | 
             
            ---
         | 
| 2 | 
             
            language: en
         | 
| 3 | 
             
            license: mit
         | 
| 4 | 
            +
            library_name: transformers
         | 
| 5 | 
            +
            pipeline_tag: image-text-to-text
         | 
| 6 | 
             
            ---
         | 
| 7 | 
             
            # Kosmos-2.5-chat
         | 
| 8 |  | 
|  | |
| 15 |  | 
| 16 | 
             
            [Kosmos-2.5: A Multimodal Literate Model](https://arxiv.org/abs/2309.11419)
         | 
| 17 |  | 
| 18 | 
            +
            ## Usage
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            KOSMOS-2.5 is supported from Transformers >= 4.56. Find the docs [here](https://huggingface.co/docs/transformers/main/en/model_doc/kosmos2_5).
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            ```python
         | 
| 23 | 
            +
            import re
         | 
| 24 | 
            +
            import torch
         | 
| 25 | 
            +
            import requests
         | 
| 26 | 
            +
            from PIL import Image, ImageDraw
         | 
| 27 | 
            +
            from transformers import AutoProcessor, Kosmos2_5ForConditionalGeneration
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            repo = "microsoft/kosmos-2.5-chat"
         | 
| 30 | 
            +
            device = "cuda:0"
         | 
| 31 | 
            +
            dtype = torch.bfloat16
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            model = Kosmos2_5ForConditionalGeneration.from_pretrained(repo,
         | 
| 34 | 
            +
                                                                      device_map=device,
         | 
| 35 | 
            +
                                                                      torch_dtype=dtype,
         | 
| 36 | 
            +
                                                                      attn_implementation="flash_attention_2")
         | 
| 37 | 
            +
            processor = AutoProcessor.from_pretrained(repo)
         | 
| 38 | 
            +
             | 
| 39 | 
            +
            # sample image
         | 
| 40 | 
            +
            url = "https://huggingface.co/microsoft/kosmos-2.5/resolve/main/receipt_00008.png"
         | 
| 41 | 
            +
             | 
| 42 | 
            +
            image = Image.open(requests.get(url, stream=True).raw)
         | 
| 43 | 
            +
             | 
| 44 | 
            +
            question = "What is the sub total of the receipt?"
         | 
| 45 | 
            +
            template = "<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:"
         | 
| 46 | 
            +
            prompt = template.format(question)
         | 
| 47 | 
            +
            inputs = processor(text=prompt, images=image, return_tensors="pt")
         | 
| 48 | 
            +
             | 
| 49 | 
            +
            height, width = inputs.pop("height"), inputs.pop("width")
         | 
| 50 | 
            +
            raw_width, raw_height = image.size
         | 
| 51 | 
            +
            scale_height = raw_height / height
         | 
| 52 | 
            +
            scale_width = raw_width / width
         | 
| 53 | 
            +
             | 
| 54 | 
            +
            inputs = {k: v.to(device) if v is not None else None for k, v in inputs.items()}
         | 
| 55 | 
            +
            inputs["flattened_patches"] = inputs["flattened_patches"].to(dtype)
         | 
| 56 | 
            +
            generated_ids = model.generate(
         | 
| 57 | 
            +
                **inputs,
         | 
| 58 | 
            +
                max_new_tokens=1024,
         | 
| 59 | 
            +
            )
         | 
| 60 | 
            +
             | 
| 61 | 
            +
            generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
         | 
| 62 | 
            +
            print(generated_text[0])
         | 
| 63 | 
            +
            ```
         | 
| 64 | 
            +
             | 
| 65 | 
             
            ## NOTE:
         | 
| 66 | 
             
            Since this is a generative model, there is a risk of **hallucination** during the generation process, and it **CAN NOT** guarantee the accuracy of all results in the images.
         | 
| 67 |  | 

