-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Automl translation ga #1614
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
Automl translation ga #1614
Changes from 5 commits
f7b283b
da78636
a30231c
0d6bd4e
882d234
3609abf
e0c5971
6387c27
15f8778
1885149
142fc96
f8350d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Copyright 2019 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. | ||
| */ | ||
|
|
||
| package com.google.cloud.translate.automl; | ||
|
|
||
| // [START automl_translate_create_dataset] | ||
| import com.google.api.gax.longrunning.OperationFuture; | ||
| import com.google.cloud.automl.v1.AutoMlClient; | ||
| import com.google.cloud.automl.v1.Dataset; | ||
| import com.google.cloud.automl.v1.LocationName; | ||
| import com.google.cloud.automl.v1.OperationMetadata; | ||
| import com.google.cloud.automl.v1.TranslationDatasetMetadata; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| class CreateDataset { | ||
|
|
||
| // Create a dataset | ||
| static void createDataset(String projectId, String displayName) { | ||
| // String projectId = "YOUR_PROJECT_ID"; | ||
| // String displayName = "YOUR_DATASET_NAME"; | ||
|
|
||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. After completing all of your requests, call | ||
| // the "close" method on the client to safely clean up any remaining background resources. | ||
| try (AutoMlClient client = AutoMlClient.create()) { | ||
| // A resource that represents Google Cloud Platform location. | ||
| LocationName projectLocation = LocationName.of(projectId, "us-central1"); | ||
|
|
||
| // Specify the source and target language. | ||
| TranslationDatasetMetadata translationDatasetMetadata = | ||
| TranslationDatasetMetadata.newBuilder() | ||
| .setSourceLanguageCode("en") | ||
| .setTargetLanguageCode("ja") | ||
| .build(); | ||
| Dataset dataset = | ||
| Dataset.newBuilder() | ||
| .setDisplayName(displayName) | ||
| .setTranslationDatasetMetadata(translationDatasetMetadata) | ||
| .build(); | ||
| OperationFuture<Dataset, OperationMetadata> future = | ||
| client.createDatasetAsync(projectLocation, dataset); | ||
|
|
||
| Dataset createdDataset = future.get(); | ||
|
|
||
| // Display the dataset information. | ||
| System.out.format("Dataset name: %s\n", createdDataset.getName()); | ||
| System.out.format( | ||
| "Dataset id: %s\n", | ||
| createdDataset.getName().split("/")[createdDataset.getName().split("/").length - 1]); | ||
| System.out.format("Dataset display name: %s\n", createdDataset.getDisplayName()); | ||
| System.out.println("Translation dataset Metadata:"); | ||
| System.out.format( | ||
| "\tSource language code: %s\n", | ||
| createdDataset.getTranslationDatasetMetadata().getSourceLanguageCode()); | ||
| System.out.format( | ||
| "\tTarget language code: %s\n", | ||
| createdDataset.getTranslationDatasetMetadata().getTargetLanguageCode()); | ||
| System.out.println("Dataset create time:"); | ||
| System.out.format("\tseconds: %s\n", createdDataset.getCreateTime().getSeconds()); | ||
| System.out.format("\tnanos: %s\n", createdDataset.getCreateTime().getNanos()); | ||
| } catch (IOException | InterruptedException | ExecutionException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| // [END automl_translate_create_dataset] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright 2019 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. | ||
| */ | ||
|
|
||
| package com.google.cloud.translate.automl; | ||
|
|
||
| // [START automl_translate_create_model] | ||
| import com.google.api.gax.longrunning.OperationFuture; | ||
| import com.google.cloud.automl.v1.AutoMlClient; | ||
| import com.google.cloud.automl.v1.LocationName; | ||
| import com.google.cloud.automl.v1.Model; | ||
| import com.google.cloud.automl.v1.OperationMetadata; | ||
| import com.google.cloud.automl.v1.TranslationModelMetadata; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| class CreateModel { | ||
|
|
||
| // Create a model | ||
| static void createModel(String projectId, String datasetId, String displayName) { | ||
| // String projectId = "YOUR_PROJECT_ID"; | ||
| // String datasetId = "YOUR_DATASET_ID"; | ||
| // String displayName = "YOUR_DATASET_NAME"; | ||
|
|
||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. After completing all of your requests, call | ||
| // the "close" method on the client to safely clean up any remaining background resources. | ||
| try (AutoMlClient client = AutoMlClient.create()) { | ||
| // A resource that represents Google Cloud Platform location. | ||
| LocationName projectLocation = LocationName.of(projectId, "us-central1"); | ||
| // Leave model unset to use the default base model provided by Google | ||
| TranslationModelMetadata translationModelMetadata = | ||
| TranslationModelMetadata.newBuilder().build(); | ||
| Model model = | ||
| Model.newBuilder() | ||
| .setDisplayName(displayName) | ||
| .setDatasetId(datasetId) | ||
| .setTranslationModelMetadata(translationModelMetadata) | ||
| .build(); | ||
|
|
||
| // Create a model with the model metadata in the region. | ||
| OperationFuture<Model, OperationMetadata> future = | ||
| client.createModelAsync(projectLocation, model); | ||
| System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName()); | ||
| System.out.println("Training started..."); | ||
| } catch (IOException | InterruptedException | ExecutionException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| // [END automl_translate_create_model] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Copyright 2019 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. | ||
| */ | ||
|
|
||
| package com.google.cloud.translate.automl; | ||
|
|
||
| // [START automl_translate_delete_dataset] | ||
| import com.google.cloud.automl.v1.AutoMlClient; | ||
| import com.google.cloud.automl.v1.DatasetName; | ||
| import com.google.protobuf.Empty; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| class DeleteDataset { | ||
|
|
||
| // Delete a dataset | ||
| static void deleteDataset(String projectId, String datasetId) { | ||
| // String projectId = "YOUR_PROJECT_ID"; | ||
| // String datasetId = "YOUR_DATASET_ID"; | ||
|
|
||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. After completing all of your requests, call | ||
| // the "close" method on the client to safely clean up any remaining background resources. | ||
| try (AutoMlClient client = AutoMlClient.create()) { | ||
| // Get the full path of the dataset. | ||
| DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId); | ||
| Empty response = client.deleteDatasetAsync(datasetFullId).get(); | ||
| System.out.format("Dataset deleted. %s\n", response); | ||
| } catch (IOException | InterruptedException | ExecutionException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| // [END automl_translate_delete_dataset] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Copyright 2019 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. | ||
| */ | ||
|
|
||
| package com.google.cloud.translate.automl; | ||
|
|
||
| // [START automl_translate_delete_model] | ||
| import com.google.cloud.automl.v1.AutoMlClient; | ||
| import com.google.cloud.automl.v1.ModelName; | ||
| import com.google.protobuf.Empty; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| class DeleteModel { | ||
|
|
||
| // Get a model | ||
| static void deleteModel(String projectId, String modelId) { | ||
| // String projectId = "YOUR_PROJECT_ID"; | ||
| // String modelId = "YOUR_MODEL_ID"; | ||
|
|
||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. After completing all of your requests, call | ||
| // the "close" method on the client to safely clean up any remaining background resources. | ||
| try (AutoMlClient client = AutoMlClient.create()) { | ||
| // Get the full path of the model. | ||
| ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId); | ||
|
|
||
| // Delete a model. | ||
| Empty response = client.deleteModelAsync(modelFullId).get(); | ||
|
|
||
| System.out.println("Model deletion started..."); | ||
| System.out.println(String.format("Model deleted. %s", response)); | ||
| } catch (IOException | InterruptedException | ExecutionException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| // [END automl_translate_delete_model] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Copyright 2019 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. | ||
| */ | ||
|
|
||
| package com.google.cloud.translate.automl; | ||
|
|
||
| // [START automl_translate_export_dataset] | ||
| import com.google.cloud.automl.v1.AutoMlClient; | ||
| import com.google.cloud.automl.v1.DatasetName; | ||
| import com.google.cloud.automl.v1.GcsDestination; | ||
| import com.google.cloud.automl.v1.OutputConfig; | ||
| import com.google.protobuf.Empty; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| class ExportDataset { | ||
|
|
||
| // Export a dataset | ||
| static void exportDataset(String projectId, String datasetId, String gcsUri) { | ||
| // String projectId = "YOUR_PROJECT_ID"; | ||
| // String datasetId = "YOUR_DATASET_ID"; | ||
| // String gcsUri = "gs://BUCKET_ID/path_to_export/"; | ||
|
|
||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. After completing all of your requests, call | ||
| // the "close" method on the client to safely clean up any remaining background resources. | ||
| try (AutoMlClient client = AutoMlClient.create()) { | ||
| // Get the complete path of the dataset. | ||
| DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId); | ||
| GcsDestination gcsDestination = | ||
| GcsDestination.newBuilder().setOutputUriPrefix(gcsUri).build(); | ||
|
|
||
| // Export the dataset to the output URI. | ||
| OutputConfig outputConfig = | ||
| OutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); | ||
|
|
||
| System.out.println("Processing export..."); | ||
| Empty response = client.exportDataAsync(datasetFullId, outputConfig).get(); | ||
| System.out.format("Dataset exported. %s\n", response); | ||
| } catch (IOException | InterruptedException | ExecutionException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| // [END automl_translate_export_dataset] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Copyright 2019 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. | ||
| */ | ||
|
|
||
| package com.google.cloud.translate.automl; | ||
|
|
||
| // [START automl_translate_get_dataset] | ||
| import com.google.cloud.automl.v1.AutoMlClient; | ||
| import com.google.cloud.automl.v1.Dataset; | ||
| import com.google.cloud.automl.v1.DatasetName; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| class GetDataset { | ||
|
|
||
| // Get a dataset | ||
| static void getDataset(String projectId, String datasetId) { | ||
| // String projectId = "YOUR_PROJECT_ID"; | ||
| // String datasetId = "YOUR_DATASET_ID"; | ||
|
|
||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. After completing all of your requests, call | ||
| // the "close" method on the client to safely clean up any remaining background resources. | ||
| try (AutoMlClient client = AutoMlClient.create()) { | ||
| // Get the complete path of the dataset. | ||
| DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId); | ||
| Dataset dataset = client.getDataset(datasetFullId); | ||
|
|
||
| // Display the dataset information | ||
| System.out.format("Dataset name: %s\n", dataset.getName()); | ||
| System.out.format( | ||
| "Dataset id: %s\n", | ||
| dataset.getName().split("/")[dataset.getName().split("/").length - 1]); | ||
|
||
| System.out.format("Dataset display name: %s\n", dataset.getDisplayName()); | ||
| System.out.println("Translation dataset metadata:"); | ||
| System.out.format( | ||
| "\tSource language code: %s\n", | ||
| dataset.getTranslationDatasetMetadata().getSourceLanguageCode()); | ||
| System.out.format( | ||
| "\tTarget language code: %s\n", | ||
| dataset.getTranslationDatasetMetadata().getTargetLanguageCode()); | ||
| System.out.println("Dataset create time:"); | ||
| System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds()); | ||
| System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos()); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| // [END automl_translate_get_dataset] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, but could you clean this up a bit and teach something here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would something like this be good?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking more like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I will probably keep the comment in there too.