From 0ed30a81e83891b35e346224a736e87f57dcb8fa Mon Sep 17 00:00:00 2001 From: Xin Yang Date: Mon, 21 Nov 2022 14:52:18 -0800 Subject: [PATCH] minor changes --- djl-serving/python-client/.gitignore | 1 + djl-serving/python-client/README.md | 27 +++++++++---------- .../djlserving_client_example1.py | 8 +++--- 3 files changed, 17 insertions(+), 19 deletions(-) create mode 100644 djl-serving/python-client/.gitignore diff --git a/djl-serving/python-client/.gitignore b/djl-serving/python-client/.gitignore new file mode 100644 index 00000000..2ed481fe --- /dev/null +++ b/djl-serving/python-client/.gitignore @@ -0,0 +1 @@ +kitten.jpg \ No newline at end of file diff --git a/djl-serving/python-client/README.md b/djl-serving/python-client/README.md index 729e85a1..4413a497 100644 --- a/djl-serving/python-client/README.md +++ b/djl-serving/python-client/README.md @@ -17,15 +17,16 @@ curl -O https://resources.djl.ai/images/kitten.jpg To register the model and make predictions: ```python -import requests - # Register model params = {'url': 'https://resources.djl.ai/demo/pytorch/traced_resnet18.zip', 'engine': 'PyTorch'} requests.post('http://localhost:8080/models', params=params) # Run inference url = 'http://localhost:8080/predictions/traced_resnet18' -res = requests.post(url, files={'data': open('kitten.jpg', 'rb')}) +headers = {'Content-Type': 'application/octet-stream'} +with open('kitten.jpg', 'rb') as f: + data = f.read() +res = requests.post(url, data=data, headers=headers) print(res.text) ``` @@ -76,8 +77,6 @@ This should return the following result: In the second example, we load a [HuggingFace Bert QA model](https://mlrepo.djl.ai/model/nlp/question_answer/ai/djl/huggingface/pytorch/deepset/bert-base-cased-squad2/0.0.1/bert-base-cased-squad2.zip) and make predictions. ```python -import requests - # Register model params = { 'url': 'https://mlrepo.djl.ai/model/nlp/question_answer/ai/djl/huggingface/pytorch/deepset/bert-base-cased-squad2' @@ -93,14 +92,13 @@ res = requests.post(url, json=data) print(res.text) ``` -The above code passes the data to the server using the Content-Type application/json. +The above code passes the data to the server using the `json` keyword. -Another way is to use the `json` keyword: +Another way is to use the explicit Content-Type application/json: ```python -url = 'http://localhost:8080/predictions/bert_base_cased_squad2' -data = {"question": "How is the weather", "paragraph": "The weather is nice, it is beautiful day"} -res = requests.post(url, json=data) +headers = {'Content-Type': 'application/json'} +res = requests.post(url, data=json.dumps(data), headers=headers) print(res.text) ``` @@ -138,14 +136,13 @@ res = requests.post(url, json=data) print(res.text) ``` -The above code passes the data to the server using the Content-Type application/json. +The above code passes the data to the server using the `json` keyword. -Another way is to use the `json` keyword: +Another way is to use the explicit Content-Type application/json: ```python -url = 'http://localhost:8080/predictions/bert_base_uncased' -data = {"data": "The man worked as a [MASK]."} -res = requests.post(url, json=data) +headers = {'Content-Type': 'application/json'} +res = requests.post(url, data=json.dumps(data), headers=headers) print(res.text) ``` diff --git a/djl-serving/python-client/djlserving_client_example1.py b/djl-serving/python-client/djlserving_client_example1.py index 0b4e3b39..713185fb 100755 --- a/djl-serving/python-client/djlserving_client_example1.py +++ b/djl-serving/python-client/djlserving_client_example1.py @@ -22,12 +22,12 @@ # Run inference url = 'http://localhost:8080/predictions/traced_resnet18' -res = requests.post(url, files={'data': open('kitten.jpg', 'rb')}) -print(res.text) - -# Another way to run inference with explicit content-type headers = {'Content-Type': 'application/octet-stream'} with open('kitten.jpg', 'rb') as f: data = f.read() res = requests.post(url, data=data, headers=headers) print(res.text) + +# Another way to run inference with multipart/form-data format +res = requests.post(url, files={'data': open('kitten.jpg', 'rb')}) +print(res.text)