@@ -164,12 +164,19 @@ Model deployment endpoints can be invoked with the oci sdk. This example invokes
164
164
165
165
>>> import requests
166
166
>>> import oci
167
- >>> import ads
167
+ >>> from ads.common.auth import default_signer
168
+ >>> import cloudpickle
169
+ >>> import PIL.Image
168
170
>>> import cloudpickle
169
171
>>> headers = {"Content-Type": "application/octet-stream"}
170
172
>>> endpoint = huggingface_pipeline_model.model_deployment.url + "/predict"
171
173
172
- >>> preds = requests.post(endpoint, data=image_bytes, auth=ads.common.auth.default_signer()['signer'], headers=headers).json()
174
+ ## download the image
175
+ image_url = "https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png"
176
+ image = PIL.Image.open(requests.get(image_link, stream=True).raw)
177
+ image_bytes = cloudpickle.dumps(image)
178
+
179
+ >>> preds = requests.post(endpoint, data=image_bytes, auth=default_signer()['signer'], headers=headers).json()
173
180
>>> print([{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds['prediction']])
174
181
[{'score': 0.9879, 'label': 'LABEL_184'},
175
182
{'score': 0.9973, 'label': 'snow'},
@@ -182,9 +189,11 @@ Example
182
189
.. code-block :: python3
183
190
184
191
from transformers import pipeline
192
+ from ads.model import HuggingFacePipelineModel
193
+
185
194
import tempfile
186
195
import PIL.Image
187
- import ads
196
+ from ads.common.auth import default_signer
188
197
import requests
189
198
import cloudpickle
190
199
@@ -205,10 +214,10 @@ Example
205
214
206
215
# Autogenerate score.py, serialized model, runtime.yaml
207
216
conda_pack_path = "oci://bucket@namespace/path/to/conda/pack"
208
- python_version = "3.x"
217
+ python_version = "3.x" # Remember to update 3.x with your actual python version, e.g. 3.8
209
218
zero_shot_image_classification_model.prepare(inference_conda_env=conda_pack_path, inference_python_version = python_version, force_overwrite=True)
210
219
211
- ## Test data
220
+ ## Convert payload to bytes
212
221
data = {"images": image, "candidate_labels": ["animals", "humans", "landscape"]}
213
222
body = cloudpickle.dumps(data) # convert image to bytes
214
223
@@ -231,7 +240,7 @@ Example
231
240
zero_shot_image_classification_model.predict(body)
232
241
233
242
### Invoke the model by sending bytes
234
- auth = ads.common.auth. default_signer()['signer']
243
+ auth = default_signer()['signer']
235
244
endpoint = zero_shot_image_classification_model.model_deployment.url + "/predict"
236
245
headers = {"Content-Type": "application/octet-stream"}
237
246
requests.post(endpoint, data=body, auth=auth, headers=headers).json()
0 commit comments