Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
```python
|
| 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 'cuda'
|
| 15 |
+
|
| 16 |
+
# create model
|
| 17 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
+
'qnguyen3/siglip-quyen-0.5b-chatml',
|
| 19 |
+
torch_dtype=torch.float16,
|
| 20 |
+
device_map='auto',
|
| 21 |
+
trust_remote_code=True)
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 23 |
+
'qnguyen3/siglip-quyen-0.5b-chatml',
|
| 24 |
+
trust_remote_code=True)
|
| 25 |
+
|
| 26 |
+
# text prompt
|
| 27 |
+
prompt = 'Describe this image in detail'
|
| 28 |
+
|
| 29 |
+
messages = [
|
| 30 |
+
{"role": "user", "content": f'<image>\n{prompt}'}
|
| 31 |
+
]
|
| 32 |
+
text = tokenizer.apply_chat_template(
|
| 33 |
+
messages,
|
| 34 |
+
tokenize=False,
|
| 35 |
+
add_generation_prompt=True
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
print(text)
|
| 39 |
+
|
| 40 |
+
text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
|
| 41 |
+
input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0)
|
| 42 |
+
|
| 43 |
+
# image, sample images can be found in images folder
|
| 44 |
+
image = Image.open('/home/qnguyen3/qnguyen3/nanoLLaVA/icon.png')
|
| 45 |
+
image_tensor = model.process_images([image], model.config).to(dtype=model.dtype)
|
| 46 |
+
|
| 47 |
+
# generate
|
| 48 |
+
output_ids = model.generate(
|
| 49 |
+
input_ids,
|
| 50 |
+
images=image_tensor,
|
| 51 |
+
max_new_tokens=1024,
|
| 52 |
+
use_cache=True)[0]
|
| 53 |
+
|
| 54 |
+
print(tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip())
|
| 55 |
+
```
|