Commit
•
4ff260d
1
Parent(s):
8ee6481
Add chat template examples
Browse files
README.md
CHANGED
@@ -6,6 +6,10 @@ datasets:
|
|
6 |
pipeline_tag: image-to-text
|
7 |
inference: false
|
8 |
arxiv: 2304.08485
|
|
|
|
|
|
|
|
|
9 |
---
|
10 |
# BakLLaVA Model Card
|
11 |
|
@@ -42,10 +46,23 @@ import requests
|
|
42 |
|
43 |
model_id = "llava-hf/bakLlava-v1-hf"
|
44 |
pipe = pipeline("image-to-text", model=model_id)
|
45 |
-
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
|
46 |
|
|
|
47 |
image = Image.open(requests.get(url, stream=True).raw)
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
outputs = pipe(image, prompt=prompt, generate_kwargs={"max_new_tokens": 200})
|
51 |
print(outputs)
|
@@ -64,10 +81,6 @@ import torch
|
|
64 |
from transformers import AutoProcessor, LlavaForConditionalGeneration
|
65 |
|
66 |
model_id = "llava-hf/bakLlava-v1-hf"
|
67 |
-
|
68 |
-
prompt = "USER: <image>\nWhat are these?\nASSISTANT:"
|
69 |
-
image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
70 |
-
|
71 |
model = LlavaForConditionalGeneration.from_pretrained(
|
72 |
model_id,
|
73 |
torch_dtype=torch.float16,
|
@@ -76,6 +89,21 @@ model = LlavaForConditionalGeneration.from_pretrained(
|
|
76 |
|
77 |
processor = AutoProcessor.from_pretrained(model_id)
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
raw_image = Image.open(requests.get(image_file, stream=True).raw)
|
80 |
inputs = processor(prompt, raw_image, return_tensors='pt').to(0, torch.float16)
|
81 |
|
|
|
6 |
pipeline_tag: image-to-text
|
7 |
inference: false
|
8 |
arxiv: 2304.08485
|
9 |
+
license: llama2
|
10 |
+
tags:
|
11 |
+
- vision
|
12 |
+
- image-text-to-text
|
13 |
---
|
14 |
# BakLLaVA Model Card
|
15 |
|
|
|
46 |
|
47 |
model_id = "llava-hf/bakLlava-v1-hf"
|
48 |
pipe = pipeline("image-to-text", model=model_id)
|
|
|
49 |
|
50 |
+
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
|
51 |
image = Image.open(requests.get(url, stream=True).raw)
|
52 |
+
|
53 |
+
# Define a chat histiry and use `apply_chat_template` to get correctly formatted prompt
|
54 |
+
# Each value in "content" has to be a list of dicts with types ("text", "image")
|
55 |
+
conversation = [
|
56 |
+
{
|
57 |
+
|
58 |
+
"role": "user",
|
59 |
+
"content": [
|
60 |
+
{"type": "text", "text": "What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"},
|
61 |
+
{"type": "image"},
|
62 |
+
],
|
63 |
+
},
|
64 |
+
]
|
65 |
+
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
66 |
|
67 |
outputs = pipe(image, prompt=prompt, generate_kwargs={"max_new_tokens": 200})
|
68 |
print(outputs)
|
|
|
81 |
from transformers import AutoProcessor, LlavaForConditionalGeneration
|
82 |
|
83 |
model_id = "llava-hf/bakLlava-v1-hf"
|
|
|
|
|
|
|
|
|
84 |
model = LlavaForConditionalGeneration.from_pretrained(
|
85 |
model_id,
|
86 |
torch_dtype=torch.float16,
|
|
|
89 |
|
90 |
processor = AutoProcessor.from_pretrained(model_id)
|
91 |
|
92 |
+
# Define a chat histiry and use `apply_chat_template` to get correctly formatted prompt
|
93 |
+
# Each value in "content" has to be a list of dicts with types ("text", "image")
|
94 |
+
conversation = [
|
95 |
+
{
|
96 |
+
|
97 |
+
"role": "user",
|
98 |
+
"content": [
|
99 |
+
{"type": "text", "text": "What are these?"},
|
100 |
+
{"type": "image"},
|
101 |
+
],
|
102 |
+
},
|
103 |
+
]
|
104 |
+
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
105 |
+
|
106 |
+
image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
107 |
raw_image = Image.open(requests.get(image_file, stream=True).raw)
|
108 |
inputs = processor(prompt, raw_image, return_tensors='pt').to(0, torch.float16)
|
109 |
|