Views
No views yet

The Imgscope-OCR-2B-0527 model is a fine-tuned version of Qwen2-VL-2B-Instruct, specifically optimized for messy handwriting recognition, document OCR, realistic handwritten OCR, and math problem solving with LaTeX formatting. This model is trained on custom datasets for document and handwriting OCR tasks and integrates a conversational approach with strong visual and textual understanding for multi-modal applications.
1from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
2from qwen_vl_utils import process_vision_info
3
4# Load the model
5model = Qwen2VLForConditionalGeneration.from_pretrained(
6 "prithivMLmods/Imgscope-OCR-2B-0527", # replace with updated model ID if available
7 torch_dtype="auto",
8 device_map="auto"
9)
10
11# Optional: Flash Attention for performance optimization
12# model = Qwen2VLForConditionalGeneration.from_pretrained(
13# "prithivMLmods/Imgscope-OCR-2B-0527",
14# torch_dtype=torch.bfloat16,
15# attn_implementation="flash_attention_2",
16# device_map="auto",
17# )
18
19# Load processor
20processor = AutoProcessor.from_pretrained("prithivMLmods/Imgscope-OCR-2B-0527")
21
22messages = [
23 {
24 "role": "user",
25 "content": [
26 {
27 "type": "image",
28 "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
29 },
30 {"type": "text", "text": "Recognize the handwriting in this image."},
31 ],
32 }
33]
34
35# Prepare input
36text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
37image_inputs, video_inputs = process_vision_info(messages)
38inputs = processor(
39 text=[text],
40 images=image_inputs,
41 videos=video_inputs,
42 padding=True,
43 return_tensors="pt",
44)
45inputs = inputs.to("cuda")
46
47# Generate output
48generated_ids = model.generate(**inputs, max_new_tokens=128)
49generated_ids_trimmed = [
50 out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
51]
52output_text = processor.batch_decode(
53 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
54)
55print(output_text)


1buffer = ""
2for new_text in streamer:
3 buffer += new_text
4 buffer = buffer.replace("<|im_end|>", "")
5 yield buffer