Views
No views yet
transformers and qwen_vl_utils library:!pip install transformers qwen_vl_utils accelerate>=0.26.0 PEFT -U
!pip install -U bitsandbytes1from PIL import Image
2from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
3import torch
4import os
5from qwen_vl_utils import process_vision_info
6
7
8
9model_name = "oddadmix/Khanandeh-0.1-Persian-OCR-2B-Instruct"
10model = Qwen2VLForConditionalGeneration.from_pretrained(
11 model_name,
12 torch_dtype="auto",
13 device_map="auto"
14 )
15processor = AutoProcessor.from_pretrained(model_name)
16max_tokens = 2000
17
18prompt = "Below is the image of one page of a document, as well as some raw textual content that was previously extracted for it. Just return the plain text representation of this document as if you were reading it naturally. Do not hallucinate."
19image.save("image.png")
20
21messages = [
22 {
23 "role": "user",
24 "content": [
25 {"type": "image", "image": f"file://{src}"},
26 {"type": "text", "text": prompt},
27 ],
28 }
29]
30text = processor.apply_chat_template(
31 messages, tokenize=False, add_generation_prompt=True
32)
33image_inputs, video_inputs = process_vision_info(messages)
34inputs = processor(
35 text=[text],
36 images=image_inputs,
37 videos=video_inputs,
38 padding=True,
39 return_tensors="pt",
40)
41inputs = inputs.to("cuda")
42generated_ids = model.generate(**inputs, max_new_tokens=max_tokens)
43generated_ids_trimmed = [
44 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
45]
46output_text = processor.batch_decode(
47 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
48)[0]
49os.remove(src)
50print(output_text)
51