Upload examples.py with huggingface_hub
Browse files- examples.py +56 -0
examples.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModel, AutoImageProcessor
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
path = "nvidia/Llama-3.1-Nemotron-Nano-VL-8B-V1"
|
| 6 |
+
model = AutoModel.from_pretrained(
|
| 7 |
+
path,
|
| 8 |
+
torch_dtype=torch.bfloat16,
|
| 9 |
+
low_cpu_mem_usage=True,
|
| 10 |
+
trust_remote_code=True,
|
| 11 |
+
device_map="cuda",).eval()
|
| 12 |
+
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(path)
|
| 14 |
+
image_processor = AutoImageProcessor.from_pretrained(path, device="cuda", trust_remote_code=True)
|
| 15 |
+
|
| 16 |
+
generation_config = dict(max_new_tokens=1024, do_sample=False, eos_token_id=tokenizer.eos_token_id)
|
| 17 |
+
|
| 18 |
+
# pure-text conversation
|
| 19 |
+
question = 'What happened in 1986?'
|
| 20 |
+
response, history = model.chat(
|
| 21 |
+
tokenizer, None, question, generation_config, history=None, return_history=True
|
| 22 |
+
)
|
| 23 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 24 |
+
|
| 25 |
+
# single-image single-round conversation
|
| 26 |
+
image_path = 'images/table.png'
|
| 27 |
+
image_features = image_processor(Image.open(image_path))
|
| 28 |
+
question = '<image>\nExtract the table in this image as HTML.'
|
| 29 |
+
response = model.chat(
|
| 30 |
+
tokenizer=tokenizer, question=question, generation_config=generation_config,
|
| 31 |
+
**image_features
|
| 32 |
+
)
|
| 33 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 34 |
+
|
| 35 |
+
# single-image single-round conversation
|
| 36 |
+
image_path = 'images/tech.png'
|
| 37 |
+
image_features = image_processor(Image.open(image_path))
|
| 38 |
+
question = '<image>\nList in bullet point the most important Technological breakthrough of Nvidia Hopper.'
|
| 39 |
+
response = model.chat(
|
| 40 |
+
tokenizer=tokenizer, question=question, generation_config=generation_config,
|
| 41 |
+
**image_features
|
| 42 |
+
)
|
| 43 |
+
print(f'User: {question}\nAssistant: {response}')
|
| 44 |
+
|
| 45 |
+
# two image single-round conversation
|
| 46 |
+
image_features = image_processor([
|
| 47 |
+
Image.open('images/example1a.jpeg'),
|
| 48 |
+
Image.open('images/example1b.jpeg')
|
| 49 |
+
])
|
| 50 |
+
|
| 51 |
+
question = '<image-1>: <image>\n<image-2>: <image>\nBriefly describe the two images.'
|
| 52 |
+
response = model.chat(
|
| 53 |
+
tokenizer=tokenizer, question=question, generation_config=generation_config,
|
| 54 |
+
**image_features
|
| 55 |
+
)
|
| 56 |
+
print(f'User: {question}\nAssistant: {response}')
|