|
| 1 | +# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import random |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +import paddle |
| 19 | + |
| 20 | +seed = 2024 |
| 21 | +paddle.seed(seed) |
| 22 | +np.random.seed(seed) |
| 23 | +random.seed(seed) |
| 24 | + |
| 25 | +import argparse |
| 26 | + |
| 27 | +from PIL import Image |
| 28 | + |
| 29 | +from paddlemix.auto.modeling import AutoModelMIX |
| 30 | +from paddlemix.auto.tokenizer import AutoTokenizerMIX |
| 31 | + |
| 32 | +parser = argparse.ArgumentParser() |
| 33 | + |
| 34 | +parser.add_argument("--from_pretrained", type=str, default="THUDM/cogagent-chat", help="pretrained ckpt and tokenizer") |
| 35 | +args = parser.parse_args() |
| 36 | +MODEL_PATH = args.from_pretrained |
| 37 | +TOKENIZER_PATH = MODEL_PATH |
| 38 | + |
| 39 | +tokenizer = AutoTokenizerMIX.from_pretrained(TOKENIZER_PATH) |
| 40 | + |
| 41 | +data_type = "float32" |
| 42 | + |
| 43 | +model = AutoModelMIX.from_pretrained( |
| 44 | + MODEL_PATH, |
| 45 | + dtype=data_type, |
| 46 | + low_cpu_mem_usage=False, |
| 47 | +) |
| 48 | +model.eval() |
| 49 | + |
| 50 | +text_only_template = "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:" |
| 51 | +while True: |
| 52 | + image_path = input("image path >>>>> ") |
| 53 | + if image_path == "": |
| 54 | + print("You did not enter image path, the following will be a plain text conversation.") |
| 55 | + image = None |
| 56 | + text_only_first_query = True |
| 57 | + else: |
| 58 | + image = Image.open(image_path).convert("RGB") |
| 59 | + history = [] |
| 60 | + while True: |
| 61 | + query = input("Human:") |
| 62 | + if query == "clear": |
| 63 | + break |
| 64 | + if image is None: |
| 65 | + if text_only_first_query: |
| 66 | + query = text_only_template.format(query) |
| 67 | + text_only_first_query = False |
| 68 | + else: |
| 69 | + old_prompt = "" |
| 70 | + for _, (old_query, response) in enumerate(history): |
| 71 | + old_prompt += old_query + " " + response + "\n" |
| 72 | + query = old_prompt + "USER: {} ASSISTANT:".format(query) |
| 73 | + if image is None: |
| 74 | + input_by_model = model.build_conversation_input_ids( |
| 75 | + tokenizer, query=query, history=history, template_version="base" |
| 76 | + ) |
| 77 | + else: |
| 78 | + input_by_model = model.build_conversation_input_ids( |
| 79 | + tokenizer, query=query, history=history, images=[image] |
| 80 | + ) |
| 81 | + inputs = { |
| 82 | + "input_ids": input_by_model["input_ids"].unsqueeze(axis=0), |
| 83 | + "token_type_ids": input_by_model["token_type_ids"].unsqueeze(axis=0), |
| 84 | + "attention_mask": input_by_model["attention_mask"].unsqueeze(axis=0), |
| 85 | + "images": [[input_by_model["images"][0].to(data_type)]] if image is not None else None, |
| 86 | + } |
| 87 | + if "cross_images" in input_by_model and input_by_model["cross_images"]: |
| 88 | + inputs["cross_images"] = [[input_by_model["cross_images"][0].to(data_type)]] |
| 89 | + gen_kwargs = {"max_new_tokens": 2048, "do_sample": False} |
| 90 | + with paddle.no_grad(): |
| 91 | + outputs, _ = model.generate(**inputs, **gen_kwargs) |
| 92 | + outputs = outputs[:, inputs["input_ids"].shape[1] :] |
| 93 | + response = tokenizer.decode(outputs[0]) |
| 94 | + response = response.split("</s>")[0] |
| 95 | + print("\nCog:", response) |
| 96 | + history.append((query, response)) |
0 commit comments