Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
xyang16 committed Nov 21, 2022
1 parent 32f7512 commit 0ed30a8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
1 change: 1 addition & 0 deletions djl-serving/python-client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kitten.jpg
27 changes: 12 additions & 15 deletions djl-serving/python-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

Expand Down Expand Up @@ -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'
Expand All @@ -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)
```

Expand Down Expand Up @@ -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)
```

Expand Down
8 changes: 4 additions & 4 deletions djl-serving/python-client/djlserving_client_example1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 0ed30a8

Please sign in to comment.