-
Notifications
You must be signed in to change notification settings - Fork 6.5k
automl: add natural language sentiment analysis ga samples #2677
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ac3100b
automl: add natural language sentiment analysis ga samples
nnegrey 94805ed
Merge branch 'master' into automl-ga-sentiment-analysis
nnegrey d1d6ab8
Add links to documentation
nnegrey 5ee488b
Merge branch 'master' into automl-ga-sentiment-analysis
nnegrey 11aeff8
Update tests to use centralized project
nnegrey 5feeeeb
Fix environment variable, make translate test less flaky
nnegrey 00e0544
Merge branch 'master' into automl-ga-sentiment-analysis
nnegrey fab8249
Merge branch 'master' into automl-ga-sentiment-analysis
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
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
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
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
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
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
50 changes: 50 additions & 0 deletions
50
automl/cloud-client/language_sentiment_analysis_create_dataset.py
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,50 @@ | ||
# 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 create_dataset(project_id, display_name): | ||
"""Create a dataset.""" | ||
# [START automl_language_sentiment_analysis_create_dataset] | ||
from google.cloud import automl | ||
|
||
# TODO(developer): Uncomment and set the following variables | ||
# project_id = "YOUR_PROJECT_ID" | ||
# display_name = "YOUR_DATASET_NAME" | ||
|
||
client = automl.AutoMlClient() | ||
|
||
# A resource that represents Google Cloud Platform location. | ||
project_location = client.location_path(project_id, "us-central1") | ||
|
||
# Each dataset requires a sentiment score with a defined sentiment_max | ||
# value, for more information on TextSentimentDatasetMetadata, see: | ||
# https://cloud.google.com/natural-language/automl/docs/prepare#sentiment-analysis | ||
# https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#textsentimentdatasetmetadata | ||
metadata = automl.types.TextSentimentDatasetMetadata( | ||
sentiment_max=4 | ||
) # Possible max sentiment score: 1-10 | ||
nnegrey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
dataset = automl.types.Dataset( | ||
display_name=display_name, text_sentiment_dataset_metadata=metadata | ||
) | ||
|
||
# Create a dataset with the dataset metadata in the region. | ||
response = client.create_dataset(project_location, dataset) | ||
|
||
created_dataset = response.result() | ||
|
||
# Display the dataset information | ||
print("Dataset name: {}".format(created_dataset.name)) | ||
print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) | ||
# [END automl_language_sentiment_analysis_create_dataset] |
41 changes: 41 additions & 0 deletions
41
automl/cloud-client/language_sentiment_analysis_create_dataset_test.py
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,41 @@ | ||
# 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 datetime | ||
import os | ||
|
||
from google.cloud import automl | ||
|
||
import language_sentiment_analysis_create_dataset | ||
|
||
|
||
PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] | ||
|
||
|
||
def test_sentiment_analysis_create_dataset(capsys): | ||
dataset_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") | ||
language_sentiment_analysis_create_dataset.create_dataset( | ||
PROJECT_ID, dataset_name | ||
) | ||
out, _ = capsys.readouterr() | ||
assert "Dataset id: " in out | ||
|
||
# Delete the created dataset | ||
dataset_id = out.splitlines()[1].split()[2] | ||
client = automl.AutoMlClient() | ||
dataset_full_id = client.dataset_path( | ||
PROJECT_ID, "us-central1", dataset_id | ||
) | ||
response = client.delete_dataset(dataset_full_id) | ||
response.result() |
43 changes: 43 additions & 0 deletions
43
automl/cloud-client/language_sentiment_analysis_create_model.py
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,43 @@ | ||
# 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 create_model(project_id, dataset_id, display_name): | ||
"""Create a model.""" | ||
# [START automl_language_sentiment_analysis_create_model] | ||
from google.cloud import automl | ||
|
||
# TODO(developer): Uncomment and set the following variables | ||
# project_id = "YOUR_PROJECT_ID" | ||
# dataset_id = "YOUR_DATASET_ID" | ||
# display_name = "YOUR_MODEL_NAME" | ||
|
||
client = automl.AutoMlClient() | ||
|
||
# A resource that represents Google Cloud Platform location. | ||
project_location = client.location_path(project_id, "us-central1") | ||
# Leave model unset to use the default base model provided by Google | ||
metadata = automl.types.TextSentimentModelMetadata() | ||
model = automl.types.Model( | ||
display_name=display_name, | ||
dataset_id=dataset_id, | ||
text_sentiment_model_metadata=metadata, | ||
) | ||
|
||
# Create a model with the model metadata in the region. | ||
response = client.create_model(project_location, model) | ||
|
||
print("Training operation name: {}".format(response.operation.name)) | ||
print("Training started...") | ||
# [END automl_language_sentiment_analysis_create_model] |
37 changes: 37 additions & 0 deletions
37
automl/cloud-client/language_sentiment_analysis_create_model_test.py
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,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 | ||
# | ||
# 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 | ||
import pytest | ||
|
||
import language_sentiment_analysis_create_model | ||
|
||
PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] | ||
DATASET_ID = os.environ["SENTIMENT_ANALYSIS_DATASET_ID"] | ||
|
||
|
||
@pytest.mark.slow | ||
def test_sentiment_analysis_create_model(capsys): | ||
language_sentiment_analysis_create_model.create_model( | ||
PROJECT_ID, DATASET_ID, "object_test_create_model" | ||
) | ||
out, _ = capsys.readouterr() | ||
assert "Training started" in out | ||
|
||
# Cancel the operation | ||
operation_id = out.split("Training operation name: ")[1].split("\n")[0] | ||
client = automl.AutoMlClient() | ||
client.transport._operations_client.cancel_operation(operation_id) |
51 changes: 51 additions & 0 deletions
51
automl/cloud-client/language_sentiment_analysis_predict.py
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,51 @@ | ||
# 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 predict(project_id, model_id, content): | ||
"""Predict.""" | ||
# [START automl_language_sentiment_analysis_predict] | ||
from google.cloud import automl | ||
|
||
# TODO(developer): Uncomment and set the following variables | ||
# project_id = "YOUR_PROJECT_ID" | ||
# model_id = "YOUR_MODEL_ID" | ||
# content = "text to predict" | ||
|
||
prediction_client = automl.PredictionServiceClient() | ||
|
||
# Get the full path of the model. | ||
model_full_id = prediction_client.model_path( | ||
project_id, "us-central1", model_id | ||
) | ||
|
||
# Supported mime_types: 'text/plain', 'text/html' | ||
# https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#textsnippet | ||
text_snippet = automl.types.TextSnippet( | ||
content=content, mime_type="text/plain" | ||
) | ||
payload = automl.types.ExamplePayload(text_snippet=text_snippet) | ||
|
||
response = prediction_client.predict(model_full_id, payload) | ||
|
||
for annotation_payload in response.payload: | ||
print( | ||
"Predicted class name: {}".format(annotation_payload.display_name) | ||
) | ||
print( | ||
"Predicted sentiment score: {}".format( | ||
annotation_payload.text_sentiment.sentiment | ||
) | ||
) | ||
# [END automl_language_sentiment_analysis_predict] |
43 changes: 43 additions & 0 deletions
43
automl/cloud-client/language_sentiment_analysis_predict_test.py
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,43 @@ | ||
# 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 | ||
import pytest | ||
|
||
import language_sentiment_analysis_predict | ||
|
||
PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] | ||
MODEL_ID = os.environ["SENTIMENT_ANALYSIS_MODEL_ID"] | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def verify_model_state(): | ||
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.UNDEPLOYED: | ||
# Deploy model if it is not deployed | ||
response = client.deploy_model(model_full_id) | ||
response.result() | ||
|
||
|
||
def test_predict(capsys, verify_model_state): | ||
verify_model_state | ||
text = "Hopefully this Claritin kicks in soon" | ||
language_sentiment_analysis_predict.predict(PROJECT_ID, MODEL_ID, text) | ||
out, _ = capsys.readouterr() | ||
assert "Predicted sentiment score: " 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
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
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
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
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
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
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
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
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.