-
Notifications
You must be signed in to change notification settings - Fork 6.7k
automl: add base model samples for automl ga #2609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aa30bc7
automl: add base model samples for automl ga
nnegrey cae70ac
Add tests for each file, provide a unique model for each python versi…
nnegrey 28008ff
Fix version check
nnegrey 8410b9f
Update tests and format
nnegrey a2ef554
Update deploy_model_test.py
nnegrey ae0d992
Merge branch 'master' into automl-ga-base-model-samples
nnegrey 49667a8
Update license headers, ensure double quotes is used everywhere, leav…
nnegrey cec993b
Merge branch 'master' into automl-ga-base-model-samples
nnegrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
automl: add base model samples for automl ga
- Loading branch information
commit aa30bc7dbe63ef15da6423469f7f8251104afc7e
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # 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(u'Model deleted. {}'.format(response.result())) | ||
| # [END automl_delete_model] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # 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(u'Model deployment finished. {}'.format(response.result())) | ||
| # [END automl_deploy_model] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright 2018 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 = 'TEN5112482778553778176' | ||
|
|
||
|
|
||
| @pytest.fixture(scope="function") | ||
| def verify_model_state(): | ||
| from google.cloud import automl | ||
| client = automl.AutoMlClient() | ||
| model_full_id = client.model_path(PROJECT_ID, 'us-central1', MODEL_ID) | ||
|
|
||
| model = client.get_model(model_full_id) | ||
| if model.deployment_state == automl.enums.Model.DeploymentState.DEPLOYED: | ||
| # Undeploy model if it is deployed | ||
| response = client.undeploy_model(model_full_id) | ||
| response.result() | ||
|
|
||
|
|
||
| @pytest.mark.slow | ||
| def test_deploy_undeploy_model(capsys, verify_model_state): | ||
| verify_model_state | ||
| deploy_model.deploy_model(PROJECT_ID, MODEL_ID) | ||
| out, _ = capsys.readouterr() | ||
| assert 'Model deployment finished.' in out | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # 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(u'Model name: {}'.format(model.name)) | ||
| print(u'Model id: {}'.format(model.name.split('/')[-1])) | ||
| print(u'Model display name: {}'.format(model.display_name)) | ||
| print(u'Model create time:') | ||
| print(u'\tseconds: {}'.format(model.create_time.seconds)) | ||
| print(u'\tnanos: {}'.format(model.create_time.nanos)) | ||
| print(u'Model deployment state: {}'.format(deployment_state)) | ||
| # [END automl_get_model] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # 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' | ||
nnegrey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # 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(u'Model evaluation name: {}'.format(response.name)) | ||
| print(u'Model annotation spec id: {}'.format(response.annotation_spec_id)) | ||
| print('Create Time:') | ||
| print(u'\tseconds: {}'.format(response.create_time.seconds)) | ||
| print(u'\tnanos: {}'.format(response.create_time.nanos / 1e9)) | ||
| print(u'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] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # 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(u'Model evaluation name: {}'.format(evaluation.name)) | ||
| print( | ||
| u'Model annotation spec id: {}'.format( | ||
| evaluation.annotation_spec_id)) | ||
| print(u'Create Time:') | ||
| print(u'\tseconds: {}'.format(evaluation.create_time.seconds)) | ||
| print(u'\tnanos: {}'.format(evaluation.create_time.nanos / 1e9)) | ||
| print(u'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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # 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(u'Model name: {}'.format(model.name)) | ||
| print(u'Model id: {}'.format(model.name.split('/')[-1])) | ||
| print(u'Model display name: {}'.format(model.display_name)) | ||
| print(u'Model create time:') | ||
| print(u'\tseconds: {}'.format(model.create_time.seconds)) | ||
| print(u'\tnanos: {}'.format(model.create_time.nanos)) | ||
| print(u'Model deployment state: {}'.format(deployment_state)) | ||
| # [END automl_list_models] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.