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
Instructions to use Tuannodev/PaddleOCR-VL-1.5 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PaddleOCR
How to use Tuannodev/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="Tuannodev/PaddleOCR-VL-1.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
Upload handler.py
Browse files- handler.py +86 -0
handler.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any
|
| 2 |
+
import torch
|
| 3 |
+
import base64
|
| 4 |
+
import io
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from transformers import AutoModelForCausalLM, AutoProcessor
|
| 7 |
+
|
| 8 |
+
PROMPTS = {
|
| 9 |
+
"ocr": "OCR:",
|
| 10 |
+
"table": "Table Recognition:",
|
| 11 |
+
"formula": "Formula Recognition:",
|
| 12 |
+
"chart": "Chart Recognition:",
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class EndpointHandler:
|
| 17 |
+
def __init__(self, path: str = ""):
|
| 18 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
+
self.processor = AutoProcessor.from_pretrained(path, trust_remote_code=True)
|
| 20 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
path,
|
| 22 |
+
trust_remote_code=True,
|
| 23 |
+
torch_dtype=torch.bfloat16,
|
| 24 |
+
).to(self.device).eval()
|
| 25 |
+
|
| 26 |
+
def _load_image(self, image_field):
|
| 27 |
+
if isinstance(image_field, Image.Image):
|
| 28 |
+
return image_field.convert("RGB")
|
| 29 |
+
if isinstance(image_field, (bytes, bytearray)):
|
| 30 |
+
return Image.open(io.BytesIO(image_field)).convert("RGB")
|
| 31 |
+
if isinstance(image_field, str):
|
| 32 |
+
data = image_field
|
| 33 |
+
if data.startswith("data:"):
|
| 34 |
+
data = data.split(",", 1)[1]
|
| 35 |
+
return Image.open(io.BytesIO(base64.b64decode(data))).convert("RGB")
|
| 36 |
+
raise ValueError("Unsupported image input type")
|
| 37 |
+
|
| 38 |
+
def __call__(self, data):
|
| 39 |
+
inputs_data = data.get("inputs", data)
|
| 40 |
+
if isinstance(inputs_data, str):
|
| 41 |
+
inputs_data = {"image": inputs_data}
|
| 42 |
+
|
| 43 |
+
image_field = inputs_data.get("image")
|
| 44 |
+
if image_field is None:
|
| 45 |
+
return {"error": "Missing 'image' (base64-encoded) in inputs"}
|
| 46 |
+
|
| 47 |
+
params = data.get("parameters", {}) if isinstance(data, dict) else {}
|
| 48 |
+
task = inputs_data.get("task") or params.get("task", "ocr")
|
| 49 |
+
prompt = (
|
| 50 |
+
inputs_data.get("prompt")
|
| 51 |
+
or params.get("prompt")
|
| 52 |
+
or PROMPTS.get(task, PROMPTS["ocr"])
|
| 53 |
+
)
|
| 54 |
+
max_new_tokens = int(
|
| 55 |
+
inputs_data.get("max_new_tokens")
|
| 56 |
+
or params.get("max_new_tokens", 1024)
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
image = self._load_image(image_field)
|
| 60 |
+
|
| 61 |
+
messages = [{
|
| 62 |
+
"role": "user",
|
| 63 |
+
"content": [
|
| 64 |
+
{"type": "image", "image": image},
|
| 65 |
+
{"type": "text", "text": prompt},
|
| 66 |
+
],
|
| 67 |
+
}]
|
| 68 |
+
|
| 69 |
+
model_inputs = self.processor.apply_chat_template(
|
| 70 |
+
messages,
|
| 71 |
+
tokenize=True,
|
| 72 |
+
add_generation_prompt=True,
|
| 73 |
+
return_dict=True,
|
| 74 |
+
return_tensors="pt",
|
| 75 |
+
).to(self.device)
|
| 76 |
+
|
| 77 |
+
with torch.inference_mode():
|
| 78 |
+
output_ids = self.model.generate(
|
| 79 |
+
**model_inputs,
|
| 80 |
+
max_new_tokens=max_new_tokens,
|
| 81 |
+
do_sample=False,
|
| 82 |
+
use_cache=True,
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
text = self.processor.batch_decode(output_ids, skip_special_tokens=True)[0]
|
| 86 |
+
return {"generated_text": text, "task": task}
|