-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnanonets.py
More file actions
38 lines (31 loc) · 984 Bytes
/
Copy pathnanonets.py
File metadata and controls
38 lines (31 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import torch
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
from PIL import Image
# Load model and processor
model = Qwen2VLForConditionalGeneration.from_pretrained(
"Qwen/Qwen2-VL-2B-Instruct",
torch_dtype="auto",
device_map="auto"
)
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
# Load the image
image = Image.open("img/03.jpg")
# Simple OCR prompt
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": image},
{"type": "text", "text": "Read all the text in this image."}
]
}
]
# Build prompt
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
# Process inputs
inputs = processor(prompt, return_tensors="pt").to(model.device)
# Generate output
generated_ids = model.generate(**inputs, max_new_tokens=512)
# Decode and print
output = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(output)