Skip to content

Commit

Permalink
✨ support npy format response
Browse files Browse the repository at this point in the history
  • Loading branch information
aisensiy committed Mar 20, 2020
1 parent 2a95ef9 commit 5ffeeac
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
resp = requests.post(
'http://localhost:8001/v1/predict',
headers={
'Accept': 'application/plain+json',
'Accept': 'application/npy+json',
'Content-Type': 'application/npy+msgpack',
},
data=msgpack.packb({
Expand Down
7 changes: 6 additions & 1 deletion src/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,12 @@ def parse_type(tp):
COMPATIBLE = (
('base64', 'json'),
('plain', 'json'),
('npy', 'json'),

('bytes', 'msgpack'),
('base64', 'msgpack'),
('plain', 'msgpack'),
('npy', 'msgpack'),
('npy', 'msgpack')
)

if (fmt, enc) not in COMPATIBLE:
Expand Down Expand Up @@ -218,6 +219,10 @@ def make_output(results):
for k, v in results.items():
if fmt == 'bytes':
rst[k] = v.tobytes()
elif fmt == 'npy':
bytesdata = io.BytesIO()
np.save(bytesdata, v, allow_pickle=False)
rst[k] = bytesdata.getvalue().decode('latin-1')
elif fmt == 'base64':
rst[k] = base64.b64encode(v.tobytes()).decode('utf-8')
elif fmt == 'plain':
Expand Down

0 comments on commit 5ffeeac

Please sign in to comment.