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
8MODEL_NAME = "turing-motors/heron-chat-blip-ja-stablelm-base-7b-v1"
9
10model = VideoBlipForConditionalGeneration.from_pretrained(
11 MODEL_NAME, torch_dtype=torch.float16, ignore_mismatched_sizes=True
12)
13
14model = model.half()
15model.eval()
16model.to(device)
17
18# prepare a processor
19processor = VideoBlipProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
20tokenizer = LlamaTokenizer.from_pretrained("novelai/nerdstash-tokenizer-v1", additional_special_tokens=['▁▁'])
21processor.tokenizer = tokenizer
22
23import requests
24from PIL import Image
25
26# prepare inputs
27url = "https://www.barnorama.com/wp-content/uploads/2016/12/03-Confusing-Pictures.jpg"
28image = Image.open(requests.get(url, stream=True).raw)
29
30text = f"##human: この画像の面白い点は何ですか?\n##gpt: "
31
32# do preprocessing
33inputs = processor(
34 text=text,
35 images=image,
36 return_tensors="pt",
37 truncation=True,
38)
39
40inputs = {k: v.to(device) for k, v in inputs.items()}
41inputs["pixel_values"] = inputs["pixel_values"].to(device, torch.float16)
42
43# set eos token
44eos_token_id_list = [
45 processor.tokenizer.pad_token_id,
46 processor.tokenizer.eos_token_id,
47 int(tokenizer.convert_tokens_to_ids("##"))
48]
49
50# do inference
51with torch.no_grad():
52 out = model.generate(**inputs, max_length=256, do_sample=False, temperature=0., eos_token_id=eos_token_id_list, no_repeat_ngram_size=2)
53
54# print result
55print(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}