shimmyshimmer commited on
Commit
c7bdafc
Β·
verified Β·
1 Parent(s): dc403f1

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +152 -0
README.md ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ base_model:
5
+ - nanonets/Nanonets-OCR-s
6
+ pipeline_tag: image-text-to-text
7
+ tags:
8
+ - OCR
9
+ - unsloth
10
+ - pdf2markdown
11
+ library_name: transformers
12
+ ---
13
+ <div>
14
+ <p style="margin-top: 0;margin-bottom: 0;">
15
+ <em><a href="https://docs.unsloth.ai/basics/unsloth-dynamic-v2.0-gguf">Unsloth Dynamic 2.0</a> achieves superior accuracy & outperforms other leading quants.</em>
16
+ </p>
17
+ <div style="display: flex; gap: 5px; align-items: center; ">
18
+ <a href="https://github.com/unslothai/unsloth/">
19
+ <img src="https://github.com/unslothai/unsloth/raw/main/images/unsloth%20new%20logo.png" width="133">
20
+ </a>
21
+ <a href="https://discord.gg/unsloth">
22
+ <img src="https://github.com/unslothai/unsloth/raw/main/images/Discord%20button.png" width="173">
23
+ </a>
24
+ <a href="https://docs.unsloth.ai/basics/qwen3-how-to-run-and-fine-tune">
25
+ <img src="https://raw.githubusercontent.com/unslothai/unsloth/refs/heads/main/images/documentation%20green%20button.png" width="143">
26
+ </a>
27
+ </div>
28
+ </div>
29
+
30
+
31
+
32
+ Nanonets-OCR-s is a powerful, state-of-the-art image-to-markdown OCR model that goes far beyond traditional text extraction. It transforms documents into structured markdown with intelligent content recognition and semantic tagging, making it ideal for downstream processing by Large Language Models (LLMs).
33
+
34
+ Nanonets-OCR-s is packed with features designed to handle complex documents with ease:
35
+
36
+ * **LaTeX Equation Recognition:** Automatically converts mathematical equations and formulas into properly formatted LaTeX syntax. It distinguishes between inline (`$...$`) and display (`$$...$$`) equations.
37
+ * **Intelligent Image Description:** Describes images within documents using structured `<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.
38
+ * **Signature Detection & Isolation:** Identifies and isolates signatures from other text, outputting them within a `<signature>` tag. This is crucial for processing legal and business documents.
39
+ * **Watermark Extraction:** Detects and extracts watermark text from documents, placing it within a `<watermark>` tag.
40
+ * **Smart Checkbox Handling:** Converts form checkboxes and radio buttons into standardized Unicode symbols (`☐`, `β˜‘`, `β˜’`) for consistent and reliable processing.
41
+ * **Complex Table Extraction:** Accurately extracts complex tables from documents and converts them into both markdown and HTML table formats.
42
+
43
+
44
+ πŸ“’ [Read the full announcement](https://nanonets.com/research/nanonets-ocr-s) | πŸ€— [Hugging Face Space Demo](https://huggingface.co/spaces/Souvik3333/Nanonets-ocr-s)
45
+
46
+ ## Usage
47
+ ### Using transformers
48
+ ```python
49
+ from PIL import Image
50
+ from transformers import AutoTokenizer, AutoProcessor, AutoModelForImageTextToText
51
+
52
+ model_path = "nanonets/Nanonets-OCR-s"
53
+
54
+ model = AutoModelForImageTextToText.from_pretrained(
55
+ model_path,
56
+ torch_dtype="auto",
57
+ device_map="auto",
58
+ attn_implementation="flash_attention_2"
59
+ )
60
+ model.eval()
61
+
62
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
63
+ processor = AutoProcessor.from_pretrained(model_path)
64
+
65
+
66
+ def ocr_page_with_nanonets_s(image_path, model, processor, max_new_tokens=4096):
67
+ 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."""
68
+ image = Image.open(image_path)
69
+ messages = [
70
+ {"role": "system", "content": "You are a helpful assistant."},
71
+ {"role": "user", "content": [
72
+ {"type": "image", "image": f"file://{image_path}"},
73
+ {"type": "text", "text": prompt},
74
+ ]},
75
+ ]
76
+ text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
77
+ inputs = processor(text=[text], images=[image], padding=True, return_tensors="pt")
78
+ inputs = inputs.to(model.device)
79
+
80
+ output_ids = model.generate(**inputs, max_new_tokens=max_new_tokens, do_sample=False)
81
+ generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs.input_ids, output_ids)]
82
+
83
+ output_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)
84
+ return output_text[0]
85
+
86
+ image_path = "/path/to/your/document.jpg"
87
+ result = ocr_page_with_nanonets_s(image_path, model, processor, max_new_tokens=15000)
88
+ print(result)
89
+ ```
90
+
91
+ ### Using vLLM
92
+ 1. Start the vLLM server.
93
+ ```bash
94
+ vllm serve nanonets/Nanonets-OCR-s
95
+ ```
96
+ 2. Predict with the model
97
+ ```python
98
+ from openai import OpenAI
99
+ import base64
100
+
101
+ client = OpenAI(api_key="123", base_url="http://localhost:8000/v1")
102
+
103
+ model = "nanonets/Nanonets-OCR-s"
104
+
105
+ def encode_image(image_path):
106
+ with open(image_path, "rb") as image_file:
107
+ return base64.b64encode(image_file.read()).decode("utf-8")
108
+
109
+ def ocr_page_with_nanonets_s(img_base64):
110
+ response = client.chat.completions.create(
111
+ model=model,
112
+ messages=[
113
+ {
114
+ "role": "user",
115
+ "content": [
116
+ {
117
+ "type": "image_url",
118
+ "image_url": {"url": f"data:image/png;base64,{img_base64}"},
119
+ },
120
+ {
121
+ "type": "text",
122
+ "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.",
123
+ },
124
+ ],
125
+ }
126
+ ],
127
+ temperature=0.0,
128
+ max_tokens=15000
129
+ )
130
+ return response.choices[0].message.content
131
+
132
+ test_img_path = "/path/to/your/document.jpg"
133
+ img_base64 = encode_image(test_img_path)
134
+ print(ocr_page_with_nanonets_s(img_base64))
135
+ ```
136
+
137
+ ### Using docext
138
+ ```python
139
+ pip install docext
140
+ python -m docext.app.app --model_name hosted_vllm/nanonets/Nanonets-OCR-s
141
+ ```
142
+ Checkout [GitHub](https://github.com/NanoNets/docext/tree/dev/markdown) for more details.
143
+
144
+
145
+ ## BibTex
146
+ ```
147
+ @misc{Nanonets-OCR-S,
148
+ title={Nanonets-OCR-S: A model for transforming documents into structured markdown with intelligent content recognition and semantic tagging},
149
+ author={Souvik Mandal and Ashish Talewar and Paras Ahuja and Prathamesh Juvatkar},
150
+ year={2025},
151
+ }
152
+ ```