File size: 944 Bytes
c1a2fc4 609d64d c589fdd c1a2fc4 c589fdd c1a2fc4 c589fdd c1a2fc4 |
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 |
import json
from typing import List
import requests as r
import base64
from PIL import Image
from io import BytesIO
ENDPOINT_URL = "https://XXXXXX" # your endpoint url
HF_TOKEN = "#######################" # your huggingface token `hf_xxx`
# helper image utils
def encode_image(image_path):
with open(image_path, "rb") as i:
b64 = base64.b64encode(i.read())
return b64.decode("utf-8")
def predict(image):
image = encode_image(image)
# prepare sample payload
payload = {"image": image, "inputs": ""}
# headers
headers = {
"Authorization": f"Bearer {HF_TOKEN}",
"Content-Type": "application/json",
}
response = r.post(ENDPOINT_URL, headers=headers, json=payload)
if response.status_code != 200:
print(response.text)
raise Exception("Prediction failed")
return response.json()
prediction = predict(
image = "test.jpeg"
)
print(prediction[1])
print(prediction[2])
|