-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Natural Language GA Samples * Add Undeploy Model * Add Vision Classification Samples * Add Vision Object Detection Samples * Lint * Update samples based on feedback: remove unhelpful descriptions, add wrapper to env variables in tests * Add comment to explain why the create model call doesn't wait for compeletion * Update comment wording * Update method comment, only check env var for tests before class, remove javadoc comments
- Loading branch information
Showing
51 changed files
with
3,506 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
57 changes: 57 additions & 0 deletions
57
automl/cloud-client/src/main/java/com/example/automl/DeployModel.java
This file contains 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,57 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.example.automl; | ||
|
||
// [START automl_deploy_model] | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.automl.v1.AutoMlClient; | ||
import com.google.cloud.automl.v1.DeployModelRequest; | ||
import com.google.cloud.automl.v1.ModelName; | ||
import com.google.cloud.automl.v1.OperationMetadata; | ||
import com.google.protobuf.Empty; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class DeployModel { | ||
|
||
static void deployModel() throws IOException, ExecutionException, InterruptedException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "YOUR_PROJECT_ID"; | ||
String modelId = "YOUR_MODEL_ID"; | ||
deployModel(projectId, modelId); | ||
} | ||
|
||
// Deploy a model for prediction | ||
static void deployModel(String projectId, String modelId) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// 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); | ||
DeployModelRequest request = | ||
DeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); | ||
OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request); | ||
|
||
future.get(); | ||
System.out.println("Model deployment finished"); | ||
} | ||
} | ||
} | ||
// [END automl_deploy_model] |
This file contains 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 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
76 changes: 76 additions & 0 deletions
76
automl/cloud-client/src/main/java/com/example/automl/LanguageBatchPredict.java
This file contains 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 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.example.automl; | ||
|
||
// [START automl_language_batch_predict] | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.automl.v1.BatchPredictInputConfig; | ||
import com.google.cloud.automl.v1.BatchPredictOutputConfig; | ||
import com.google.cloud.automl.v1.BatchPredictRequest; | ||
import com.google.cloud.automl.v1.BatchPredictResult; | ||
import com.google.cloud.automl.v1.GcsDestination; | ||
import com.google.cloud.automl.v1.GcsSource; | ||
import com.google.cloud.automl.v1.ModelName; | ||
import com.google.cloud.automl.v1.OperationMetadata; | ||
import com.google.cloud.automl.v1.PredictionServiceClient; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class LanguageBatchPredict { | ||
|
||
static void batchPredict() throws IOException, ExecutionException, InterruptedException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "YOUR_PROJECT_ID"; | ||
String modelId = "YOUR_MODEL_ID"; | ||
String inputUri = "gs://YOUR_BUCKET_ID/path_to_your_input_file.jsonl"; | ||
String outputUri = "gs://YOUR_BUCKET_ID/path_to_save_results/"; | ||
batchPredict(projectId, modelId, inputUri, outputUri); | ||
} | ||
|
||
static void batchPredict(String projectId, String modelId, String inputUri, String outputUri) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// 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 (PredictionServiceClient client = PredictionServiceClient.create()) { | ||
// Get the full path of the model. | ||
ModelName name = ModelName.of(projectId, "us-central1", modelId); | ||
GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build(); | ||
BatchPredictInputConfig inputConfig = | ||
BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build(); | ||
GcsDestination gcsDestination = | ||
GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build(); | ||
BatchPredictOutputConfig outputConfig = | ||
BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); | ||
BatchPredictRequest request = | ||
BatchPredictRequest.newBuilder() | ||
.setName(name.toString()) | ||
.setInputConfig(inputConfig) | ||
.setOutputConfig(outputConfig) | ||
.build(); | ||
|
||
OperationFuture<BatchPredictResult, OperationMetadata> future = | ||
client.batchPredictAsync(request); | ||
|
||
System.out.println("Waiting for operation to complete..."); | ||
BatchPredictResult response = future.get(); | ||
System.out.println("Batch Prediction results saved to specified Cloud Storage bucket."); | ||
} | ||
} | ||
} | ||
// [END automl_language_batch_predict] |
71 changes: 71 additions & 0 deletions
71
.../cloud-client/src/main/java/com/example/automl/LanguageEntityExtractionCreateDataset.java
This file contains 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,71 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.example.automl; | ||
|
||
// [START automl_language_entity_extraction_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.TextExtractionDatasetMetadata; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class LanguageEntityExtractionCreateDataset { | ||
|
||
static void createDataset() throws IOException, ExecutionException, InterruptedException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "YOUR_PROJECT_ID"; | ||
String displayName = "YOUR_DATASET_NAME"; | ||
createDataset(projectId, displayName); | ||
} | ||
|
||
// Create a dataset | ||
static void createDataset(String projectId, String displayName) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// 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"); | ||
|
||
TextExtractionDatasetMetadata metadata = TextExtractionDatasetMetadata.newBuilder().build(); | ||
Dataset dataset = | ||
Dataset.newBuilder() | ||
.setDisplayName(displayName) | ||
.setTextExtractionDatasetMetadata(metadata) | ||
.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()); | ||
// To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are | ||
// required for other methods. | ||
// Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}` | ||
String[] names = createdDataset.getName().split("/"); | ||
String datasetId = names[names.length - 1]; | ||
System.out.format("Dataset id: %s\n", datasetId); | ||
} | ||
} | ||
} | ||
// [END automl_language_entity_extraction_create_dataset] |
Oops, something went wrong.