Yan commited on
Commit
c1a2fc4
·
1 Parent(s): 88393ab

remove transformers dependency, added script for endpoint testing

Browse files
Files changed (3) hide show
  1. endpoint_tester.py +37 -0
  2. handler.py +1 -0
  3. requirements.txt +0 -1
endpoint_tester.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from typing import List
3
+ import requests as r
4
+ import base64
5
+ from PIL import Image
6
+ from io import BytesIO
7
+
8
+ ENDPOINT_URL = "https://j0thokqylseue22z.us-east-1.aws.endpoints.huggingface.cloud" # your endpoint url
9
+ HF_TOKEN = "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" # your huggingface token `hf_xxx`
10
+
11
+ # helper image utils
12
+ def encode_image(image_path):
13
+ with open(image_path, "rb") as i:
14
+ b64 = base64.b64encode(i.read())
15
+ return b64.decode("utf-8")
16
+
17
+ def predict(image):
18
+ image = encode_image(image)
19
+
20
+ # prepare sample payload
21
+ payload = {"image": image, "inputs": ""}
22
+ # headers
23
+ headers = {
24
+ "Authorization": f"Bearer {HF_TOKEN}",
25
+ "Content-Type": "application/json",
26
+ }
27
+
28
+ response = r.post(ENDPOINT_URL, headers=headers, json=payload)
29
+ if response.status_code != 200:
30
+ print(response.text)
31
+ raise Exception("Prediction failed")
32
+ return response.json()
33
+
34
+ prediction = predict(
35
+ image = "test.png"
36
+ )
37
+
handler.py CHANGED
@@ -79,6 +79,7 @@ class EndpointHandler():
79
  reverse_norm_std = torch.tensor([0.26862954, 0.26130258, 0.27577711])[:, None, None]
80
  image_tensor = image_tensor * reverse_norm_std + reverse_norm_mean
81
  pil_img = T.ToPILImage()(image_tensor)
 
82
  image_h = pil_img.height
83
  image_w = pil_img.width
84
  image = np.array(pil_img)[:, :, [2, 1, 0]]
 
79
  reverse_norm_std = torch.tensor([0.26862954, 0.26130258, 0.27577711])[:, None, None]
80
  image_tensor = image_tensor * reverse_norm_std + reverse_norm_mean
81
  pil_img = T.ToPILImage()(image_tensor)
82
+ a
83
  image_h = pil_img.height
84
  image_w = pil_img.width
85
  image = np.array(pil_img)[:, :, [2, 1, 0]]
requirements.txt CHANGED
@@ -1,2 +1 @@
1
  opencv-python
2
- transformers
 
1
  opencv-python