File size: 6,681 Bytes
3b6df47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Setup & Installation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!apt install -y tesseract-ocr\n",
    "pip install pytesseract"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Create Custom Handler for Inference Endpoints\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting handler.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile handler.py\n",
    "from typing import Dict, List, Any\n",
    "from transformers import LayoutLMForTokenClassification, LayoutLMv2Processor\n",
    "import torch\n",
    "from subprocess import run\n",
    "\n",
    "# install tesseract-ocr and pytesseract\n",
    "run(\"apt install -y tesseract-ocr\", shell=True, check=True)\n",
    "run(\"pip install pytesseract\", shell=True, check=True)\n",
    "\n",
    "# helper function to unnormalize bboxes for drawing onto the image\n",
    "def unnormalize_box(bbox, width, height):\n",
    "    return [\n",
    "        width * (bbox[0] / 1000),\n",
    "        height * (bbox[1] / 1000),\n",
    "        width * (bbox[2] / 1000),\n",
    "        height * (bbox[3] / 1000),\n",
    "    ]\n",
    "\n",
    "\n",
    "# set device\n",
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
    "\n",
    "\n",
    "class EndpointHandler:\n",
    "    def __init__(self, path=\"\"):\n",
    "        # load model and processor from path\n",
    "        self.model = LayoutLMForTokenClassification.from_pretrained(\"philschmid/layoutlm-funsd\").to(device)\n",
    "        self.processor = LayoutLMv2Processor.from_pretrained(\"philschmid/layoutlm-funsd\")\n",
    "\n",
    "    def __call__(self, data: Dict[str, bytes]) -> Dict[str, List[Any]]:\n",
    "        \"\"\"\n",
    "        Args:\n",
    "            data (:obj:):\n",
    "                includes the deserialized image file as PIL.Image\n",
    "        \"\"\"\n",
    "        # process input\n",
    "        image = data.pop(\"inputs\", data)\n",
    "\n",
    "        # process image\n",
    "        encoding = self.processor(image, return_tensors=\"pt\")\n",
    "\n",
    "        # run prediction\n",
    "        with torch.inference_mode():\n",
    "            outputs = self.model(\n",
    "                input_ids=encoding.input_ids.to(device),\n",
    "                bbox=encoding.bbox.to(device),\n",
    "                attention_mask=encoding.attention_mask.to(device),\n",
    "                token_type_ids=encoding.token_type_ids.to(device),\n",
    "            )\n",
    "            predictions = outputs.logits.softmax(-1)\n",
    "\n",
    "        # post process output\n",
    "        result = []\n",
    "        for item, inp_ids, bbox in zip(\n",
    "            predictions.squeeze(0).cpu(), encoding.input_ids.squeeze(0).cpu(), encoding.bbox.squeeze(0).cpu()\n",
    "        ):\n",
    "            label = self.model.config.id2label[int(item.argmax().cpu())]\n",
    "            if label == \"O\":\n",
    "                continue\n",
    "            score = item.max().item()\n",
    "            text = self.processor.tokenizer.decode(inp_ids)\n",
    "            bbox = unnormalize_box(bbox.tolist(), image.width, image.height)\n",
    "            result.append({\"label\": label, \"score\": score, \"text\": text, \"bbox\": bbox})\n",
    "        return {\"predictions\": result}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "test custom pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from handler import EndpointHandler\n",
    "\n",
    "my_handler = EndpointHandler(\".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
     ]
    }
   ],
   "source": [
    "import base64\n",
    "from PIL import Image\n",
    "from io import BytesIO\n",
    "import json\n",
    "\n",
    "# read image from disk\n",
    "image = Image.open(\"invoice_example.png\")\n",
    "request = {\"inputs\":image }\n",
    "\n",
    "# test the handler\n",
    "pred = my_handler(request)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image, ImageDraw, ImageFont\n",
    "\n",
    "\n",
    "def draw_result(image,result):\n",
    "    label2color = {\n",
    "        \"B-HEADER\": \"blue\",\n",
    "        \"B-QUESTION\": \"red\",\n",
    "        \"B-ANSWER\": \"green\",\n",
    "        \"I-HEADER\": \"blue\",\n",
    "        \"I-QUESTION\": \"red\",\n",
    "        \"I-ANSWER\": \"green\",\n",
    "    }\n",
    "\n",
    "\n",
    "    # draw predictions over the image\n",
    "    draw = ImageDraw.Draw(image)\n",
    "    font = ImageFont.load_default()\n",
    "    for res in result:\n",
    "        draw.rectangle(res[\"bbox\"], outline=\"black\")\n",
    "        draw.rectangle(res[\"bbox\"], outline=label2color[res[\"label\"]])\n",
    "        draw.text((res[\"bbox\"][0] + 10, res[\"bbox\"][1] - 10), text=res[\"label\"], fill=label2color[res[\"label\"]], font=font)\n",
    "    return image\n",
    "\n",
    "draw_result(image,pred[\"predictions\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.13 ('dev': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "f6dd96c16031089903d5a31ec148b80aeb0d39c32affb1a1080393235fbfa2fc"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}