File size: 1,773 Bytes
0df5db6
 
3620d80
0df5db6
 
 
 
f019a93
 
3620d80
f019a93
3620d80
d83fbf1
3620d80
c1744f6
93018f7
 
c1744f6
697c48f
d83fbf1
e8f70dd
 
3620d80
d83fbf1
e636a5a
d83fbf1
3620d80
7d2aaba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e636a5a
3620d80
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
---
tags:
- document-understanding
- endpoints-template
library_name: generic
---

# Deploy a Space as inference Endpoint

_This is a fork of the [naver-clova-ix/donut-base-finetuned-cord-v2](https://huggingface.co/spaces/naver-clova-ix/donut-base-finetuned-cord-v2) Space.

This repository implements a custom container for 🤗 Inference Endpoints using a Gradio space.

To deploy this model as an Inference Endpoint, you have to select Custom as task and a custom image.

* CPU image: `philschmi/gradio-api:cpu`
* GPU image: `philschmi/gradio-api:gpu`
* PORT: `7860`
* ~Health Route: `/`~-> is default

Also make sure to add `server_name="0.0.0.0"` in your `launch()` call to make sure the request is correct proxied.

If you want to use the UI with the Inference Endpoint, you have to select as endpoint type `public` and add [auth through gradio](https://gradio.app/docs/#launch-header)
 
### Example API Request Payload

Get an image you want to use, e.g.

```bash
!wget https://datasets-server.huggingface.co/assets/naver-clova-ix/cord-v2/--/naver-clova-ix--cord-v2/train/0/image/image.jpg
```

run inference

```python
import requests as r
import base64

ENDPOINT_URL = ""
HF_TOKEN = ""

def predict(path_to_image: str = None):
    ext = path_to_image.split('.')[-1]
    prefix = f'data:image/{ext};base64,'
    with open(path_to_image, 'rb') as f:
        img = f.read()

    payload = {"data": [prefix + base64.b64encode(img).decode('utf-8')]}
    response = r.post(
        f"{ENDPOINT_URL}/api/predict", headers={"Authorization": f"Bearer {HF_TOKEN}"}, json=payload
    )
    if response.status_code == 200:
        return response.json()
    else:
        raise Exception(f"Error: {response.status_code}")


prediction = predict(path_to_image="image.jpg")

```