Views
No views yet
Attribution
This is an unmodified fork ofnanonets/Nanonets-OCR-s, created by the Qwen team. All weights, files and behaviour are identical to upstream — we rehost it so our experiments stay reproducible and version-pinned. The original license and all credit remain with the Qwen team. If you want the canonical model, please use the original.
$...$) and display ($$...$$) equations.<img> tags, making them digestible for LLM processing. It can describe various image types, including logos, charts, graphs and so on, detailing their content, style, and context.<signature> tag. This is crucial for processing legal and business documents.<watermark> tag.☐, ☑, ☒) for consistent and reliable processing.1from PIL import Image
2from transformers import AutoTokenizer, AutoProcessor, AutoModelForImageTextToText
3
4model_path = "nanonets/Nanonets-OCR-s"
5
6model = AutoModelForImageTextToText.from_pretrained(
7 model_path,
8 torch_dtype="auto",
9 device_map="auto",
10 attn_implementation="flash_attention_2"
11)
12model.eval()
13
14tokenizer = AutoTokenizer.from_pretrained(model_path)
15processor = AutoProcessor.from_pretrained(model_path)
16
17
18def ocr_page_with_nanonets_s(image_path, model, processor, max_new_tokens=4096):
19 prompt = """Extract the text from the above document as if you were reading it naturally. Return the tables in html format. Return the equations in LaTeX representation. If there is an image in the document and image caption is not present, add a small description of the image inside the <img></img> tag; otherwise, add the image caption inside <img></img>. Watermarks should be wrapped in brackets. Ex: <watermark>OFFICIAL COPY</watermark>. Page numbers should be wrapped in brackets. Ex: <page_number>14</page_number> or <page_number>9/22</page_number>. Prefer using ☐ and ☑ for check boxes."""
20 image = Image.open(image_path)
21 messages = [
22 {"role": "system", "content": "You are a helpful assistant."},
23 {"role": "user", "content": [
24 {"type": "image", "image": f"file://{image_path}"},
25 {"type": "text", "text": prompt},
26 ]},
27 ]
28 text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
29 inputs = processor(text=[text], images=[image], padding=True, return_tensors="pt")
30 inputs = inputs.to(model.device)
31
32 output_ids = model.generate(**inputs, max_new_tokens=max_new_tokens, do_sample=False)
33 generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs.input_ids, output_ids)]
34
35 output_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)
36 return output_text[0]
37
38image_path = "/path/to/your/document.jpg"
39result = ocr_page_with_nanonets_s(image_path, model, processor, max_new_tokens=15000)
40print(result)vllm serve nanonets/Nanonets-OCR-s1from openai import OpenAI
2import base64
3
4client = OpenAI(api_key="123", base_url="http://localhost:8000/v1")
5
6model = "nanonets/Nanonets-OCR-s"
7
8def encode_image(image_path):
9 with open(image_path, "rb") as image_file:
10 return base64.b64encode(image_file.read()).decode("utf-8")
11
12def ocr_page_with_nanonets_s(img_base64):
13 response = client.chat.completions.create(
14 model=model,
15 messages=[
16 {
17 "role": "user",
18 "content": [
19 {
20 "type": "image_url",
21 "image_url": {"url": f"data:image/png;base64,{img_base64}"},
22 },
23 {
24 "type": "text",
25 "text": "Extract the text from the above document as if you were reading it naturally. Return the tables in html format. Return the equations in LaTeX representation. If there is an image in the document and image caption is not present, add a small description of the image inside the <img></img> tag; otherwise, add the image caption inside <img></img>. Watermarks should be wrapped in brackets. Ex: <watermark>OFFICIAL COPY</watermark>. Page numbers should be wrapped in brackets. Ex: <page_number>14</page_number> or <page_number>9/22</page_number>. Prefer using ☐ and ☑ for check boxes.",
26 },
27 ],
28 }
29 ],
30 temperature=0.0,
31 max_tokens=15000
32 )
33 return response.choices[0].message.content
34
35test_img_path = "/path/to/your/document.jpg"
36img_base64 = encode_image(test_img_path)
37print(ocr_page_with_nanonets_s(img_base64))1pip install docext
2python -m docext.app.app --model_name hosted_vllm/nanonets/Nanonets-OCR-s@misc{Nanonets-OCR-S,
title={Nanonets-OCR-S: A model for transforming documents into structured markdown with intelligent content recognition and semantic tagging},
author={Souvik Mandal and Ashish Talewar and Paras Ahuja and Prathamesh Juvatkar},
year={2025},
}