Commit
·
b3e445d
1
Parent(s):
9982797
Update README.md
Browse files
README.md
CHANGED
@@ -33,9 +33,38 @@ below is an example on how to run a request using Python and `requests`.
|
|
33 |
2. run request
|
34 |
|
35 |
```python
|
|
|
|
|
|
|
|
|
36 |
|
|
|
|
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
```
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
|
|
|
33 |
2. run request
|
34 |
|
35 |
```python
|
36 |
+
import json
|
37 |
+
from typing import List
|
38 |
+
import requests as r
|
39 |
+
import base64
|
40 |
|
41 |
+
ENDPOINT_URL = ""
|
42 |
+
HF_TOKEN = ""
|
43 |
|
44 |
+
|
45 |
+
def predict(path_to_image: str = None, candiates: List[str] = None):
|
46 |
+
with open(path_to_image, "rb") as i:
|
47 |
+
b64 = base64.b64encode(i.read())
|
48 |
+
|
49 |
+
payload = {"inputs": {"image": b64.decode("utf-8"), "candiates": candiates}}
|
50 |
+
response = r.post(
|
51 |
+
ENDPOINT_URL, headers={"Authorization": f"Bearer {HF_TOKEN}"}, json=payload
|
52 |
+
)
|
53 |
+
return response.json()
|
54 |
+
|
55 |
+
|
56 |
+
prediction = predict(
|
57 |
+
path_to_image="palace.jpg", candiates=["sea", "palace", "car", "ship"]
|
58 |
+
)
|
59 |
```
|
60 |
|
61 |
+
expected output
|
62 |
+
|
63 |
+
```python
|
64 |
+
[{'label': 'palace', 'score': 0.9996134638786316},
|
65 |
+
{'label': 'car', 'score': 0.0002602009626571089},
|
66 |
+
{'label': 'ship', 'score': 0.00011758189066313207},
|
67 |
+
{'label': 'sea', 'score': 8.666840585647151e-06}]
|
68 |
+
```
|
69 |
|
70 |
|