File size: 3,003 Bytes
92f1a62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
641a625
 
 
 
 
 
 
da403c1
 
641a625
 
26e7373
 
 
 
 
 
 
 
 
641a625
26e7373
 
641a625
26e7373
 
 
641a625
26e7373
 
 
 
 
641a625
26e7373
641a625
 
26e7373
 
 
 
 
 
 
641a625
 
 
26e7373
641a625
 
26e7373
 
 
641a625
 
 
26e7373
 
 
 
 
641a625
 
26e7373
641a625
 
26e7373
641a625
 
26e7373
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import subprocess
import sys
import os

# Install transformers dev build that supports paddleocr_vl
subprocess.check_call([
    sys.executable, "-m", "pip", "install", "--quiet", "--upgrade",
    "git+https://github.com/huggingface/transformers.git",
])

# Monkey-patch old API that huggingface_inference_toolkit depends on
import transformers
if not hasattr(transformers, "file_utils"):
    import types
    file_utils_shim = types.ModuleType("transformers.file_utils")
    # Provide stubs for commonly used symbols
    file_utils_shim.is_tf_available = transformers.utils.is_tf_available
    file_utils_shim.is_torch_available = transformers.utils.is_torch_available
    sys.modules["transformers.file_utils"] = file_utils_shim
    transformers.file_utils = file_utils_shim

from transformers import AutoProcessor, AutoModelForImageTextToText
import torch
from PIL import Image
import requests
from io import BytesIO
import base64

MODEL_ID = "strangervisionhf/PaddleOCR-VL-1.5-hf-transformers-v5.2.0.dev0"


class EndpointHandler:
    def __init__(self, path=""):
        self.processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
        self.model = AutoModelForImageTextToText.from_pretrained(
            MODEL_ID,
            trust_remote_code=True,
            torch_dtype=torch.float16,
            device_map="auto",
        )
        self.model.eval()

    def __call__(self, data: dict):
        inputs_data = data.get("inputs", data)

        image_src = inputs_data.get("image", "")
        if not image_src:
            return {"error": "No image provided"}

        if image_src.startswith("http://") or image_src.startswith("https://"):
            response = requests.get(image_src, stream=True, timeout=30)
            image = Image.open(response.raw).convert("RGB")
        else:
            image = Image.open(BytesIO(base64.b64decode(image_src))).convert("RGB")

        prompt = inputs_data.get("text", "Recognize text in the image.")

        messages = [
            {
                "role": "user",
                "content": [
                    {"type": "image", "image": image},
                    {"type": "text", "text": prompt},
                ],
            }
        ]

        text_input = self.processor.apply_chat_template(
            messages, tokenize=False, add_generation_prompt=True
        )
        model_inputs = self.processor(
            text=[text_input],
            images=[image],
            return_tensors="pt",
        ).to(self.model.device)

        with torch.no_grad():
            output_ids = self.model.generate(
                **model_inputs,
                max_new_tokens=512,
                do_sample=False,
            )

        output_ids_trimmed = [
            o[len(i):] for i, o in zip(model_inputs.input_ids, output_ids)
        ]
        output_text = self.processor.batch_decode(
            output_ids_trimmed, skip_special_tokens=True
        )

        return {"generated_text": output_text[0]}