|
| 1 | +# |
| 2 | +# Copyright 2016 The BigDL Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | + |
| 18 | +import os |
| 19 | +import time |
| 20 | +import argparse |
| 21 | +import requests |
| 22 | +import torch |
| 23 | +from PIL import Image |
| 24 | +from ipex_llm.transformers import AutoModel |
| 25 | +from transformers import AutoTokenizer |
| 26 | + |
| 27 | + |
| 28 | +if __name__ == '__main__': |
| 29 | + parser = argparse.ArgumentParser(description='Predict Tokens using `chat()` API for MiniCPM-V-2_6 model') |
| 30 | + parser.add_argument('--repo-id-or-model-path', type=str, default="openbmb/MiniCPM-V-2_6", |
| 31 | + help='The huggingface repo id for the MiniCPM-V-2_6 model to be downloaded' |
| 32 | + ', or the path to the huggingface checkpoint folder') |
| 33 | + parser.add_argument('--image-url-or-path', type=str, |
| 34 | + default='http://farm6.staticflickr.com/5268/5602445367_3504763978_z.jpg', |
| 35 | + help='The URL or path to the image to infer') |
| 36 | + parser.add_argument('--prompt', type=str, default="What is in the image?", |
| 37 | + help='Prompt to infer') |
| 38 | + parser.add_argument('--stream', action='store_true', |
| 39 | + help='Whether to chat in streaming mode') |
| 40 | + |
| 41 | + args = parser.parse_args() |
| 42 | + model_path = args.repo_id_or_model_path |
| 43 | + image_path = args.image_url_or_path |
| 44 | + |
| 45 | + # Load model in 4 bit, |
| 46 | + # which convert the relevant layers in the model into INT4 format |
| 47 | + model = AutoModel.from_pretrained(model_path, |
| 48 | + load_in_low_bit="asym_int4", |
| 49 | + optimize_model=True, |
| 50 | + trust_remote_code=True, |
| 51 | + use_cache=True, |
| 52 | + torch_dtype=torch.float32, |
| 53 | + modules_to_not_convert=["vpm", "resampler"]) |
| 54 | + |
| 55 | + # Load tokenizer |
| 56 | + tokenizer = AutoTokenizer.from_pretrained(model_path, |
| 57 | + trust_remote_code=True) |
| 58 | + model.eval() |
| 59 | + |
| 60 | + query = args.prompt |
| 61 | + if os.path.exists(image_path): |
| 62 | + image = Image.open(image_path).convert('RGB') |
| 63 | + else: |
| 64 | + image = Image.open(requests.get(image_path, stream=True).raw).convert('RGB') |
| 65 | + |
| 66 | + # Generate predicted tokens |
| 67 | + # here the prompt tuning refers to https://huggingface.co/openbmb/MiniCPM-V-2_6/blob/main/README.md |
| 68 | + msgs = [{'role': 'user', 'content': args.prompt}] |
| 69 | + |
| 70 | + if args.stream: |
| 71 | + res, context, _ = model.chat( |
| 72 | + image=image, |
| 73 | + msgs=msgs, |
| 74 | + context= None, |
| 75 | + tokenizer=tokenizer, |
| 76 | + stream=True |
| 77 | + ) |
| 78 | + |
| 79 | + print('-'*20, 'Input Image', '-'*20) |
| 80 | + print(image_path) |
| 81 | + print('-'*20, 'Input Prompt', '-'*20) |
| 82 | + print(args.prompt) |
| 83 | + print('-'*20, 'Stream Chat Output', '-'*20) |
| 84 | + for new_text in res: |
| 85 | + print(new_text, flush=True, end='') |
| 86 | + else: |
| 87 | + st = time.time() |
| 88 | + res, context, _ = model.chat( |
| 89 | + image=image, |
| 90 | + msgs=msgs, |
| 91 | + context=None, |
| 92 | + tokenizer=tokenizer, |
| 93 | + ) |
| 94 | + end = time.time() |
| 95 | + |
| 96 | + print(f'Inference time: {end-st} s') |
| 97 | + print('-'*20, 'Input Image', '-'*20) |
| 98 | + print(image_path) |
| 99 | + print('-'*20, 'Input Prompt', '-'*20) |
| 100 | + print(args.prompt) |
| 101 | + print('-'*20, 'Chat Output', '-'*20) |
| 102 | + print(res) |
0 commit comments