Search in sources :

Example 36 with ModelName

use of com.google.cloud.aiplatform.v1.ModelName in project java-automl by googleapis.

the class UndeployModel method undeployModel.

// Undeploy a model from prediction
static void undeployModel(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);
        UndeployModelRequest request = UndeployModelRequest.newBuilder().setName(modelFullId.toString()).build();
        OperationFuture<Empty, OperationMetadata> future = client.undeployModelAsync(request);
        future.get();
        System.out.println("Model undeployment finished");
    }
}
Also used : Empty(com.google.protobuf.Empty) ModelName(com.google.cloud.automl.v1.ModelName) UndeployModelRequest(com.google.cloud.automl.v1.UndeployModelRequest) OperationMetadata(com.google.cloud.automl.v1.OperationMetadata) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient)

Example 37 with ModelName

use of com.google.cloud.aiplatform.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");
    }
}
Also used : DeployModelRequest(com.google.cloud.automl.v1.DeployModelRequest) Empty(com.google.protobuf.Empty) ModelName(com.google.cloud.automl.v1.ModelName) OperationMetadata(com.google.cloud.automl.v1.OperationMetadata) ImageClassificationModelDeploymentMetadata(com.google.cloud.automl.v1.ImageClassificationModelDeploymentMetadata) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient)

Example 38 with ModelName

use of com.google.cloud.aiplatform.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()));
        }
    }
}
Also used : ModelName(com.google.cloud.automl.v1beta1.ModelName) TablesAnnotation(com.google.cloud.automl.v1beta1.TablesAnnotation) PredictResponse(com.google.cloud.automl.v1beta1.PredictResponse) ExamplePayload(com.google.cloud.automl.v1beta1.ExamplePayload) Row(com.google.cloud.automl.v1beta1.Row) PredictRequest(com.google.cloud.automl.v1beta1.PredictRequest) PredictionServiceClient(com.google.cloud.automl.v1beta1.PredictionServiceClient) AnnotationPayload(com.google.cloud.automl.v1beta1.AnnotationPayload)

Example 39 with ModelName

use of com.google.cloud.aiplatform.v1.ModelName in project java-automl by googleapis.

the class LanguageTextClassificationPredict method predict.

static void predict(String projectId, String modelId, String content) 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);
        // For available mime types, see:
        // https://cloud.google.com/automl/docs/reference/rest/v1/projects.locations.models/predict#textsnippet
        TextSnippet textSnippet = TextSnippet.newBuilder().setContent(content).setMimeType(// Types: text/plain, text/html
        "text/plain").build();
        ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build();
        PredictRequest predictRequest = PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).build();
        PredictResponse response = client.predict(predictRequest);
        for (AnnotationPayload annotationPayload : response.getPayloadList()) {
            System.out.format("Predicted class name: %s\n", annotationPayload.getDisplayName());
            System.out.format("Predicted sentiment score: %.2f\n\n", annotationPayload.getClassification().getScore());
        }
    }
}
Also used : ModelName(com.google.cloud.automl.v1.ModelName) TextSnippet(com.google.cloud.automl.v1.TextSnippet) PredictResponse(com.google.cloud.automl.v1.PredictResponse) ExamplePayload(com.google.cloud.automl.v1.ExamplePayload) PredictRequest(com.google.cloud.automl.v1.PredictRequest) PredictionServiceClient(com.google.cloud.automl.v1.PredictionServiceClient) AnnotationPayload(com.google.cloud.automl.v1.AnnotationPayload)

Example 40 with ModelName

use of com.google.cloud.aiplatform.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));
    }
}
Also used : Empty(com.google.protobuf.Empty) ModelName(com.google.cloud.automl.v1.ModelName) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient)

Aggregations

ModelName (com.google.cloud.automl.v1.ModelName)24 AutoMlClient (com.google.cloud.automl.v1.AutoMlClient)16 ModelName (com.google.cloud.automl.v1beta1.ModelName)15 Empty (com.google.protobuf.Empty)14 AutoMlClient (com.google.cloud.automl.v1beta1.AutoMlClient)11 GcsDestination (com.google.cloud.aiplatform.v1.GcsDestination)10 DeployModelRequest (com.google.cloud.automl.v1.DeployModelRequest)10 ModelName (com.google.cloud.aiplatform.v1.ModelName)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 PrintStream (java.io.PrintStream)9 Before (org.junit.Before)9 BatchPredictionJob (com.google.cloud.aiplatform.v1.BatchPredictionJob)8 JobServiceClient (com.google.cloud.aiplatform.v1.JobServiceClient)8 JobServiceSettings (com.google.cloud.aiplatform.v1.JobServiceSettings)8 LocationName (com.google.cloud.aiplatform.v1.LocationName)8 Model (com.google.cloud.automl.v1.Model)8 PredictionServiceClient (com.google.cloud.automl.v1.PredictionServiceClient)8 GcsSource (com.google.cloud.aiplatform.v1.GcsSource)7 ExamplePayload (com.google.cloud.automl.v1.ExamplePayload)7 PredictResponse (com.google.cloud.automl.v1.PredictResponse)7