Image-to-Text
Transformers
PyTorch
vision-encoder-decoder
image-text-to-text
donut
vision
endpoints-template
Instructions to use philschmid/donut-base-finetuned-cord-v2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use philschmid/donut-base-finetuned-cord-v2 with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "image-to-text" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # 'pip install "transformers<5.0.0' from transformers import pipeline pipe = pipeline("image-to-text", model="philschmid/donut-base-finetuned-cord-v2")# Load model directly from transformers import AutoTokenizer, AutoModelForMultimodalLM tokenizer = AutoTokenizer.from_pretrained("philschmid/donut-base-finetuned-cord-v2") model = AutoModelForMultimodalLM.from_pretrained("philschmid/donut-base-finetuned-cord-v2", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Commit ·
74473b1
1
Parent(s): 734ffa0
readme
Browse files- README.md +50 -0
- create_handler.ipynb +24 -9
README.md
CHANGED
|
@@ -20,4 +20,54 @@ Donut consists of a vision encoder (Swin Transformer) and a text decoder (BART).
|
|
| 20 |
|
| 21 |
# Use with Inference Endpoints
|
| 22 |
|
|
|
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Use with Inference Endpoints
|
| 22 |
|
| 23 |
+
Hugging Face Inference endpoints can directly work with binary data, this means that we can directly send our image from our document to the endpoint. We are going to use requests to send our requests. (make your you have it installed `pip install requests`)
|
| 24 |
|
| 25 |
+
load sample image
|
| 26 |
+
|
| 27 |
+
```bash
|
| 28 |
+
wget https://huggingface.co/philschmid/donut-base-finetuned-cord-v2/resolve/main/sample.png
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
send request to endpoint
|
| 32 |
+
|
| 33 |
+
```python
|
| 34 |
+
import json
|
| 35 |
+
import requests as r
|
| 36 |
+
import mimetypes
|
| 37 |
+
|
| 38 |
+
ENDPOINT_URL="" # url of your endpoint
|
| 39 |
+
HF_TOKEN="" # organization token where you deployed your endpoint
|
| 40 |
+
|
| 41 |
+
def predict(path_to_image:str=None):
|
| 42 |
+
with open(path_to_image, "rb") as i:
|
| 43 |
+
b = i.read()
|
| 44 |
+
headers= {
|
| 45 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 46 |
+
"Content-Type": mimetypes.guess_type(path_to_image)[0]
|
| 47 |
+
}
|
| 48 |
+
response = r.post(ENDPOINT_URL, headers=headers, data=b)
|
| 49 |
+
return response.json()
|
| 50 |
+
|
| 51 |
+
prediction = predict(path_to_image="sample.png")
|
| 52 |
+
|
| 53 |
+
print(prediction)
|
| 54 |
+
# {'menu': [{'nm': '0571-1854 BLUS WANITA',
|
| 55 |
+
# 'unitprice': '@120.000',
|
| 56 |
+
# 'cnt': '1',
|
| 57 |
+
# 'price': '120,000'},
|
| 58 |
+
# {'nm': '1002-0060 SHOPPING BAG', 'cnt': '1', 'price': '0'}],
|
| 59 |
+
# 'total': {'total_price': '120,000',
|
| 60 |
+
# 'changeprice': '0',
|
| 61 |
+
# 'creditcardprice': '120,000',
|
| 62 |
+
# 'menuqty_cnt': '1'}}
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
**curl example**
|
| 66 |
+
|
| 67 |
+
```bash
|
| 68 |
+
curl https://ak7gduay2ypyr9vp.us-east-1.aws.endpoints.huggingface.cloud \
|
| 69 |
+
-X POST \
|
| 70 |
+
--data-binary 'sample.png' \
|
| 71 |
+
-H "Authorization: Bearer XXX" \
|
| 72 |
+
-H "Content-Type: null"
|
| 73 |
+
```
|
create_handler.ipynb
CHANGED
|
@@ -99,18 +99,26 @@
|
|
| 99 |
},
|
| 100 |
{
|
| 101 |
"cell_type": "code",
|
| 102 |
-
"execution_count":
|
| 103 |
"metadata": {},
|
| 104 |
"outputs": [
|
| 105 |
{
|
| 106 |
-
"
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
}
|
| 115 |
],
|
| 116 |
"source": [
|
|
@@ -120,6 +128,13 @@
|
|
| 120 |
"\n",
|
| 121 |
"my_handler(payload)"
|
| 122 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
}
|
| 124 |
],
|
| 125 |
"metadata": {
|
|
|
|
| 99 |
},
|
| 100 |
{
|
| 101 |
"cell_type": "code",
|
| 102 |
+
"execution_count": 18,
|
| 103 |
"metadata": {},
|
| 104 |
"outputs": [
|
| 105 |
{
|
| 106 |
+
"data": {
|
| 107 |
+
"text/plain": [
|
| 108 |
+
"{'menu': [{'nm': '0571-1854 BLUS WANITA',\n",
|
| 109 |
+
" 'unitprice': '@120.000',\n",
|
| 110 |
+
" 'cnt': '1',\n",
|
| 111 |
+
" 'price': '120,000'},\n",
|
| 112 |
+
" {'nm': '1002-0060 SHOPPING BAG', 'cnt': '1', 'price': '0'}],\n",
|
| 113 |
+
" 'total': {'total_price': '120,000',\n",
|
| 114 |
+
" 'changeprice': '0',\n",
|
| 115 |
+
" 'creditcardprice': '120,000',\n",
|
| 116 |
+
" 'menuqty_cnt': '1'}}"
|
| 117 |
+
]
|
| 118 |
+
},
|
| 119 |
+
"execution_count": 18,
|
| 120 |
+
"metadata": {},
|
| 121 |
+
"output_type": "execute_result"
|
| 122 |
}
|
| 123 |
],
|
| 124 |
"source": [
|
|
|
|
| 128 |
"\n",
|
| 129 |
"my_handler(payload)"
|
| 130 |
]
|
| 131 |
+
},
|
| 132 |
+
{
|
| 133 |
+
"cell_type": "code",
|
| 134 |
+
"execution_count": null,
|
| 135 |
+
"metadata": {},
|
| 136 |
+
"outputs": [],
|
| 137 |
+
"source": []
|
| 138 |
}
|
| 139 |
],
|
| 140 |
"metadata": {
|