Search in sources :

Example 1 with ListModelsRequest

use of com.google.cloud.automl.v1beta1.ListModelsRequest in project java-automl by googleapis.

the class ModelApi method listModels.

// [START automl_translate_list_models]
/**
 * Demonstrates using the AutoML client to list all models.
 *
 * @param projectId the Id of the project.
 * @param computeRegion the Region name.
 * @param filter the filter expression.
 * @throws IOException on Input/Output errors.
 */
public static void listModels(String projectId, String computeRegion, String filter) throws IOException {
    // Instantiates a client
    try (AutoMlClient client = AutoMlClient.create()) {
        // A resource that represents Google Cloud Platform location.
        LocationName projectLocation = LocationName.of(projectId, computeRegion);
        // Create list models request.
        ListModelsRequest listModlesRequest = ListModelsRequest.newBuilder().setParent(projectLocation.toString()).setFilter(filter).build();
        // List all the models available in the region by applying filter.
        System.out.println("List of models:");
        for (Model model : client.listModels(listModlesRequest).iterateAll()) {
            // Display the model information.
            System.out.println(String.format("Model name: %s", model.getName()));
            System.out.println(String.format("Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1]));
            System.out.println(String.format("Model display name: %s", model.getDisplayName()));
            System.out.println("Model create time:");
            System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds()));
            System.out.println(String.format("\tnanos: %s", model.getCreateTime().getNanos()));
            System.out.println(String.format("Model deployment state: %s", model.getDeploymentState()));
        }
    }
}
Also used : Model(com.google.cloud.automl.v1.Model) ListModelsRequest(com.google.cloud.automl.v1.ListModelsRequest) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient) LocationName(com.google.cloud.automl.v1.LocationName)

Example 2 with ListModelsRequest

use of com.google.cloud.automl.v1beta1.ListModelsRequest in project java-automl by googleapis.

the class ListModels method listModels.

// List the models available in the specified location
static void listModels(String projectId) throws IOException {
    // 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");
        // Create list models request.
        ListModelsRequest listModelsRequest = ListModelsRequest.newBuilder().setParent(projectLocation.toString()).setFilter("").build();
        // List all the models available in the region by applying filter.
        System.out.println("List of models:");
        for (Model model : client.listModels(listModelsRequest).iterateAll()) {
            // 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());
        }
    }
}
Also used : Model(com.google.cloud.automl.v1.Model) ListModelsRequest(com.google.cloud.automl.v1.ListModelsRequest) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient) LocationName(com.google.cloud.automl.v1.LocationName)

Example 3 with ListModelsRequest

use of com.google.cloud.automl.v1beta1.ListModelsRequest in project java-automl by googleapis.

the class AutoMlClientTest method listModelsTest.

@Test
public void listModelsTest() throws Exception {
    Model responsesElement = Model.newBuilder().build();
    ListModelsResponse expectedResponse = ListModelsResponse.newBuilder().setNextPageToken("").addAllModel(Arrays.asList(responsesElement)).build();
    mockAutoMl.addResponse(expectedResponse);
    LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
    ListModelsPagedResponse pagedListResponse = client.listModels(parent);
    List<Model> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getModelList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockAutoMl.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListModelsRequest actualRequest = ((ListModelsRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListModelsPagedResponse(com.google.cloud.automl.v1beta1.AutoMlClient.ListModelsPagedResponse) Test(org.junit.Test)

Example 4 with ListModelsRequest

use of com.google.cloud.automl.v1beta1.ListModelsRequest in project java-automl by googleapis.

the class ListModels method listModels.

// List the models available in the specified location
static void listModels(String projectId) throws IOException {
    // 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");
        // Create list models request.
        ListModelsRequest listModelsRequest = ListModelsRequest.newBuilder().setParent(projectLocation.toString()).setFilter("").build();
        // List all the models available in the region by applying filter.
        System.out.println("List of models:");
        for (Model model : client.listModels(listModelsRequest).iterateAll()) {
            // 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());
        }
    }
}
Also used : Model(com.google.cloud.automl.v1beta1.Model) ListModelsRequest(com.google.cloud.automl.v1beta1.ListModelsRequest) AutoMlClient(com.google.cloud.automl.v1beta1.AutoMlClient) LocationName(com.google.cloud.automl.v1beta1.LocationName)

Example 5 with ListModelsRequest

use of com.google.cloud.automl.v1beta1.ListModelsRequest in project java-automl by googleapis.

the class AutoMlClientTest method listModelsTest2.

@Test
public void listModelsTest2() throws Exception {
    Model responsesElement = Model.newBuilder().build();
    ListModelsResponse expectedResponse = ListModelsResponse.newBuilder().setNextPageToken("").addAllModel(Arrays.asList(responsesElement)).build();
    mockAutoMl.addResponse(expectedResponse);
    String parent = "parent-995424086";
    ListModelsPagedResponse pagedListResponse = client.listModels(parent);
    List<Model> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getModelList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockAutoMl.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListModelsRequest actualRequest = ((ListModelsRequest) actualRequests.get(0));
    Assert.assertEquals(parent, actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListModelsPagedResponse(com.google.cloud.automl.v1beta1.AutoMlClient.ListModelsPagedResponse) Test(org.junit.Test)

Aggregations

AutoMlClient (com.google.cloud.automl.v1.AutoMlClient)2 ListModelsRequest (com.google.cloud.automl.v1.ListModelsRequest)2 LocationName (com.google.cloud.automl.v1.LocationName)2 Model (com.google.cloud.automl.v1.Model)2 ListModelsPagedResponse (com.google.cloud.automl.v1beta1.AutoMlClient.ListModelsPagedResponse)2 AbstractMessage (com.google.protobuf.AbstractMessage)2 Test (org.junit.Test)2 AutoMlClient (com.google.cloud.automl.v1beta1.AutoMlClient)1 ListModelsRequest (com.google.cloud.automl.v1beta1.ListModelsRequest)1 LocationName (com.google.cloud.automl.v1beta1.LocationName)1 Model (com.google.cloud.automl.v1beta1.Model)1