import json | |
from typing import List | |
import requests as r | |
import base64 | |
from PIL import Image | |
from io import BytesIO | |
ENDPOINT_URL = "https://j0thokqylseue22z.us-east-1.aws.endpoints.huggingface.cloud" # 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]) | |