Buckets:
| import torch | |
| import transformers | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| from PIL import Image | |
| import warnings | |
| import attack_model | |
| # disable some warnings | |
| transformers.logging.set_verbosity_error() | |
| transformers.logging.disable_progress_bar() | |
| warnings.filterwarnings('ignore') | |
| class Bunny(attack_model.attack_model): | |
| def __init__(self,device): | |
| self.device = device # or cpu | |
| torch.set_default_device(self.device) | |
| self.model = AutoModelForCausalLM.from_pretrained( | |
| 'BAAI/Bunny-v1_0-3B', | |
| torch_dtype=torch.float16, | |
| device_map='auto', | |
| trust_remote_code=True) | |
| self.tokenizer = AutoTokenizer.from_pretrained( | |
| 'BAAI/Bunny-v1_0-3B', | |
| trust_remote_code=True) | |
| def inference(self, image_path, prompt): | |
| text = (f"A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, " | |
| f"detailed, and polite answers to the user's questions. USER: <image>\n{prompt} ASSISTANT:") | |
| text_chunks = [self.tokenizer(chunk).input_ids for chunk in text.split('<image>')] | |
| input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0) | |
| # image, sample images can be found in https://huggingface.co/BAAI/Bunny-v1_0-3B/tree/main/images | |
| image = Image.open(image_path) | |
| image_tensor = self.model.process_images([image], self.model.config).to(dtype=self.model.dtype) | |
| # generate | |
| output_ids = self.model.generate( | |
| input_ids, | |
| images=image_tensor, | |
| max_new_tokens=100, | |
| use_cache=True)[0] | |
| response = self.tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip() | |
| return response | |
Xet Storage Details
- Size:
- 1.82 kB
- Xet hash:
- 58beb9eebb334dd23940b15be712744cb742cd6892254e15ebf0806930bc65f9
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.