Search in sources :

Example 21 with LocationName

use of com.google.cloud.kms.v1.LocationName in project java-translate by googleapis.

the class GetSupportedLanguagesForTarget method getSupportedLanguagesForTarget.

// Listing supported languages with target language name
public static void getSupportedLanguagesForTarget(String projectId, String languageCode) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TranslationServiceClient client = TranslationServiceClient.create()) {
        // Supported Locations: `global`, [glossary location], or [model location]
        // Glossaries must be hosted in `us-central1`
        // Custom Models must use the same location as your model. (us-central1)
        LocationName parent = LocationName.of(projectId, "global");
        GetSupportedLanguagesRequest request = GetSupportedLanguagesRequest.newBuilder().setParent(parent.toString()).setDisplayLanguageCode(languageCode).build();
        SupportedLanguages response = client.getSupportedLanguages(request);
        // List language codes of supported languages
        for (SupportedLanguage language : response.getLanguagesList()) {
            System.out.printf("Language Code: %s\n", language.getLanguageCode());
            System.out.printf("Display Name: %s\n", language.getDisplayName());
        }
    }
}
Also used : GetSupportedLanguagesRequest(com.google.cloud.translate.v3.GetSupportedLanguagesRequest) SupportedLanguages(com.google.cloud.translate.v3.SupportedLanguages) TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) SupportedLanguage(com.google.cloud.translate.v3.SupportedLanguage) LocationName(com.google.cloud.translate.v3.LocationName)

Example 22 with LocationName

use of com.google.cloud.kms.v1.LocationName in project java-translate by googleapis.

the class TranslateTextWithGlossary method translateTextWithGlossary.

// Translates a given text using a glossary.
public static void translateTextWithGlossary(String projectId, String sourceLanguage, String targetLanguage, String text, String glossaryId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TranslationServiceClient client = TranslationServiceClient.create()) {
        // Supported Locations: `global`, [glossary location], or [model location]
        // Glossaries must be hosted in `us-central1`
        // Custom Models must use the same location as your model. (us-central1)
        String location = "us-central1";
        LocationName parent = LocationName.of(projectId, location);
        GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
        TranslateTextGlossaryConfig glossaryConfig = TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
        // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
        TranslateTextRequest request = TranslateTextRequest.newBuilder().setParent(parent.toString()).setMimeType("text/plain").setSourceLanguageCode(sourceLanguage).setTargetLanguageCode(targetLanguage).addContents(text).setGlossaryConfig(glossaryConfig).build();
        TranslateTextResponse response = client.translateText(request);
        // Display the translation for each input text provided
        for (Translation translation : response.getGlossaryTranslationsList()) {
            System.out.printf("Translated text: %s\n", translation.getTranslatedText());
        }
    }
}
Also used : Translation(com.google.cloud.translate.v3.Translation) TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) TranslateTextRequest(com.google.cloud.translate.v3.TranslateTextRequest) GlossaryName(com.google.cloud.translate.v3.GlossaryName) TranslateTextGlossaryConfig(com.google.cloud.translate.v3.TranslateTextGlossaryConfig) TranslateTextResponse(com.google.cloud.translate.v3.TranslateTextResponse) LocationName(com.google.cloud.translate.v3.LocationName)

Example 23 with LocationName

use of com.google.cloud.kms.v1.LocationName in project java-automl by googleapis.

the class VisionObjectDetectionCreateModel method createModel.

// Create a model
static void createModel(String projectId, String datasetId, String displayName) throws IOException, ExecutionException, InterruptedException {
    // 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");
        // Set model metadata.
        ImageObjectDetectionModelMetadata metadata = ImageObjectDetectionModelMetadata.newBuilder().build();
        Model model = Model.newBuilder().setDisplayName(displayName).setDatasetId(datasetId).setImageObjectDetectionModelMetadata(metadata).build();
        // Create a model with the model metadata in the region.
        OperationFuture<Model, OperationMetadata> future = client.createModelAsync(projectLocation, model);
        // OperationFuture.get() will block until the model is created, which may take several hours.
        // You can use OperationFuture.getInitialFuture to get a future representing the initial
        // response to the request, which contains information while the operation is in progress.
        System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName());
        System.out.println("Training started...");
    }
}
Also used : Model(com.google.cloud.automl.v1.Model) OperationMetadata(com.google.cloud.automl.v1.OperationMetadata) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient) LocationName(com.google.cloud.automl.v1.LocationName) ImageObjectDetectionModelMetadata(com.google.cloud.automl.v1.ImageObjectDetectionModelMetadata)

Example 24 with LocationName

use of com.google.cloud.kms.v1.LocationName 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 25 with LocationName

use of com.google.cloud.kms.v1.LocationName in project java-automl by googleapis.

the class ModelApi method createModel.

// [START automl_vision_create_model]
/**
 * Demonstrates using the AutoML client to create a model.
 *
 * @param projectId the Id of the project.
 * @param computeRegion the Region name.
 * @param dataSetId the Id of the dataset to which model is created.
 * @param modelName the Name of the model.
 * @param trainBudget the Budget for training the model.
 */
static void createModel(String projectId, String computeRegion, String dataSetId, String modelName, String trainBudget) {
    // Instantiates a client
    try (AutoMlClient client = AutoMlClient.create()) {
        // A resource that represents Google Cloud Platform location.
        LocationName projectLocation = LocationName.of(projectId, computeRegion);
        // Set model metadata.
        ImageClassificationModelMetadata imageClassificationModelMetadata = Long.valueOf(trainBudget) == 0 ? ImageClassificationModelMetadata.newBuilder().build() : ImageClassificationModelMetadata.newBuilder().setTrainBudget(Long.valueOf(trainBudget)).build();
        // Set model name and model metadata for the image dataset.
        Model myModel = Model.newBuilder().setDisplayName(modelName).setDatasetId(dataSetId).setImageClassificationModelMetadata(imageClassificationModelMetadata).build();
        // Create a model with the model metadata in the region.
        OperationFuture<Model, OperationMetadata> response = client.createModelAsync(projectLocation, myModel);
        System.out.println(String.format("Training operation name: %s", response.getInitialFuture().get().getName()));
        System.out.println("Training started...");
    } catch (IOException | ExecutionException | InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : ImageClassificationModelMetadata(com.google.cloud.automl.v1beta1.ImageClassificationModelMetadata) Model(com.google.cloud.automl.v1beta1.Model) IOException(java.io.IOException) OperationMetadata(com.google.cloud.automl.v1beta1.OperationMetadata) ExecutionException(java.util.concurrent.ExecutionException) AutoMlClient(com.google.cloud.automl.v1beta1.AutoMlClient) LocationName(com.google.cloud.automl.v1beta1.LocationName)

Aggregations

Test (org.junit.Test)34 LocationName (com.google.privacy.dlp.v2.LocationName)22 OrganizationLocationName (com.google.privacy.dlp.v2.OrganizationLocationName)22 AbstractMessage (com.google.protobuf.AbstractMessage)18 AutoMlClient (com.google.cloud.automl.v1.AutoMlClient)17 LocationName (com.google.cloud.automl.v1.LocationName)17 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)16 StatusRuntimeException (io.grpc.StatusRuntimeException)14 LocationName (com.google.cloud.translate.v3.LocationName)13 TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)13 OperationMetadata (com.google.cloud.automl.v1.OperationMetadata)12 AutoMlClient (com.google.cloud.automl.v1beta1.AutoMlClient)12 LocationName (com.google.cloud.automl.v1beta1.LocationName)12 LocationName (com.google.cloud.translate.v3beta1.LocationName)10 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)10 Model (com.google.cloud.automl.v1.Model)8 LocationName (com.google.cloud.bigquery.connection.v1.LocationName)8 Dataset (com.google.cloud.automl.v1.Dataset)7 Dataset (com.google.cloud.automl.v1beta1.Dataset)6 CloudTasksClient (com.google.cloud.tasks.v2.CloudTasksClient)5