Views
No views yet

pipeline, see the below example:1from transformers import pipeline
2
3pipe = pipeline("image-text-to-text", model="llava-hf/llava-next-110b-hf")
4messages = [
5 {
6 "role": "user",
7 "content": [
8 {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"},
9 {"type": "text", "text": "What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"},
10 ],
11 },
12]
13
14out = pipe(text=messages, max_new_tokens=20)
15print(out)
16>>> [{'input_text': [{'role': 'user', 'content': [{'type': 'image', 'url': 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'}, {'type': 'text', 'text': 'What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud'}]}], 'generated_text': 'Lava'}]1from transformers import LlavaNextProcessor, LlavaNextForConditionalGeneration
2import torch
3from PIL import Image
4import requests
5
6processor = LlavaNextProcessor.from_pretrained("llava-hf/llava-next-110b-hf")
7model = LlavaNextForConditionalGeneration.from_pretrained("llava-hf/llava-next-110b-hf", torch_dtype=torch.float16, device_map="auto")
8
9# prepare image and text prompt, using the appropriate prompt template
10url = "https://github.com/haotian-liu/LLaVA/blob/1a91fc274d7c35a9b50b3cb29c4247ae5837ce39/images/llava_v1_5_radar.jpg?raw=true"
11image = Image.open(requests.get(url, stream=True).raw)
12
13# Define a chat histiry and use `apply_chat_template` to get correctly formatted prompt
14# Each value in "content" has to be a list of dicts with types ("text", "image")
15conversation = [
16 {
17
18 "role": "user",
19 "content": [
20 {"type": "text", "text": "What is shown in this image?"},
21 {"type": "image"},
22 ],
23 },
24]
25prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
26
27inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device)
28
29# autoregressively complete prompt
30output = model.generate(**inputs, max_new_tokens=100)
31
32print(processor.decode(output[0], skip_special_tokens=True))torch.Tensor which you can pass directly to model.generate()1messages = [
2 {
3 "role": "user",
4 "content": [
5 {"type": "image", "url": "https://www.ilankelman.org/stopsigns/australia.jpg"}
6 {"type": "text", "text": "What is shown in this image?"},
7 ],
8 },
9]
10
11inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors"pt")
12output = model.generate(**inputs, max_new_tokens=50)bitsandbytes librarybitsandbytes, pip install bitsandbytes and make sure to have access to a CUDA compatible GPU device. Simply change the snippet above with:1model = LlavaNextForConditionalGeneration.from_pretrained(
2 model_id,
3 torch_dtype=torch.float16,
4 low_cpu_mem_usage=True,
5+ load_in_4bit=True
6)flash-attn. Refer to the original repository of Flash Attention regarding that package installation. Simply change the snippet above with:1model = LlavaNextForConditionalGeneration.from_pretrained(
2 model_id,
3 torch_dtype=torch.float16,
4 low_cpu_mem_usage=True,
5+ use_flash_attention_2=True
6).to(0)1@misc{li2024llavanext-strong,
2 title={LLaVA-NeXT: Stronger LLMs Supercharge Multimodal Capabilities in the Wild},
3 url={https://llava-vl.github.io/blog/2024-05-10-llava-next-stronger-llms/},
4 author={Li, Bo and Zhang, Kaichen and Zhang, Hao and Guo, Dong and Zhang, Renrui and Li, Feng and Zhang, Yuanhan and Liu, Ziwei and Li, Chunyuan},
5 month={May},
6 year={2024}
7}