GeezerVison is showing github.com — fix: Support multiple instances in custom predict sample (#857) · ccubc/python-aiplatform@8cb4839
File treeExpand file treeCollapse file tree Open diff view settings samples/snippets/prediction_service
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: samples/snippets/prediction_service/predict_custom_trained_model_sample.py +10-4Lines changed: 10 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 13 | 13 | # limitations under the License. | 14 | 14 |
| 15 | 15 | # [START aiplatform_predict_custom_trained_model_sample] | 16 | | -from typing import Dict | | 16 | +from typing import Dict, List, Union | 17 | 17 |
| 18 | 18 | from google.cloud import aiplatform | 19 | 19 | from google.protobuf import json_format | | 23 | 23 | def predict_custom_trained_model_sample( | 24 | 24 | project: str, | 25 | 25 | endpoint_id: str, | 26 | | - instance_dict: Dict, | | 26 | + instances: Union[Dict, List[Dict]], | 27 | 27 | location: str = "us-central1", | 28 | 28 | api_endpoint: str = "us-central1-aiplatform.googleapis.com", | 29 | 29 | ): | | 30 | + """ | | 31 | + `instances` can be either single instance of type dict or a list | | 32 | + of instances. | | 33 | + """ | 30 | 34 | # The AI Platform services require regional API endpoints. | 31 | 35 | client_options = {"api_endpoint": api_endpoint} | 32 | 36 | # Initialize client that will be used to create and send requests. | 33 | 37 | # This client only needs to be created once, and can be reused for multiple requests. | 34 | 38 | client = aiplatform.gapic.PredictionServiceClient(client_options=client_options) | 35 | 39 | # The format of each instance should conform to the deployed model's prediction input schema. | 36 | | - instance = json_format.ParseDict(instance_dict, Value()) | 37 | | - instances = [instance] | | 40 | + instances = instances if type(instances) == list else [instances] | | 41 | + instances = [ | | 42 | + json_format.ParseDict(instance_dict, Value()) for instance_dict in instances | | 43 | + ] | 38 | 44 | parameters_dict = {} | 39 | 45 | parameters = json_format.ParseDict(parameters_dict, Value()) | 40 | 46 | endpoint = client.endpoint_path( | |
Collapse file Copy file name to clipboardExpand all lines: samples/snippets/prediction_service/predict_custom_trained_model_sample_test.py +12-1Lines changed: 12 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,9 +32,20 @@ def test_ucaip_generated_predict_custom_trained_model_sample(capsys): | 32 | 32 |
| 33 | 33 | instance_dict = {"image_bytes": {"b64": encoded_content}, "key": "0"} | 34 | 34 |
| | 35 | + # Single instance as a dict | 35 | 36 | predict_custom_trained_model_sample.predict_custom_trained_model_sample( | 36 | | - instance_dict=instance_dict, project=PROJECT_ID, endpoint_id=ENDPOINT_ID | | 37 | + instances=instance_dict, project=PROJECT_ID, endpoint_id=ENDPOINT_ID | | 38 | + ) | | 39 | + | | 40 | + # Multiple instances in a list | | 41 | + predict_custom_trained_model_sample.predict_custom_trained_model_sample( | | 42 | + instances=[instance_dict, instance_dict], | | 43 | + project=PROJECT_ID, | | 44 | + endpoint_id=ENDPOINT_ID, | 37 | 45 | ) | 38 | 46 |
| 39 | 47 | out, _ = capsys.readouterr() | 40 | 48 | assert "1.0" in out | | 49 | + | | 50 | + # Two sets of scores for multi-instance, one score for single instance | | 51 | + assert out.count("scores") == 3 |
|