Skip to content

Commit

Permalink
feat: Migrate sample and sample tests - Batch 1 of 5 (#11)
Browse files Browse the repository at this point in the history
* chore: release 0.2.0

* Init samples and sample tests - Batch 1

* Add pytest to nox requirements

* Add dependency on library itself for testing

* Address reviewer comments - 1 of 5

* Add sample test resources

* Revert Changelog to pre-release

* Revert Changelog to pre-release (part 2)

* Update tabular + GAPIC namespace (B1/5)

* Update tabular + GAPIC namespace (B1/5) (part 2)

* Sync to private repo (1/5)

* Address requested changes

* Add / assertions, timeout as a sample param

* Sync with private (1/5) and address comments

* Add / assert to cancel_training_pipeline_test

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 2, 2020
1 parent 3c713d5 commit c37c8ef
Show file tree
Hide file tree
Showing 28 changed files with 1,421 additions and 0 deletions.
Empty file added samples/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions samples/cancel_training_pipeline_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_cancel_training_pipeline_sample]
from google.cloud import aiplatform


def cancel_training_pipeline_sample(
project: str, training_pipeline_id: str, location: str = "us-central1"
):
client_options = {"api_endpoint": "us-central1-aiplatform.googleapis.com"}
# Initialize client that will be used to create and send requests.
# This client only needs to be created once, and can be reused for multiple requests.
client = aiplatform.gapic.PipelineServiceClient(client_options=client_options)
name = client.training_pipeline_path(
project=project, location=location, training_pipeline=training_pipeline_id
)
response = client.cancel_training_pipeline(name=name)
print("response:", response)


# [END aiplatform_cancel_training_pipeline_sample]
63 changes: 63 additions & 0 deletions samples/create_batch_prediction_job_video_classification_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_create_batch_prediction_job_video_classification_sample]
from google.cloud import aiplatform
from google.protobuf import json_format
from google.protobuf.struct_pb2 import Value


def create_batch_prediction_job_video_classification_sample(
project: str,
display_name: str,
model_name: str,
gcs_source_uri: str,
gcs_destination_output_uri_prefix: str,
location: str = "us-central1",
):
client_options = {"api_endpoint": "us-central1-aiplatform.googleapis.com"}
# Initialize client that will be used to create and send requests.
# This client only needs to be created once, and can be reused for multiple requests.
client = aiplatform.gapic.JobServiceClient(client_options=client_options)
model_parameters_dict = {
"confidenceThreshold": 0.5,
"maxPredictions": 10000,
"segmentClassification": True,
"shotClassification": True,
"oneSecIntervalClassification": True,
}
model_parameters = json_format.ParseDict(model_parameters_dict, Value())

batch_prediction_job = {
"display_name": display_name,
# Format: 'projects/{project}/locations/{location}/models/{model_id}'
"model": model_name,
"model_parameters": model_parameters,
"input_config": {
"instances_format": "jsonl",
"gcs_source": {"uris": [gcs_source_uri]},
},
"output_config": {
"predictions_format": "jsonl",
"gcs_destination": {"output_uri_prefix": gcs_destination_output_uri_prefix},
},
}
parent = f"projects/{project}/locations/{location}"
response = client.create_batch_prediction_job(
parent=parent, batch_prediction_job=batch_prediction_job
)
print("response:", response)


# [END aiplatform_create_batch_prediction_job_video_classification_sample]
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_create_batch_prediction_job_video_object_tracking_sample]
from google.cloud import aiplatform
from google.protobuf import json_format
from google.protobuf.struct_pb2 import Value


def create_batch_prediction_job_video_object_tracking_sample(
project: str,
display_name: str,
model_name: str,
gcs_source_uri: str,
gcs_destination_output_uri_prefix: str,
location: str = "us-central1",
):
client_options = {"api_endpoint": "us-central1-aiplatform.googleapis.com"}
# Initialize client that will be used to create and send requests.
# This client only needs to be created once, and can be reused for multiple requests.
client = aiplatform.gapic.JobServiceClient(client_options=client_options)
model_parameters_dict = {"confidenceThreshold": 0.0}
model_parameters = json_format.ParseDict(model_parameters_dict, Value())

batch_prediction_job = {
"display_name": display_name,
# Format: 'projects/{project}/locations/{location}/models/{model_id}'
"model": model_name,
"model_parameters": model_parameters,
"input_config": {
"instances_format": "jsonl",
"gcs_source": {"uris": [gcs_source_uri]},
},
"output_config": {
"predictions_format": "jsonl",
"gcs_destination": {"output_uri_prefix": gcs_destination_output_uri_prefix},
},
}
parent = f"projects/{project}/locations/{location}"
response = client.create_batch_prediction_job(
parent=parent, batch_prediction_job=batch_prediction_job
)
print("response:", response)


# [END aiplatform_create_batch_prediction_job_video_object_tracking_sample]
58 changes: 58 additions & 0 deletions samples/create_data_labeling_job_images_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_create_data_labeling_job_images_sample]
from google.cloud import aiplatform
from google.protobuf import json_format
from google.protobuf.struct_pb2 import Value


def create_data_labeling_job_images_sample(
project: str,
display_name: str,
dataset: str,
instruction_uri: str,
annotation_spec: str,
location: str = "us-central1",
):
client_options = {
"api_endpoint": "us-central1-autopush-aiplatform.sandbox.googleapis.com"
}
# Initialize client that will be used to create and send requests.
# This client only needs to be created once, and can be reused for multiple requests.
client = aiplatform.gapic.JobServiceClient(client_options=client_options)
inputs_dict = {"annotation_specs": [annotation_spec]}
inputs = json_format.ParseDict(inputs_dict, Value())

data_labeling_job = {
"display_name": display_name,
# Full resource name: projects/{project_id}/locations/{location}/datasets/{dataset_id}
"datasets": [dataset],
# labeler_count must be 1, 3, or 5
"labeler_count": 1,
"instruction_uri": instruction_uri,
"inputs_schema_uri": "gs://google-cloud-aiplatform/schema/datalabelingjob/inputs/image_classification.yaml",
"inputs": inputs,
"annotation_labels": {
"aiplatform.googleapis.com/annotation_set_name": "my_test_saved_query"
},
}
parent = f"projects/{project}/locations/{location}"
response = client.create_data_labeling_job(
parent=parent, data_labeling_job=data_labeling_job
)
print("response:", response)


# [END aiplatform_create_data_labeling_job_images_sample]
59 changes: 59 additions & 0 deletions samples/create_data_labeling_job_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_create_data_labeling_job_sample]
from google.cloud import aiplatform
from google.protobuf import json_format
from google.protobuf.struct_pb2 import Value


def create_data_labeling_job_sample(
project: str,
display_name: str,
dataset_name: str,
instruction_uri: str,
inputs_schema_uri: str,
annotation_spec: str,
location: str = "us-central1",
):
client_options = {
"api_endpoint": "us-central1-autopush-aiplatform.sandbox.googleapis.com"
}
# Initialize client that will be used to create and send requests.
# This client only needs to be created once, and can be reused for multiple requests.
client = aiplatform.gapic.JobServiceClient(client_options=client_options)
inputs_dict = {"annotation_specs": [annotation_spec]}
inputs = json_format.ParseDict(inputs_dict, Value())

data_labeling_job = {
"display_name": display_name,
# Full resource name: projects/{project_id}/locations/{location}/datasets/{dataset_id}
"datasets": [dataset_name],
# labeler_count must be 1, 3, or 5
"labeler_count": 1,
"instruction_uri": instruction_uri,
"inputs_schema_uri": inputs_schema_uri,
"inputs": inputs,
"annotation_labels": {
"aiplatform.googleapis.com/annotation_set_name": "my_test_saved_query"
},
}
parent = f"projects/{project}/locations/{location}"
response = client.create_data_labeling_job(
parent=parent, data_labeling_job=data_labeling_job
)
print("response:", response)


# [END aiplatform_create_data_labeling_job_sample]
58 changes: 58 additions & 0 deletions samples/create_data_labeling_job_video_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_create_data_labeling_job_video_sample]
from google.cloud import aiplatform
from google.protobuf import json_format
from google.protobuf.struct_pb2 import Value


def create_data_labeling_job_video_sample(
project: str,
display_name: str,
dataset: str,
instruction_uri: str,
annotation_spec: str,
location: str = "us-central1",
):
client_options = {
"api_endpoint": "us-central1-autopush-aiplatform.sandbox.googleapis.com"
}
# Initialize client that will be used to create and send requests.
# This client only needs to be created once, and can be reused for multiple requests.
client = aiplatform.gapic.JobServiceClient(client_options=client_options)
inputs_dict = {"annotation_specs": [annotation_spec]}
inputs = json_format.ParseDict(inputs_dict, Value())

data_labeling_job = {
"display_name": display_name,
# Full resource name: projects/{project_id}/locations/{location}/datasets/{dataset_id}
"datasets": [dataset],
# labeler_count must be 1, 3, or 5
"labeler_count": 1,
"instruction_uri": instruction_uri,
"inputs_schema_uri": "gs://google-cloud-aiplatform/schema/datalabelingjob/inputs/video_classification.yaml",
"inputs": inputs,
"annotation_labels": {
"aiplatform.googleapis.com/annotation_set_name": "my_test_saved_query"
},
}
parent = f"projects/{project}/locations/{location}"
response = client.create_data_labeling_job(
parent=parent, data_labeling_job=data_labeling_job
)
print("response:", response)


# [END aiplatform_create_data_labeling_job_video_sample]
37 changes: 37 additions & 0 deletions samples/create_dataset_image_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START aiplatform_create_dataset_image_sample]
from google.cloud import aiplatform


def create_dataset_image_sample(
project: str, display_name: str, location: str = "us-central1", timeout: int = 300
):
client_options = {"api_endpoint": "us-central1-aiplatform.googleapis.com"}
# Initialize client that will be used to create and send requests.
# This client only needs to be created once, and can be reused for multiple requests.
client = aiplatform.gapic.DatasetServiceClient(client_options=client_options)
dataset = {
"display_name": display_name,
"metadata_schema_uri": "gs://google-cloud-aiplatform/schema/dataset/metadata/image_1.0.0.yaml",
}
parent = f"projects/{project}/locations/{location}"
response = client.create_dataset(parent=parent, dataset=dataset)
print("Long running operation:", response.operation.name)
create_dataset_response = response.result(timeout=timeout)
print("create_dataset_response:", create_dataset_response)


# [END aiplatform_create_dataset_image_sample]

0 comments on commit c37c8ef

Please sign in to comment.