-
Notifications
You must be signed in to change notification settings - Fork 3
/
serving.py
66 lines (50 loc) · 1.92 KB
/
serving.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import json
import cv2
import requests
import base64
import numpy as np
def main():
# Load an image from file
im = cv2.imread("data/01_raw/imageset/img1.jpg")
im2 = cv2.imread("data/01_raw/imageset/img3.jpg")
# Base64 encode it
# (you can also load the raw png data from file using open instead of opencv)
encoded = base64.b64encode(cv2.imencode(".jpg", im)[1].tobytes())
json_data = {
"signature_name": "serving_bytes",
"instances": [{
"age": 24,
"bp": 85,
"gender": "Male",
"zip": 12345,
"text_input_for_bert": "This is a test sentence.",
"input_bytes": {"b64": encoded.decode("utf-8")}
}]
}
_json_data = {
"signature_name": "serving_default",
"instances": [{
"age": 64,
"bp": 121,
"gender": "Female",
"zip": 12345,
"text_input_for_bert": "This is a test sentence.",
"input_1": np.asarray(im2).tolist()
}]
}
# Wrap it in json (tf-serving compatible with instances and b64)
data = json.dumps(json_data)
# Standard port for tf-serving rest interface is 8501
# Request Format: http://domain:[tf-serving-port]/v1/models/[model_name]:predict
resp = requests.post("http://localhost:8501/v1/models/train:predict", data)
# Load base 64 encoded image string from response
print("Response with b64: {}".format(resp.json()))
# Wrap it in json (tf-serving compatible with instances and b64)
data = json.dumps(_json_data)
# Standard port for tf-serving rest interface is 8501
# Request Format: http://domain:[tf-serving-port]/v1/models/[model_name]:predict
resp = requests.post("http://localhost:8501/v1/models/train:predict", data)
# Load base 64 encoded image string from response
print("Response with Array: {}".format(resp.json()))
if __name__ == "__main__":
main()