Abhilashvj's picture
Added custom handler
93910bd
from handler import EndpointHandler
import json
from typing import List
import requests as r
import base64
import requests as r
import base64
from PIL import Image
from io import BytesIO
ENDPOINT_URL = ""
HF_TOKEN = ""
def decode_base64_image(image_string):
base64_image = base64.b64decode(image_string)
buffer = BytesIO(base64_image)
return Image.open(buffer)
# init handler
my_handler = EndpointHandler(path=".")
# prepare sample payload
path_to_image = "test_images/lal.jpg"
with open(path_to_image, "rb") as i:
b64 = base64.b64encode(i.read())
payload = {"inputs": {"image": b64.decode("utf-8"), "class_text": "shirt", "prompt": "wedding shirt"}}
# test the handler
results=my_handler(payload)
# show results
# print("non_holiday_pred", non_holiday_pred)
# print("holiday_payload", holiday_payload)
decode_base64_image(results["image"]).save("test_results.jpg")
# def predict(path_to_image: str = None, candiates: List[str] = None):
# with open(path_to_image, "rb") as i:
# b64 = base64.b64encode(i.read())
# payload = {"inputs": {"image": b64.decode("utf-8"), "candiates": candiates}}
# response = r.post(
# ENDPOINT_URL, headers={"Authorization": f"Bearer {HF_TOKEN}"}, json=payload
# )
# return response.json()
# prediction = predict(
# path_to_image="palace.jpg", candiates=["sea", "palace", "car", "ship"]
# )