Views
No views yet


pip install perceptron1import torch
2from transformers import AutoModelForCausalLM, AutoProcessor
3from transformers.image_utils import load_image
4from transformers.utils.import_utils import is_torch_cuda_available
5
6def document_to_messages(document: list[dict]):
7 messages, images = [], []
8 for item in document:
9 if not (content := item.get("content")):
10 continue
11 role = item.get("role", "user")
12 if item.get("type") == "image":
13 images.append(load_image(content))
14 messages.append({"role": role, "content": "<image>"})
15 elif item.get("type") == "text":
16 messages.append({"role": role, "content": content})
17 return messages, images
18
19hf_path = "PerceptronAI/Isaac-0.2-1B"
20device, dtype = ("cuda",torch.bfloat16) if is_torch_cuda_available() else ("cpu",torch.float32)
21
22# Load model/processor from the checkpoint
23processor = AutoProcessor.from_pretrained(hf_path, trust_remote_code=True)
24model = AutoModelForCausalLM.from_pretrained(
25 hf_path, trust_remote_code=True, vision_attn_implementation="flash_attention_2"
26)
27model = model.to(device=device, dtype=dtype)
28model.eval()
29
30# Prepare input for generation
31document = [
32 {
33 "type": "text",
34 "content": "<hint>BOX</hint>",
35 "role": "user",
36 },
37 {
38 "type": "image",
39 "content": "https://raw.githubusercontent.com/perceptron-ai-inc/perceptron/refs/heads/main/huggingface/assets/example.webp",
40 "role": "user",
41 },
42 {
43 "type": "text",
44 "content": "Determine whether it is safe to cross the street. Look for signage and moving traffic.",
45 "role": "user",
46 },
47]
48messages, images = document_to_messages(document)
49text = processor.apply_chat_template(
50 messages, tokenize=False, add_generation_prompt=True
51)
52inputs = processor(text=text, images=images, return_tensors="pt")
53
54# Generate text using the model
55generated_ids = model.generate(
56 tensor_stream=inputs["tensor_stream"].to(next(model.parameters()).device),
57 max_new_tokens=256,
58 do_sample=False,
59)
60generated_text = processor.tokenizer.decode(
61 generated_ids[0], skip_special_tokens=False
62)
63print(f"\nFull generated output:\n{generated_text}")