Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
chore: update translation samples
Browse files Browse the repository at this point in the history
  • Loading branch information
busunkim96 committed Sep 15, 2020
1 parent abe263a commit af21576
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion samples/beta/cancel_operation.py
Expand Up @@ -31,7 +31,7 @@ def sample_cancel_operation(project, operation_id):

client = automl_v1beta1.AutoMlClient()

operations_client = client.transport._operations_client
operations_client = client._transport.operations_client

# project = '[Google Cloud Project ID]'
# operation_id = '[Operation ID]'
Expand Down
27 changes: 11 additions & 16 deletions samples/snippets/automl_translation_dataset.py
Expand Up @@ -40,7 +40,7 @@ def create_dataset(project_id, compute_region, dataset_name, source, target):
client = automl.AutoMlClient()

# A resource that represents Google Cloud Platform location.
project_location = client.location_path(project_id, compute_region)
project_location = f"projects/{project_id}/locations/{compute_region}"

# Specify the source and target language.
dataset_metadata = {
Expand All @@ -54,7 +54,7 @@ def create_dataset(project_id, compute_region, dataset_name, source, target):
}

# Create a dataset with the dataset metadata in the region.
dataset = client.create_dataset(project_location, my_dataset)
dataset = client.create_dataset(parent=project_location, dataset=my_dataset)

# Display the dataset information
print("Dataset name: {}".format(dataset.name))
Expand All @@ -71,9 +71,7 @@ def create_dataset(project_id, compute_region, dataset_name, source, target):
dataset.translation_dataset_metadata.target_language_code
)
)
print("Dataset create time:")
print("\tseconds: {}".format(dataset.create_time.seconds))
print("\tnanos: {}".format(dataset.create_time.nanos))
print("Dataset create time: {}".format(dataset.create_time))

# [END automl_translate_create_dataset]

Expand All @@ -91,10 +89,11 @@ def list_datasets(project_id, compute_region, filter_):
client = automl.AutoMlClient()

# A resource that represents Google Cloud Platform location.
project_location = client.location_path(project_id, compute_region)
project_location = f"projects/{project_id}/locations/{compute_region}"

# List all the datasets available in the region by applying filter.
response = client.list_datasets(project_location, filter_)
request = automl.ListDatasetsRequest(parent=project_location, filter=filter_)
response = client.list_datasets(request=request)

print("List of datasets:")
for dataset in response:
Expand All @@ -113,9 +112,7 @@ def list_datasets(project_id, compute_region, filter_):
dataset.translation_dataset_metadata.target_language_code
)
)
print("Dataset create time:")
print("\tseconds: {}".format(dataset.create_time.seconds))
print("\tnanos: {}".format(dataset.create_time.nanos))
print("Dataset create time: {}".format(dataset.create_time))

# [END automl_translate_list_datasets]

Expand All @@ -138,7 +135,7 @@ def get_dataset(project_id, compute_region, dataset_id):
)

# Get complete detail of the dataset.
dataset = client.get_dataset(dataset_full_id)
dataset = client.get_dataset(name=dataset_full_id)

# Display the dataset information
print("Dataset name: {}".format(dataset.name))
Expand All @@ -155,9 +152,7 @@ def get_dataset(project_id, compute_region, dataset_id):
dataset.translation_dataset_metadata.target_language_code
)
)
print("Dataset create time:")
print("\tseconds: {}".format(dataset.create_time.seconds))
print("\tnanos: {}".format(dataset.create_time.nanos))
print("Dataset create time: {}".format(dataset.create_time))

# [END automl_translate_get_dataset]

Expand Down Expand Up @@ -185,7 +180,7 @@ def import_data(project_id, compute_region, dataset_id, path):
input_config = {"gcs_source": {"input_uris": input_uris}}

# Import data from the input URI
response = client.import_data(dataset_full_id, input_config)
response = client.import_data(name=dataset_full_id, input_config=input_config)

print("Processing import...")
# synchronous check of operation status
Expand All @@ -212,7 +207,7 @@ def delete_dataset(project_id, compute_region, dataset_id):
)

# Delete a dataset.
response = client.delete_dataset(dataset_full_id)
response = client.delete_dataset(name=dataset_full_id)

# synchronous check of operation status
print("Dataset deleted. {}".format(response.result()))
Expand Down
35 changes: 17 additions & 18 deletions samples/snippets/automl_translation_model.py
Expand Up @@ -49,7 +49,7 @@ def create_model(project_id, compute_region, dataset_id, model_name):
}

# Create a model with the model metadata in the region.
response = client.create_model(project_location, my_model)
response = client.create_model(parent=project_location, model=my_model)

print("Training operation name: {}".format(response.operation.name))
print("Training started...")
Expand All @@ -66,30 +66,28 @@ def list_models(project_id, compute_region, filter_):
# filter_ = 'DATASET_ID_HERE'

from google.cloud import automl_v1beta1 as automl
from google.cloud.automl_v1beta1 import enums

client = automl.AutoMlClient()

# A resource that represents Google Cloud Platform location.
project_location = client.location_path(project_id, compute_region)
project_location = f"projects/{project_id}/locations/{compute_region}"

# List all the models available in the region by applying filter.
response = client.list_models(project_location, filter_)
request = automl.ListDatasetsRequest(parent=project_location, filter=filter_)
response = client.list_datasets(request=request)

print("List of models:")
for model in response:
# Display the model information.
if model.deployment_state == enums.Model.DeploymentState.DEPLOYED:
if model.deployment_state == automl.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 create time: {}".format(model.create_time))
print("Model deployment state: {}".format(deployment_state))

# [END automl_translate_list_models]
Expand All @@ -104,18 +102,17 @@ def get_model(project_id, compute_region, model_id):
# model_id = 'MODEL_ID_HERE'

from google.cloud import automl_v1beta1 as automl
from google.cloud.automl_v1beta1 import enums

client = automl.AutoMlClient()

# Get the full path of the model.
model_full_id = client.model_path(project_id, compute_region, model_id)

# Get complete detail of the model.
model = client.get_model(model_full_id)
model = client.get_model(name=model_full_id)

# Retrieve deployment state.
if model.deployment_state == enums.Model.DeploymentState.DEPLOYED:
if model.deployment_state == automl.Model.DeploymentState.DEPLOYED:
deployment_state = "deployed"
else:
deployment_state = "undeployed"
Expand All @@ -124,9 +121,7 @@ def get_model(project_id, compute_region, model_id):
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 create time: {}".format(model.create_time))
print("Model deployment state: {}".format(deployment_state))

# [END automl_translate_get_model]
Expand All @@ -149,7 +144,11 @@ def list_model_evaluations(project_id, compute_region, model_id, filter_):
model_full_id = client.model_path(project_id, compute_region, model_id)

print("List of model evaluations:")
for element in client.list_model_evaluations(model_full_id, filter_):
request = automl.ListModelEvaluationsRequest(
parent=model_full_id,
filter=filter_
)
for element in client.list_model_evaluations(request=request):
print(element)

# [END automl_translate_list_model_evaluations]
Expand All @@ -176,7 +175,7 @@ def get_model_evaluation(
)

# Get complete detail of the model evaluation.
response = client.get_model_evaluation(model_evaluation_full_id)
response = client.get_model_evaluation(name=model_evaluation_full_id)

print(response)

Expand All @@ -199,7 +198,7 @@ def delete_model(project_id, compute_region, model_id):
model_full_id = client.model_path(project_id, compute_region, model_id)

# Delete a model.
response = client.delete_model(model_full_id)
response = client.delete_model(name=model_full_id)

# synchronous check of operation status.
print("Model deleted. {}".format(response.result()))
Expand All @@ -219,7 +218,7 @@ def get_operation_status(operation_full_id):
client = automl.AutoMlClient()

# Get the latest state of a long-running operation.
response = client.transport._operations_client.get_operation(
response = client._transport.operations_client.get_operation(
operation_full_id
)

Expand Down
8 changes: 7 additions & 1 deletion samples/snippets/automl_translation_predict.py
Expand Up @@ -56,7 +56,13 @@ def predict(project_id, compute_region, model_id, file_path):
# params is additional domain-specific parameters.
params = {}

response = prediction_client.predict(model_full_id, payload, params)
request = automl.PredictRequest(
name=model_full_id,
payload=payload,
params=params
)

response = prediction_client.predict(request=request)
translated_content = response.payload[0].translation.translated_content

print(u"Translated content: {}".format(translated_content.content))
Expand Down
4 changes: 2 additions & 2 deletions samples/snippets/model_test.py
Expand Up @@ -31,13 +31,13 @@ def test_model_create_status_delete(capsys):
# create model
client = automl.AutoMlClient()
model_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
project_location = client.location_path(project_id, compute_region)
project_location = f"projects/{project_id}/locations/{compute_region}"
my_model = {
"display_name": model_name,
"dataset_id": "3876092572857648864",
"translation_model_metadata": {"base_model": ""},
}
response = client.create_model(project_location, my_model)
response = client.create_model(parent=project_location, model=my_model)
operation_name = response.operation.name
assert operation_name

Expand Down

0 comments on commit af21576

Please sign in to comment.