use of com.google.cloud.automl.v1.ModelName in project java-automl by googleapis.
the class TablesPredict method predict.
static void predict(String projectId, String modelId, List<Value> values) throws IOException {
// 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);
Row row = Row.newBuilder().addAllValues(values).build();
ExamplePayload payload = ExamplePayload.newBuilder().setRow(row).build();
// Feature importance gives you visibility into how the features in a specific prediction
// request informed the resulting prediction. For more info, see:
// https://cloud.google.com/automl-tables/docs/features#local
PredictRequest request = PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).putParams("feature_importance", "true").build();
PredictResponse response = client.predict(request);
System.out.println("Prediction results:");
for (AnnotationPayload annotationPayload : response.getPayloadList()) {
TablesAnnotation tablesAnnotation = annotationPayload.getTables();
System.out.format("Classification label: %s%n", tablesAnnotation.getValue().getStringValue());
System.out.format("Classification score: %.3f%n", tablesAnnotation.getScore());
// Get features of top importance
tablesAnnotation.getTablesModelColumnInfoList().forEach(info -> System.out.format("\tColumn: %s - Importance: %.2f%n", info.getColumnDisplayName(), info.getFeatureImportance()));
}
}
}
use of com.google.cloud.automl.v1.ModelName in project java-automl by googleapis.
the class TranslatePredict method predict.
static void predict(String projectId, String modelId, String filePath) throws IOException {
// 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);
String content = new String(Files.readAllBytes(Paths.get(filePath)));
TextSnippet textSnippet = TextSnippet.newBuilder().setContent(content).build();
ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build();
PredictRequest predictRequest = PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).build();
PredictResponse response = client.predict(predictRequest);
TextSnippet translatedContent = response.getPayload(0).getTranslation().getTranslatedContent();
System.out.format("Translated Content: %s\n", translatedContent.getContent());
}
}
use of com.google.cloud.automl.v1.ModelName in project java-automl by googleapis.
the class VisionClassificationDeployModelNodeCount method visionClassificationDeployModelNodeCount.
// Deploy a model for prediction with a specified node count (can be used to redeploy a model)
static void visionClassificationDeployModelNodeCount(String projectId, String modelId) throws IOException, ExecutionException, InterruptedException {
// 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);
ImageClassificationModelDeploymentMetadata metadata = ImageClassificationModelDeploymentMetadata.newBuilder().setNodeCount(2).build();
DeployModelRequest request = DeployModelRequest.newBuilder().setName(modelFullId.toString()).setImageClassificationModelDeploymentMetadata(metadata).build();
OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request);
future.get();
System.out.println("Model deployment finished");
}
}
use of com.google.cloud.automl.v1.ModelName in project java-automl by googleapis.
the class DeleteModel method deleteModel.
// Delete a model
static void deleteModel(String projectId, String modelId) throws IOException, ExecutionException, InterruptedException {
// 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));
}
}
use of com.google.cloud.automl.v1.ModelName in project java-automl by googleapis.
the class GetModel method getModel.
// Get a model
static void getModel(String projectId, String modelId) throws IOException {
// 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);
Model model = client.getModel(modelFullId);
// Display the model information.
System.out.format("Model name: %s\n", model.getName());
// To get the model id, you have to parse it out of the `name` field. As models Ids are
// required for other methods.
// Name Format: `projects/{project_id}/locations/{location_id}/models/{model_id}`
String[] names = model.getName().split("/");
String retrievedModelId = names[names.length - 1];
System.out.format("Model id: %s\n", retrievedModelId);
System.out.format("Model display name: %s\n", model.getDisplayName());
System.out.println("Model create time:");
System.out.format("\tseconds: %s\n", model.getCreateTime().getSeconds());
System.out.format("\tnanos: %s\n", model.getCreateTime().getNanos());
System.out.format("Model deployment state: %s\n", model.getDeploymentState());
}
}
Aggregations