diff --git a/automl/cloud-client/delete_model.py b/automl/cloud-client/delete_model.py new file mode 100644 index 00000000000..cc6e7546882 --- /dev/null +++ b/automl/cloud-client/delete_model.py @@ -0,0 +1,31 @@ +# Copyright 2019 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 + + # 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/cloud-client/delete_model_test.py b/automl/cloud-client/delete_model_test.py new file mode 100644 index 00000000000..4b68a647557 --- /dev/null +++ b/automl/cloud-client/delete_model_test.py @@ -0,0 +1,31 @@ +# Copyright 2019 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["GCLOUD_PROJECT"] + + +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/cloud-client/deploy_model.py b/automl/cloud-client/deploy_model.py new file mode 100644 index 00000000000..cc55cf1e312 --- /dev/null +++ b/automl/cloud-client/deploy_model.py @@ -0,0 +1,31 @@ +# Copyright 2019 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 deploy_model(project_id, model_id): + """Deploy a model.""" + # [START automl_deploy_model] + from google.cloud import 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.deploy_model(model_full_id) + + print("Model deployment finished. {}".format(response.result())) + # [END automl_deploy_model] diff --git a/automl/cloud-client/deploy_model_test.py b/automl/cloud-client/deploy_model_test.py new file mode 100644 index 00000000000..8a2026188b8 --- /dev/null +++ b/automl/cloud-client/deploy_model_test.py @@ -0,0 +1,35 @@ +# Copyright 2019 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 pytest + +import deploy_model + +PROJECT_ID = os.environ["GCLOUD_PROJECT"] +MODEL_ID = "TRL0000000000000000000" + + +@pytest.mark.slow +def test_deploy_model(capsys): + # As model deployment can take a long time, instead try to deploy a + # nonexistent model and confirm that the model was not found, but other + # elements of the request were valid. + try: + deploy_model.deploy_model(PROJECT_ID, MODEL_ID) + 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/cloud-client/get_model.py b/automl/cloud-client/get_model.py new file mode 100644 index 00000000000..b1ea5154f60 --- /dev/null +++ b/automl/cloud-client/get_model.py @@ -0,0 +1,44 @@ +# Copyright 2019 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] + from google.cloud import 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] diff --git a/automl/cloud-client/get_model_evaluation.py b/automl/cloud-client/get_model_evaluation.py new file mode 100644 index 00000000000..4a1a97a393e --- /dev/null +++ b/automl/cloud-client/get_model_evaluation.py @@ -0,0 +1,92 @@ +# Copyright 2019 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_language_entity_extraction_get_model_evaluation] + # [START automl_language_sentiment_analysis_get_model_evaluation] + # [START automl_language_text_classification_get_model_evaluation] + # [START automl_translate_get_model_evaluation] + # [START automl_vision_classification_get_model_evaluation] + # [START automl_vision_object_detection_get_model_evaluation] + from google.cloud import 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) + ) + # [END automl_language_sentiment_analysis_get_model_evaluation] + # [END automl_language_text_classification_get_model_evaluation] + # [END automl_translate_get_model_evaluation] + # [END automl_vision_classification_get_model_evaluation] + # [END automl_vision_object_detection_get_model_evaluation] + print( + "Entity extraction model evaluation metrics: {}".format( + response.text_extraction_evaluation_metrics + ) + ) + # [END automl_language_entity_extraction_get_model_evaluation] + + # [START automl_language_sentiment_analysis_get_model_evaluation] + print( + "Sentiment analysis model evaluation metrics: {}".format( + response.text_sentiment_evaluation_metrics + ) + ) + # [END automl_language_sentiment_analysis_get_model_evaluation] + + # [START automl_language_text_classification_get_model_evaluation] + # [START automl_vision_classification_get_model_evaluation] + print( + "Classification model evaluation metrics: {}".format( + response.classification_evaluation_metrics + ) + ) + # [END automl_language_text_classification_get_model_evaluation] + # [END automl_vision_classification_get_model_evaluation] + + # [START automl_translate_get_model_evaluation] + print( + "Translation model evaluation metrics: {}".format( + response.translation_evaluation_metrics + ) + ) + # [END automl_translate_get_model_evaluation] + + # [START automl_vision_object_detection_get_model_evaluation] + print( + "Object detection model evaluation metrics: {}".format( + response.image_object_detection_evaluation_metrics + ) + ) + # [END automl_vision_object_detection_get_model_evaluation] diff --git a/automl/cloud-client/get_model_evaluation_test.py b/automl/cloud-client/get_model_evaluation_test.py new file mode 100644 index 00000000000..252c860d6cb --- /dev/null +++ b/automl/cloud-client/get_model_evaluation_test.py @@ -0,0 +1,46 @@ +# Copyright 2019 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 pytest + +import get_model_evaluation + +PROJECT_ID = os.environ["GCLOUD_PROJECT"] +MODEL_ID = "TEN1499896588007374848" + + +@pytest.fixture(scope="function") +def get_evaluation_id(): + from google.cloud import automl + + client = automl.AutoMlClient() + model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) + evaluation = None + for e in client.list_model_evaluations(model_full_id, ""): + evaluation = e + break + model_evaluation_id = evaluation.name.split( + "{}/modelEvaluations/".format(MODEL_ID) + )[1].split("\n")[0] + yield model_evaluation_id + + +def test_get_model_evaluation(capsys, get_evaluation_id): + get_model_evaluation.get_model_evaluation( + PROJECT_ID, MODEL_ID, get_evaluation_id + ) + out, _ = capsys.readouterr() + assert "Model evaluation name: " in out diff --git a/automl/cloud-client/get_model_test.py b/automl/cloud-client/get_model_test.py new file mode 100644 index 00000000000..176161bf6fe --- /dev/null +++ b/automl/cloud-client/get_model_test.py @@ -0,0 +1,26 @@ +# Copyright 2019 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["GCLOUD_PROJECT"] +MODEL_ID = "TEN1499896588007374848" + + +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/cloud-client/list_model_evaluations.py b/automl/cloud-client/list_model_evaluations.py new file mode 100644 index 00000000000..3dcb7932a2f --- /dev/null +++ b/automl/cloud-client/list_model_evaluations.py @@ -0,0 +1,94 @@ +# Copyright 2019 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_model_evaluations(project_id, model_id): + """List model evaluations.""" + # [START automl_language_entity_extraction_list_model_evaluations] + # [START automl_language_sentiment_analysis_list_model_evaluations] + # [START automl_language_text_classification_list_model_evaluations] + # [START automl_translate_list_model_evaluations] + # [START automl_vision_classification_list_model_evaluations] + # [START automl_vision_object_detection_list_model_evaluations] + from google.cloud import 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) + + print("List of model evaluations:") + for evaluation in client.list_model_evaluations(model_full_id, ""): + print("Model evaluation name: {}".format(evaluation.name)) + print( + "Model annotation spec id: {}".format( + evaluation.annotation_spec_id + ) + ) + print("Create Time:") + print("\tseconds: {}".format(evaluation.create_time.seconds)) + print("\tnanos: {}".format(evaluation.create_time.nanos / 1e9)) + print( + "Evaluation example count: {}".format( + evaluation.evaluated_example_count + ) + ) + # [END automl_language_sentiment_analysis_list_model_evaluations] + # [END automl_language_text_classification_list_model_evaluations] + # [END automl_translate_list_model_evaluations] + # [END automl_vision_classification_list_model_evaluations] + # [END automl_vision_object_detection_list_model_evaluations] + print( + "Entity extraction model evaluation metrics: {}".format( + evaluation.text_extraction_evaluation_metrics + ) + ) + # [END automl_language_entity_extraction_list_model_evaluations] + + # [START automl_language_sentiment_analysis_list_model_evaluations] + print( + "Sentiment analysis model evaluation metrics: {}".format( + evaluation.text_sentiment_evaluation_metrics + ) + ) + # [END automl_language_sentiment_analysis_list_model_evaluations] + + # [START automl_language_text_classification_list_model_evaluations] + # [START automl_vision_classification_list_model_evaluations] + print( + "Classification model evaluation metrics: {}".format( + evaluation.classification_evaluation_metrics + ) + ) + # [END automl_language_text_classification_list_model_evaluations] + # [END automl_vision_classification_list_model_evaluations] + + # [START automl_translate_list_model_evaluations] + print( + "Translation model evaluation metrics: {}".format( + evaluation.translation_evaluation_metrics + ) + ) + # [END automl_translate_list_model_evaluations] + + # [START automl_vision_object_detection_list_model_evaluations] + print( + "Object detection model evaluation metrics: {}\n\n".format( + evaluation.image_object_detection_evaluation_metrics + ) + ) + # [END automl_vision_object_detection_list_model_evaluations] diff --git a/automl/cloud-client/list_model_evaluations_test.py b/automl/cloud-client/list_model_evaluations_test.py new file mode 100644 index 00000000000..2acad83ed1d --- /dev/null +++ b/automl/cloud-client/list_model_evaluations_test.py @@ -0,0 +1,27 @@ +# Copyright 2019 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_model_evaluations + + +PROJECT_ID = os.environ["GCLOUD_PROJECT"] +MODEL_ID = "TEN1499896588007374848" + + +def test_list_get_eval_model(capsys): + list_model_evaluations.list_model_evaluations(PROJECT_ID, MODEL_ID) + out, _ = capsys.readouterr() + assert "Model evaluation name: " in out diff --git a/automl/cloud-client/list_models.py b/automl/cloud-client/list_models.py new file mode 100644 index 00000000000..5c5dff6743f --- /dev/null +++ b/automl/cloud-client/list_models.py @@ -0,0 +1,47 @@ +# Copyright 2019 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] + from google.cloud import 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] diff --git a/automl/cloud-client/list_models_test.py b/automl/cloud-client/list_models_test.py new file mode 100644 index 00000000000..adb64190987 --- /dev/null +++ b/automl/cloud-client/list_models_test.py @@ -0,0 +1,25 @@ +# Copyright 2019 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["GCLOUD_PROJECT"] + + +def test_list_get_eval_model(capsys): + list_models.list_models(PROJECT_ID) + out, _ = capsys.readouterr() + assert "Model id: " in out diff --git a/automl/cloud-client/requirements.txt b/automl/cloud-client/requirements.txt new file mode 100644 index 00000000000..d64d7c33cbc --- /dev/null +++ b/automl/cloud-client/requirements.txt @@ -0,0 +1,2 @@ +google-cloud-automl==0.8.0 +google-cloud-storage==1.20.0 \ No newline at end of file diff --git a/automl/cloud-client/undeploy_model.py b/automl/cloud-client/undeploy_model.py new file mode 100644 index 00000000000..b737064dc5f --- /dev/null +++ b/automl/cloud-client/undeploy_model.py @@ -0,0 +1,31 @@ +# Copyright 2019 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 undeploy_model(project_id, model_id): + """Undeploy a model.""" + # [START automl_undeploy_model] + from google.cloud import 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.undeploy_model(model_full_id) + + print("Model undeployment finished. {}".format(response.result())) + # [END automl_undeploy_model] diff --git a/automl/cloud-client/undeploy_model_test.py b/automl/cloud-client/undeploy_model_test.py new file mode 100644 index 00000000000..caaa78f07fc --- /dev/null +++ b/automl/cloud-client/undeploy_model_test.py @@ -0,0 +1,35 @@ +# Copyright 2019 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 pytest + +import undeploy_model + +PROJECT_ID = os.environ["GCLOUD_PROJECT"] +MODEL_ID = "TRL0000000000000000000" + + +@pytest.mark.slow +def test_undeploy_model(capsys): + # As model undeployment can take a long time, instead try to deploy a + # nonexistent model and confirm that the model was not found, but other + # elements of the request were valid. + try: + undeploy_model.undeploy_model(PROJECT_ID, MODEL_ID) + out, _ = capsys.readouterr() + assert "The model does not exist" in out + except Exception as e: + assert "The model does not exist" in e.message