Search in sources :

Example 31 with GcsSource

use of com.google.cloud.aiplatform.v1.GcsSource in project java-translate by googleapis.

the class BatchTranslateText method batchTranslateText.

// Batch translate text
public static void batchTranslateText(String projectId, String sourceLanguage, String targetLanguage, String inputUri, String outputUri) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TranslationServiceClient client = TranslationServiceClient.create()) {
        // Supported Locations: `us-central1`
        LocationName parent = LocationName.of(projectId, "us-central1");
        GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
        // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
        InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
        GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
        OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
        BatchTranslateTextRequest request = BatchTranslateTextRequest.newBuilder().setParent(parent.toString()).setSourceLanguageCode(sourceLanguage).addTargetLanguageCodes(targetLanguage).addInputConfigs(inputConfig).setOutputConfig(outputConfig).build();
        OperationFuture<BatchTranslateResponse, BatchTranslateMetadata> future = client.batchTranslateTextAsync(request);
        System.out.println("Waiting for operation to complete...");
        // random number between 300 - 450 (maximum allowed seconds)
        long randomNumber = ThreadLocalRandom.current().nextInt(450, 600);
        BatchTranslateResponse response = future.get(randomNumber, TimeUnit.SECONDS);
        System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
        System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
    }
}
Also used : BatchTranslateMetadata(com.google.cloud.translate.v3.BatchTranslateMetadata) TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) GcsSource(com.google.cloud.translate.v3.GcsSource) OutputConfig(com.google.cloud.translate.v3.OutputConfig) BatchTranslateTextRequest(com.google.cloud.translate.v3.BatchTranslateTextRequest) InputConfig(com.google.cloud.translate.v3.InputConfig) GcsDestination(com.google.cloud.translate.v3.GcsDestination) BatchTranslateResponse(com.google.cloud.translate.v3.BatchTranslateResponse) LocationName(com.google.cloud.translate.v3.LocationName)

Example 32 with GcsSource

use of com.google.cloud.aiplatform.v1.GcsSource in project google-cloud-java by GoogleCloudPlatform.

the class TranslateSnippetsBeta method createGlossary.

// [END translate_batch_translate_text_beta]
/**
 * Creates a glossary.
 *
 * @param projectId - Id of the project.
 * @param location - location name.
 * @param name - Glossary name.
 * @param gcsUri - Google Cloud Storage URI where glossary is stored in csv format.
 */
// [START translate_create_glossary_beta]
static Glossary createGlossary(String projectId, String location, String name, String gcsUri) {
    try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
        LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
        LanguageCodesSet languageCodesSet = LanguageCodesSet.newBuilder().addLanguageCodes("en").addLanguageCodes("es").build();
        GcsSource gcsSource = GcsSource.newBuilder().setInputUri(gcsUri).build();
        GlossaryInputConfig glossaryInputConfig = GlossaryInputConfig.newBuilder().setGcsSource(gcsSource).build();
        GlossaryName glossaryName = GlossaryName.newBuilder().setProject(projectId).setLocation(location).setGlossary(name).build();
        Glossary glossary = Glossary.newBuilder().setLanguageCodesSet(languageCodesSet).setInputConfig(glossaryInputConfig).setName(glossaryName.toString()).build();
        CreateGlossaryRequest request = CreateGlossaryRequest.newBuilder().setParent(locationName.toString()).setGlossary(glossary).build();
        // Call the API
        Glossary response = translationServiceClient.createGlossaryAsync(request).get(300, TimeUnit.SECONDS);
        System.out.format("Created: %s\n", response.getName());
        System.out.format("Input Uri: %s\n", response.getInputConfig().getGcsSource());
        return response;
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : LanguageCodesSet(com.google.cloud.translate.v3beta1.Glossary.LanguageCodesSet) TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) GcsSource(com.google.cloud.translate.v3beta1.GcsSource) CreateGlossaryRequest(com.google.cloud.translate.v3beta1.CreateGlossaryRequest) GlossaryInputConfig(com.google.cloud.translate.v3beta1.GlossaryInputConfig) Glossary(com.google.cloud.translate.v3beta1.Glossary) GlossaryName(com.google.cloud.translate.v3beta1.GlossaryName) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Example 33 with GcsSource

use of com.google.cloud.aiplatform.v1.GcsSource in project java-retail by googleapis.

the class ImportProductsGcs method getImportProductsGcsRequest.

public static ImportProductsRequest getImportProductsGcsRequest(String gcsObjectName, String gcsBucket, String gcsErrorBucket, String branchName) {
    GcsSource gcsSource = GcsSource.newBuilder().addAllInputUris(Collections.singleton(String.format("%s/%s", gcsBucket, gcsObjectName))).build();
    ProductInputConfig inputConfig = ProductInputConfig.newBuilder().setGcsSource(gcsSource).build();
    System.out.println("GRS source: " + gcsSource.getInputUrisList());
    ImportErrorsConfig errorsConfig = ImportErrorsConfig.newBuilder().setGcsPrefix(gcsErrorBucket).build();
    ImportProductsRequest importRequest = ImportProductsRequest.newBuilder().setParent(branchName).setReconciliationMode(ReconciliationMode.INCREMENTAL).setInputConfig(inputConfig).setErrorsConfig(errorsConfig).build();
    System.out.println("Import products from google cloud source request: " + importRequest);
    return importRequest;
}
Also used : ImportProductsRequest(com.google.cloud.retail.v2.ImportProductsRequest) GcsSource(com.google.cloud.retail.v2.GcsSource) ProductInputConfig(com.google.cloud.retail.v2.ProductInputConfig) ImportErrorsConfig(com.google.cloud.retail.v2.ImportErrorsConfig)

Example 34 with GcsSource

use of com.google.cloud.aiplatform.v1.GcsSource in project java-retail by googleapis.

the class ImportUserEventsGcs method importUserEventsFromGcs.

public static void importUserEventsFromGcs(String gcsEventsObject, String defaultCatalog) throws IOException, InterruptedException {
    try {
        String gcsBucket = String.format("gs://%s", EventsCreateGcsBucket.getBucketName());
        String gcsErrorsBucket = String.format("%s/error", gcsBucket);
        GcsSource gcsSource = GcsSource.newBuilder().addInputUris(String.format("%s/%s", gcsBucket, gcsEventsObject)).build();
        UserEventInputConfig inputConfig = UserEventInputConfig.newBuilder().setGcsSource(gcsSource).build();
        ImportErrorsConfig errorsConfig = ImportErrorsConfig.newBuilder().setGcsPrefix(gcsErrorsBucket).build();
        ImportUserEventsRequest importRequest = ImportUserEventsRequest.newBuilder().setParent(defaultCatalog).setInputConfig(inputConfig).setErrorsConfig(errorsConfig).build();
        System.out.printf("Import user events from google cloud source request: %s%n", importRequest);
        // the "close" method on the client to safely clean up any remaining background resources.
        try (UserEventServiceClient serviceClient = UserEventServiceClient.create()) {
            String operationName = serviceClient.importUserEventsCallable().call(importRequest).getName();
            System.out.printf("OperationName = %s\n", operationName);
            OperationsClient operationsClient = serviceClient.getOperationsClient();
            Operation operation = operationsClient.getOperation(operationName);
            while (!operation.getDone()) {
                // Keep polling the operation periodically until the import task is done.
                int awaitDuration = 30000;
                Thread.sleep(awaitDuration);
                operation = operationsClient.getOperation(operationName);
            }
            if (operation.hasMetadata()) {
                ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class);
                System.out.printf("Number of successfully imported events: %s\n", metadata.getSuccessCount());
                System.out.printf("Number of failures during the importing: %s\n", metadata.getFailureCount());
            }
            if (operation.hasResponse()) {
                ImportUserEventsResponse response = operation.getResponse().unpack(ImportUserEventsResponse.class);
                System.out.printf("Operation result: %s%n", response);
            }
        } catch (InvalidArgumentException e) {
            System.out.printf("Given GCS input path was not found. %n%s%n " + "Please run CreateTestResources class to create resources.", e.getMessage());
        }
    } catch (BigQueryException e) {
        System.out.printf("Exception message: %s", e.getMessage());
    }
}
Also used : ImportUserEventsResponse(com.google.cloud.retail.v2.ImportUserEventsResponse) GcsSource(com.google.cloud.retail.v2.GcsSource) ImportErrorsConfig(com.google.cloud.retail.v2.ImportErrorsConfig) OperationsClient(com.google.longrunning.OperationsClient) Operation(com.google.longrunning.Operation) UserEventInputConfig(com.google.cloud.retail.v2.UserEventInputConfig) UserEventServiceClient(com.google.cloud.retail.v2.UserEventServiceClient) ImportUserEventsRequest(com.google.cloud.retail.v2.ImportUserEventsRequest) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) BigQueryException(com.google.cloud.bigquery.BigQueryException) ImportMetadata(com.google.cloud.retail.v2.ImportMetadata)

Example 35 with GcsSource

use of com.google.cloud.aiplatform.v1.GcsSource in project java-retail by googleapis.

the class CreateTestResources method importProductsFromGcs.

public static void importProductsFromGcs(String bucketName, String gcsErrorBucket, String branchName) throws IOException, InterruptedException {
    GcsSource gcsSource = GcsSource.newBuilder().addAllInputUris(Collections.singleton(String.format("gs://%s/%s", bucketName, "products.json"))).build();
    ProductInputConfig inputConfig = ProductInputConfig.newBuilder().setGcsSource(gcsSource).build();
    System.out.println("GRS source: " + gcsSource.getInputUrisList());
    ImportErrorsConfig errorsConfig = ImportErrorsConfig.newBuilder().setGcsPrefix(gcsErrorBucket).build();
    ImportProductsRequest importRequest = ImportProductsRequest.newBuilder().setParent(branchName).setReconciliationMode(ReconciliationMode.INCREMENTAL).setInputConfig(inputConfig).setErrorsConfig(errorsConfig).build();
    System.out.println("Import products from google cloud source request: " + importRequest);
    try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
        String operationName = serviceClient.importProductsCallable().call(importRequest).getName();
        System.out.printf("OperationName = %s\n", operationName);
        OperationsClient operationsClient = serviceClient.getOperationsClient();
        Operation operation = operationsClient.getOperation(operationName);
        while (!operation.getDone()) {
            System.out.println("Please wait till operation is completed.");
            // Keep polling the operation periodically until the import task is done.
            Thread.sleep(30_000);
            operation = operationsClient.getOperation(operationName);
        }
        System.out.println("Import products operation is completed.");
        if (operation.hasMetadata()) {
            ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class);
            System.out.printf("Number of successfully imported products: %s\n", metadata.getSuccessCount());
            System.out.printf("Number of failures during the importing: %s\n", metadata.getFailureCount());
        }
        if (operation.hasResponse()) {
            ImportProductsResponse response = operation.getResponse().unpack(ImportProductsResponse.class);
            System.out.printf("Operation result: %s", response);
        }
    }
}
Also used : ImportProductsRequest(com.google.cloud.retail.v2.ImportProductsRequest) GcsSource(com.google.cloud.retail.v2.GcsSource) ProductInputConfig(com.google.cloud.retail.v2.ProductInputConfig) ImportErrorsConfig(com.google.cloud.retail.v2.ImportErrorsConfig) ImportProductsResponse(com.google.cloud.retail.v2.ImportProductsResponse) ProductServiceClient(com.google.cloud.retail.v2.ProductServiceClient) OperationsClient(com.google.longrunning.OperationsClient) Operation(com.google.longrunning.Operation) ImportMetadata(com.google.cloud.retail.v2.ImportMetadata)

Aggregations

GcsSource (com.google.cloud.aiplatform.v1.GcsSource)16 BatchPredictionJob (com.google.cloud.aiplatform.v1.BatchPredictionJob)8 DatasetName (com.google.cloud.aiplatform.v1.DatasetName)8 DatasetServiceClient (com.google.cloud.aiplatform.v1.DatasetServiceClient)8 DatasetServiceSettings (com.google.cloud.aiplatform.v1.DatasetServiceSettings)8 GcsDestination (com.google.cloud.aiplatform.v1.GcsDestination)8 ImportDataConfig (com.google.cloud.aiplatform.v1.ImportDataConfig)8 ImportDataOperationMetadata (com.google.cloud.aiplatform.v1.ImportDataOperationMetadata)8 ImportDataResponse (com.google.cloud.aiplatform.v1.ImportDataResponse)8 JobServiceClient (com.google.cloud.aiplatform.v1.JobServiceClient)8 JobServiceSettings (com.google.cloud.aiplatform.v1.JobServiceSettings)8 LocationName (com.google.cloud.aiplatform.v1.LocationName)7 Document (com.google.cloud.documentai.v1beta2.Document)5 DocumentUnderstandingServiceClient (com.google.cloud.documentai.v1beta2.DocumentUnderstandingServiceClient)5 GcsSource (com.google.cloud.documentai.v1beta2.GcsSource)5 InputConfig (com.google.cloud.documentai.v1beta2.InputConfig)5 ProcessDocumentRequest (com.google.cloud.documentai.v1beta2.ProcessDocumentRequest)5 GcsSource (com.google.cloud.translate.v3.GcsSource)5 LocationName (com.google.cloud.translate.v3.LocationName)5 TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)5