Update README.md
Browse files
README.md
CHANGED
@@ -95,7 +95,8 @@ doc.load_from_document_tokens([doctags], [image])
|
|
95 |
# export as any format
|
96 |
# HTML
|
97 |
# print(doc.export_to_html())
|
98 |
-
|
|
|
99 |
# MD
|
100 |
# print(doc.export_to_markdown())
|
101 |
|
@@ -115,7 +116,8 @@ from transformers.image_utils import load_image
|
|
115 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
116 |
|
117 |
# Load images
|
118 |
-
|
|
|
119 |
|
120 |
# Initialize processor and model
|
121 |
processor = AutoProcessor.from_pretrained("ds4sd/SmolDocling-256M-preview")
|
@@ -131,24 +133,39 @@ messages = [
|
|
131 |
"role": "user",
|
132 |
"content": [
|
133 |
{"type": "image"},
|
134 |
-
{"type": "
|
|
|
135 |
]
|
136 |
},
|
137 |
]
|
138 |
|
139 |
# Prepare inputs
|
140 |
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
141 |
-
inputs = processor(text=prompt, images=[
|
142 |
inputs = inputs.to(DEVICE)
|
143 |
|
144 |
# Generate outputs
|
145 |
-
generated_ids = model.generate(**inputs, max_new_tokens=
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
``````
|
153 |
</details>
|
154 |
|
|
|
95 |
# export as any format
|
96 |
# HTML
|
97 |
# print(doc.export_to_html())
|
98 |
+
# with open(output_file, "w", encoding="utf-8") as f:
|
99 |
+
# f.write(doc.export_to_html())
|
100 |
# MD
|
101 |
# print(doc.export_to_markdown())
|
102 |
|
|
|
116 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
117 |
|
118 |
# Load images
|
119 |
+
page_1 = load_image("https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg")
|
120 |
+
page_2 = load_image("https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg")
|
121 |
|
122 |
# Initialize processor and model
|
123 |
processor = AutoProcessor.from_pretrained("ds4sd/SmolDocling-256M-preview")
|
|
|
133 |
"role": "user",
|
134 |
"content": [
|
135 |
{"type": "image"},
|
136 |
+
{"type": "image"},
|
137 |
+
{"type": "text", "text": "Convert this document to docling."}
|
138 |
]
|
139 |
},
|
140 |
]
|
141 |
|
142 |
# Prepare inputs
|
143 |
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
144 |
+
inputs = processor(text=prompt, images=[page_1, page_2], return_tensors="pt")
|
145 |
inputs = inputs.to(DEVICE)
|
146 |
|
147 |
# Generate outputs
|
148 |
+
generated_ids = model.generate(**inputs, max_new_tokens=8192)
|
149 |
+
prompt_length = inputs.input_ids.shape[1]
|
150 |
+
trimmed_generated_ids = generated_ids[:, prompt_length:]
|
151 |
+
doctags = processor.batch_decode(
|
152 |
+
trimmed_generated_ids,
|
153 |
+
skip_special_tokens=False,
|
154 |
+
)[0].lstrip()
|
155 |
+
|
156 |
+
# create a docling document
|
157 |
+
doc = DoclingDocument(name="Document")
|
158 |
+
|
159 |
+
# populate it
|
160 |
+
doc.load_from_document_tokens([doctags], [page_1, page_2])
|
161 |
|
162 |
+
# export as any format
|
163 |
+
# HTML
|
164 |
+
# print(doc.export_to_html())
|
165 |
+
# with open(output_file, "w", encoding="utf-8") as f:
|
166 |
+
# f.write(doc.export_to_html())
|
167 |
+
# MD
|
168 |
+
# print(doc.export_to_markdown())
|
169 |
``````
|
170 |
</details>
|
171 |
|