From ca3dba37aae5749b0d11213d74cfa496675e20c6 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Wed, 29 Jan 2020 09:56:54 -0700 Subject: [PATCH 1/2] automl: video beta move model samples from branch to master --- automl/beta/delete_model.py | 31 +++++++++++++++ automl/beta/delete_model_test.py | 31 +++++++++++++++ automl/beta/get_model.py | 44 +++++++++++++++++++++ automl/beta/get_model_evaluation.py | 49 ++++++++++++++++++++++++ automl/beta/get_model_evaluation_test.py | 44 +++++++++++++++++++++ automl/beta/get_model_test.py | 26 +++++++++++++ automl/beta/list_models.py | 47 +++++++++++++++++++++++ automl/beta/list_models_test.py | 25 ++++++++++++ 8 files changed, 297 insertions(+) create mode 100644 automl/beta/delete_model.py create mode 100644 automl/beta/delete_model_test.py create mode 100644 automl/beta/get_model.py create mode 100644 automl/beta/get_model_evaluation.py create mode 100644 automl/beta/get_model_evaluation_test.py create mode 100644 automl/beta/get_model_test.py create mode 100644 automl/beta/list_models.py create mode 100644 automl/beta/list_models_test.py diff --git a/automl/beta/delete_model.py b/automl/beta/delete_model.py new file mode 100644 index 00000000000..e0e93bd870e --- /dev/null +++ b/automl/beta/delete_model.py @@ -0,0 +1,31 @@ +# 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 +# +# http://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. + + +def delete_model(project_id, model_id): + """Delete a model.""" + # [START automl_delete_model] + from google.cloud import automl_v1beta1 as automl + + # TODO(developer): Uncomment and set the following variables + # project_id = "YOUR_PROJECT_ID" + # model_id = "YOUR_MODEL_ID" + + client = automl.AutoMlClient() + # Get the full path of the model. + model_full_id = client.model_path(project_id, "us-central1", model_id) + response = client.delete_model(model_full_id) + + print("Model deleted. {}".format(response.result())) + # [END automl_delete_model] diff --git a/automl/beta/delete_model_test.py b/automl/beta/delete_model_test.py new file mode 100644 index 00000000000..1d3548f3d5e --- /dev/null +++ b/automl/beta/delete_model_test.py @@ -0,0 +1,31 @@ +# 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 +# +# http://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. + +import os + +import delete_model + +PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] + + +def test_delete_model(capsys): + # As model creation can take many hours, instead try to delete a + # nonexistent model and confirm that the model was not found, but other + # elements of the request were valid. + try: + delete_model.delete_model(PROJECT_ID, "TRL0000000000000000000") + out, _ = capsys.readouterr() + assert "The model does not exist" in out + except Exception as e: + assert "The model does not exist" in e.message diff --git a/automl/beta/get_model.py b/automl/beta/get_model.py new file mode 100644 index 00000000000..834dac0c4c3 --- /dev/null +++ b/automl/beta/get_model.py @@ -0,0 +1,44 @@ +# 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 +# +# http://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. + + +def get_model(project_id, model_id): + """Get a model.""" + # [START automl_get_model_beta] + from google.cloud import automl_v1beta1 as automl + + # TODO(developer): Uncomment and set the following variables + # project_id = "YOUR_PROJECT_ID" + # model_id = "YOUR_MODEL_ID" + + client = automl.AutoMlClient() + # Get the full path of the model. + model_full_id = client.model_path(project_id, "us-central1", model_id) + model = client.get_model(model_full_id) + + # Retrieve deployment state. + if model.deployment_state == automl.enums.Model.DeploymentState.DEPLOYED: + deployment_state = "deployed" + else: + deployment_state = "undeployed" + + # Display the model information. + print("Model name: {}".format(model.name)) + print("Model id: {}".format(model.name.split("/")[-1])) + print("Model display name: {}".format(model.display_name)) + print("Model create time:") + print("\tseconds: {}".format(model.create_time.seconds)) + print("\tnanos: {}".format(model.create_time.nanos)) + print("Model deployment state: {}".format(deployment_state)) + # [END automl_get_model_beta] diff --git a/automl/beta/get_model_evaluation.py b/automl/beta/get_model_evaluation.py new file mode 100644 index 00000000000..147547c06d8 --- /dev/null +++ b/automl/beta/get_model_evaluation.py @@ -0,0 +1,49 @@ +# 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 +# +# http://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. + + +def get_model_evaluation(project_id, model_id, model_evaluation_id): + """Get model evaluation.""" + # [START automl_video_classification_get_model_evaluation_beta] + from google.cloud import automl_v1beta1 as automl + + # TODO(developer): Uncomment and set the following variables + # project_id = "YOUR_PROJECT_ID" + # model_id = "YOUR_MODEL_ID" + # model_evaluation_id = "YOUR_MODEL_EVALUATION_ID" + + client = automl.AutoMlClient() + # Get the full path of the model evaluation. + model_evaluation_full_id = client.model_evaluation_path( + project_id, "us-central1", model_id, model_evaluation_id + ) + + # Get complete detail of the model evaluation. + response = client.get_model_evaluation(model_evaluation_full_id) + + print("Model evaluation name: {}".format(response.name)) + print("Model annotation spec id: {}".format(response.annotation_spec_id)) + print("Create Time:") + print("\tseconds: {}".format(response.create_time.seconds)) + print("\tnanos: {}".format(response.create_time.nanos / 1e9)) + print( + "Evaluation example count: {}".format(response.evaluated_example_count) + ) + + print( + "Classification model evaluation metrics: {}".format( + response.classification_evaluation_metrics + ) + ) + # [END automl_video_classification_get_model_evaluation_beta] diff --git a/automl/beta/get_model_evaluation_test.py b/automl/beta/get_model_evaluation_test.py new file mode 100644 index 00000000000..5b2ecf3631d --- /dev/null +++ b/automl/beta/get_model_evaluation_test.py @@ -0,0 +1,44 @@ +# 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 +# +# http://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. + +import os + +from google.cloud import automl_v1beta1 as automl +import pytest + +import get_model_evaluation + +PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] +MODEL_ID = os.environ["ENTITY_EXTRACTION_MODEL_ID"] + + +@pytest.fixture(scope="function") +def model_evaluation_id(): + client = automl.AutoMlClient() + model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) + generator = client.list_model_evaluations(model_full_id, "").pages + page = next(generator) + evaluation = page.next() + model_evaluation_id = evaluation.name.split( + "{}/modelEvaluations/".format(MODEL_ID) + )[1].split("\n")[0] + yield model_evaluation_id + + +def test_get_model_evaluation(capsys, model_evaluation_id): + get_model_evaluation.get_model_evaluation( + PROJECT_ID, MODEL_ID, model_evaluation_id + ) + out, _ = capsys.readouterr() + assert "Model evaluation name: " in out diff --git a/automl/beta/get_model_test.py b/automl/beta/get_model_test.py new file mode 100644 index 00000000000..237ad6daaff --- /dev/null +++ b/automl/beta/get_model_test.py @@ -0,0 +1,26 @@ +# 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 +# +# http://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. + +import os + +import get_model + +PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] +MODEL_ID = os.environ["ENTITY_EXTRACTION_MODEL_ID"] + + +def test_get_model(capsys): + get_model.get_model(PROJECT_ID, MODEL_ID) + out, _ = capsys.readouterr() + assert "Model id: " in out diff --git a/automl/beta/list_models.py b/automl/beta/list_models.py new file mode 100644 index 00000000000..7e9c7e34280 --- /dev/null +++ b/automl/beta/list_models.py @@ -0,0 +1,47 @@ +# 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 +# +# http://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. + + +def list_models(project_id): + """List models.""" + # [START automl_list_models_beta] + from google.cloud import automl_v1beta1 as automl + + # TODO(developer): Uncomment and set the following variables + # project_id = "YOUR_PROJECT_ID" + + client = automl.AutoMlClient() + # A resource that represents Google Cloud Platform location. + project_location = client.location_path(project_id, "us-central1") + response = client.list_models(project_location, "") + + print("List of models:") + for model in response: + # Display the model information. + if ( + model.deployment_state + == automl.enums.Model.DeploymentState.DEPLOYED + ): + deployment_state = "deployed" + else: + deployment_state = "undeployed" + + print("Model name: {}".format(model.name)) + print("Model id: {}".format(model.name.split("/")[-1])) + print("Model display name: {}".format(model.display_name)) + print("Model create time:") + print("\tseconds: {}".format(model.create_time.seconds)) + print("\tnanos: {}".format(model.create_time.nanos)) + print("Model deployment state: {}".format(deployment_state)) + # [END automl_list_models_beta] diff --git a/automl/beta/list_models_test.py b/automl/beta/list_models_test.py new file mode 100644 index 00000000000..75f8c40a038 --- /dev/null +++ b/automl/beta/list_models_test.py @@ -0,0 +1,25 @@ +# 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 +# +# http://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. + +import os + +import list_models + +PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] + + +def test_list_models(capsys): + list_models.list_models(PROJECT_ID) + out, _ = capsys.readouterr() + assert "Model id: " in out From fc9fd4054884eb45789ce30ea9057e4e145ce959 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Wed, 29 Jan 2020 13:23:15 -0700 Subject: [PATCH 2/2] Fix region tag --- automl/beta/delete_model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/automl/beta/delete_model.py b/automl/beta/delete_model.py index e0e93bd870e..030a2900113 100644 --- a/automl/beta/delete_model.py +++ b/automl/beta/delete_model.py @@ -15,7 +15,7 @@ def delete_model(project_id, model_id): """Delete a model.""" - # [START automl_delete_model] + # [START automl_delete_model_beta] from google.cloud import automl_v1beta1 as automl # TODO(developer): Uncomment and set the following variables @@ -28,4 +28,4 @@ def delete_model(project_id, model_id): response = client.delete_model(model_full_id) print("Model deleted. {}".format(response.result())) - # [END automl_delete_model] + # [END automl_delete_model_beta]