handler
Browse files- handler.py +25 -0
- requirements.txt +1 -0
handler.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
from transformers import LayoutLMForTokenClassification, LayoutLMv2Processor
|
3 |
+
import torch
|
4 |
+
from subprocess import run
|
5 |
+
import pytesseract
|
6 |
+
|
7 |
+
# install tesseract-ocr and pytesseract
|
8 |
+
run("apt install -y tesseract-ocr", shell=True, check=True)
|
9 |
+
|
10 |
+
# set device
|
11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
+
|
13 |
+
|
14 |
+
class EndpointHandler:
|
15 |
+
def __call__(self, data: Dict[str, bytes]) -> Dict[str, List[Any]]:
|
16 |
+
"""
|
17 |
+
Args:
|
18 |
+
data (:obj:):
|
19 |
+
includes the deserialized image file as PIL.Image
|
20 |
+
"""
|
21 |
+
# process input
|
22 |
+
image = data.pop("inputs", data)
|
23 |
+
|
24 |
+
result = pytesseract.image_to_string(image)
|
25 |
+
return {"predictions": result}
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
pytesseract
|