Views
No views yet

1import torch
2from heron.models.video_blip import VideoBlipForConditionalGeneration, VideoBlipProcessor
3from transformers import LlamaTokenizer
4
5device_id = 0
6device = f"cuda:{device_id}"
7
8max_length = 512
9MODEL_NAME = "turing-motors/heron-chat-blip-ja-stablelm-base-7b-v0"
10
11model = VideoBlipForConditionalGeneration.from_pretrained(
12 MODEL_NAME, torch_dtype=torch.float16, ignore_mismatched_sizes=True
13)
14
15model = model.half()
16model.eval()
17model.to(device)
18
19# prepare a processor
20processor = VideoBlipProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
21tokenizer = LlamaTokenizer.from_pretrained("novelai/nerdstash-tokenizer-v1", additional_special_tokens=['▁▁'])
22processor.tokenizer = tokenizer
23
24import requests
25from PIL import Image
26
27# prepare inputs
28url = "https://www.barnorama.com/wp-content/uploads/2016/12/03-Confusing-Pictures.jpg"
29image = Image.open(requests.get(url, stream=True).raw)
30
31text = f"##human: この画像の面白い点は何ですか?\n##gpt: "
32
33# do preprocessing
34inputs = processor(
35 text=text,
36 images=image,
37 return_tensors="pt",
38 truncation=True,
39)
40
41inputs = {k: v.to(device) for k, v in inputs.items()}
42inputs["pixel_values"] = inputs["pixel_values"].to(device, torch.float16)
43
44# set eos token
45eos_token_id_list = [
46 processor.tokenizer.pad_token_id,
47 processor.tokenizer.eos_token_id,
48 int(tokenizer.convert_tokens_to_ids("##"))
49]
50
51# do inference
52with torch.no_grad():
53 out = model.generate(**inputs, max_length=256, do_sample=False, temperature=0., eos_token_id=eos_token_id_list, no_repeat_ngram_size=2)
54
55# print result
56print(processor.tokenizer.batch_decode(out))1@misc{BlipJapaneseStableLM,
2 url = {[https://huggingface.co/turing-motors/heron-chat-blip-ja-stablelm-base-7b-v0](https://huggingface.co/turing-motors/heron-chat-blip-ja-stablelm-base-7b-v0)},
3 title = {Heron BLIP Japanese StableLM Base 7B},
4 author = {Kotaro Tanahashi, Yuichi Inoue, and Yu Yamaguchi}
5}1@misc{JapaneseInstructBLIPAlpha,
2 url = {[https://huggingface.co/stabilityai/japanese-instructblip-alpha](https://huggingface.co/stabilityai/japanese-instructblip-alpha)},
3 title = {Japanese InstructBLIP Alpha},
4 author = {Shing, Makoto and Akiba, Takuya}
5}