Image-Text-to-Text
PaddleOCR
Safetensors
English
Chinese
multilingual
paddleocr_vl
ERNIE4.5
PaddlePaddle
image-to-text
ocr
document-parse
layout
table
formula
chart
seal
spotting
conversational
custom_code
Eval Results
Instructions to use PaddlePaddle/PaddleOCR-VL-1.5 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PaddleOCR
How to use PaddlePaddle/PaddleOCR-VL-1.5 with PaddleOCR:
# See https://www.paddleocr.ai/latest/version3.x/pipeline_usage/PaddleOCR-VL.html to installation from paddleocr import PaddleOCRVL pipeline = PaddleOCRVL(pipeline_version="v1.5") output = pipeline.predict("path/to/document_image.png") for res in output: res.print() res.save_to_json(save_path="output") res.save_to_markdown(save_path="output") - Notebooks
- Google Colab
- Kaggle
update transformers usage
Browse files
README.md
CHANGED
|
@@ -169,49 +169,39 @@ Currently, the PaddleOCR-VL-1.5-0.9B model facilitates seamless inference via th
|
|
| 169 |
```python
|
| 170 |
from PIL import Image
|
| 171 |
import torch
|
| 172 |
-
from transformers import
|
|
|
|
|
|
|
| 173 |
|
| 174 |
# ---- Settings ----
|
| 175 |
-
model_path = "PaddlePaddle/PaddleOCR-VL"
|
| 176 |
image_path = "test.png"
|
| 177 |
-
task = "ocr" # Options: 'ocr' | 'table' | 'chart' | 'formula'
|
| 178 |
# ------------------
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
PROMPTS = {
|
| 183 |
-
"ocr": "OCR:",
|
| 184 |
-
"table": "Table Recognition:",
|
| 185 |
-
"formula": "Formula Recognition:",
|
| 186 |
-
"chart": "Chart Recognition:",
|
| 187 |
-
}
|
| 188 |
-
|
| 189 |
image = Image.open(image_path).convert("RGB")
|
| 190 |
-
|
| 191 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 192 |
-
model_path, trust_remote_code=True, torch_dtype=torch.bfloat16
|
| 193 |
-
).to(DEVICE).eval()
|
| 194 |
-
processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
|
| 195 |
-
|
| 196 |
messages = [
|
| 197 |
-
{
|
| 198 |
-
|
|
|
|
| 199 |
{"type": "image", "image": image},
|
| 200 |
{"type": "text", "text": PROMPTS[task]},
|
| 201 |
]
|
| 202 |
}
|
| 203 |
]
|
| 204 |
inputs = processor.apply_chat_template(
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
).to(
|
| 211 |
-
|
| 212 |
-
outputs = model.generate(**inputs, max_new_tokens=
|
| 213 |
-
|
| 214 |
-
print(
|
| 215 |
```
|
| 216 |
|
| 217 |
<details>
|
|
@@ -223,60 +213,41 @@ pip install flash-attn --no-build-isolation
|
|
| 223 |
```
|
| 224 |
|
| 225 |
```python
|
| 226 |
-
import torch
|
| 227 |
-
from transformers import AutoModelForCausalLM, AutoProcessor
|
| 228 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
# ---- Settings ----
|
| 231 |
-
model_path = "PaddlePaddle/PaddleOCR-VL"
|
| 232 |
image_path = "test.png"
|
| 233 |
-
task = "ocr" #
|
| 234 |
# ------------------
|
| 235 |
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
model_path,
|
| 240 |
-
trust_remote_code=True,
|
| 241 |
-
torch_dtype=torch.bfloat16,
|
| 242 |
-
attn_implementation="flash_attention_2",
|
| 243 |
-
).to(dtype=torch.bfloat16, device=DEVICE).eval()
|
| 244 |
-
processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
|
| 245 |
-
|
| 246 |
-
PROMPTS = {
|
| 247 |
-
"ocr": "OCR:",
|
| 248 |
-
"table": "Table Recognition:",
|
| 249 |
-
"chart": "Chart Recognition:",
|
| 250 |
-
"formula": "Formula Recognition:",
|
| 251 |
-
}
|
| 252 |
messages = [
|
| 253 |
{
|
| 254 |
-
"role": "user",
|
| 255 |
"content": [
|
| 256 |
-
{"type": "image", "image":
|
| 257 |
-
{"type": "text",
|
| 258 |
]
|
| 259 |
}
|
| 260 |
]
|
| 261 |
-
|
| 262 |
inputs = processor.apply_chat_template(
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
).to(
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
max_new_tokens=1024,
|
| 274 |
-
do_sample=False,
|
| 275 |
-
use_cache=True
|
| 276 |
-
)
|
| 277 |
-
|
| 278 |
-
outputs = processor.batch_decode(out, skip_special_tokens=True)[0]
|
| 279 |
-
print(outputs)
|
| 280 |
```
|
| 281 |
|
| 282 |
</details>
|
|
|
|
| 169 |
```python
|
| 170 |
from PIL import Image
|
| 171 |
import torch
|
| 172 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
| 173 |
+
|
| 174 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 175 |
|
| 176 |
# ---- Settings ----
|
| 177 |
+
model_path = "PaddlePaddle/PaddleOCR-VL-1.5"
|
| 178 |
image_path = "test.png"
|
| 179 |
+
task = "ocr" # Options: 'ocr' | 'table' | 'chart' | 'formula' | 'spotting'
|
| 180 |
# ------------------
|
| 181 |
|
| 182 |
+
model = AutoModelForImageTextToText.from_pretrained(model_path, dtype="bfloat16").to(DEVICE).eval()
|
| 183 |
+
processor = AutoProcessor.from_pretrained(model_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
image = Image.open(image_path).convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
messages = [
|
| 186 |
+
{
|
| 187 |
+
"role": "user",
|
| 188 |
+
"content": [
|
| 189 |
{"type": "image", "image": image},
|
| 190 |
{"type": "text", "text": PROMPTS[task]},
|
| 191 |
]
|
| 192 |
}
|
| 193 |
]
|
| 194 |
inputs = processor.apply_chat_template(
|
| 195 |
+
messages,
|
| 196 |
+
add_generation_prompt=True,
|
| 197 |
+
tokenize=True,
|
| 198 |
+
return_dict=True,
|
| 199 |
+
return_tensors="pt",
|
| 200 |
+
).to(model.device)
|
| 201 |
+
|
| 202 |
+
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 203 |
+
result = processor.decode(outputs[0][inputs["input_ids"].shape[-1]:-1])
|
| 204 |
+
print(result)
|
| 205 |
```
|
| 206 |
|
| 207 |
<details>
|
|
|
|
| 213 |
```
|
| 214 |
|
| 215 |
```python
|
|
|
|
|
|
|
| 216 |
from PIL import Image
|
| 217 |
+
import torch
|
| 218 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
| 219 |
+
|
| 220 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 221 |
|
| 222 |
# ---- Settings ----
|
| 223 |
+
model_path = "PaddlePaddle/PaddleOCR-VL-1.5"
|
| 224 |
image_path = "test.png"
|
| 225 |
+
task = "ocr" # Options: 'ocr' | 'table' | 'chart' | 'formula' | 'spotting'
|
| 226 |
# ------------------
|
| 227 |
|
| 228 |
+
model = AutoModelForImageTextToText.from_pretrained(model_path, dtype="bfloat16", attn_implementation="flash_attention_2").to(DEVICE).eval()
|
| 229 |
+
processor = AutoProcessor.from_pretrained(model_path)
|
| 230 |
+
image = Image.open(image_path).convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
messages = [
|
| 232 |
{
|
| 233 |
+
"role": "user",
|
| 234 |
"content": [
|
| 235 |
+
{"type": "image", "image": image},
|
| 236 |
+
{"type": "text", "text": PROMPTS[task]},
|
| 237 |
]
|
| 238 |
}
|
| 239 |
]
|
|
|
|
| 240 |
inputs = processor.apply_chat_template(
|
| 241 |
+
messages,
|
| 242 |
+
add_generation_prompt=True,
|
| 243 |
+
tokenize=True,
|
| 244 |
+
return_dict=True,
|
| 245 |
+
return_tensors="pt",
|
| 246 |
+
).to(model.device)
|
| 247 |
+
|
| 248 |
+
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 249 |
+
result = processor.decode(outputs[0][inputs["input_ids"].shape[-1]:-1])
|
| 250 |
+
print(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
```
|
| 252 |
|
| 253 |
</details>
|