File size: 1,379 Bytes
636182d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"]
# )