Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
```
|
2 |
+
import torch
|
3 |
+
import transformers
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
from PIL import Image
|
6 |
+
import warnings
|
7 |
+
|
8 |
+
# disable some warnings
|
9 |
+
transformers.logging.set_verbosity_error()
|
10 |
+
transformers.logging.disable_progress_bar()
|
11 |
+
warnings.filterwarnings('ignore')
|
12 |
+
|
13 |
+
# set device
|
14 |
+
torch.set_default_device('cuda') # or 'cpu'
|
15 |
+
|
16 |
+
model_name = 'fne/dolphin-llava-72b'
|
17 |
+
|
18 |
+
# create model
|
19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
20 |
+
model_name,
|
21 |
+
torch_dtype=torch.float16,
|
22 |
+
device_map='auto',
|
23 |
+
trust_remote_code=True)
|
24 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
25 |
+
model_name,
|
26 |
+
trust_remote_code=True)
|
27 |
+
|
28 |
+
# text prompt
|
29 |
+
prompt = 'Describe this image in detail'
|
30 |
+
|
31 |
+
messages = [
|
32 |
+
{"role": "user", "content": f'<image>\n{prompt}'}
|
33 |
+
]
|
34 |
+
text = tokenizer.apply_chat_template(
|
35 |
+
messages,
|
36 |
+
tokenize=False,
|
37 |
+
add_generation_prompt=True
|
38 |
+
)
|
39 |
+
|
40 |
+
print(text)
|
41 |
+
|
42 |
+
text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
|
43 |
+
input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0)
|
44 |
+
|
45 |
+
# image, sample images can be found in images folder
|
46 |
+
image = Image.open('/path/to/image.png')
|
47 |
+
image_tensor = model.process_images([image], model.config).to(dtype=model.dtype)
|
48 |
+
|
49 |
+
# generate
|
50 |
+
output_ids = model.generate(
|
51 |
+
input_ids,
|
52 |
+
images=image_tensor,
|
53 |
+
max_new_tokens=2048,
|
54 |
+
use_cache=True)[0]
|
55 |
+
|
56 |
+
print(tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip())
|
57 |
+
```
|