Search in sources :

Example 26 with InputConfig

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

the class BatchTranslateTextWithModel method batchTranslateTextWithModel.

// Batch translate text using AutoML Translation model
public static void batchTranslateTextWithModel(String projectId, String sourceLanguage, String targetLanguage, String inputUri, String outputUri, String modelId) 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: `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);
        // Configure the source of the file from a GCS bucket
        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();
        // Configure where to store the output in a GCS bucket
        GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
        OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
        // Configure the model used in the request
        String modelPath = String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
        // Build the request that will be sent to the API
        BatchTranslateTextRequest request = BatchTranslateTextRequest.newBuilder().setParent(parent.toString()).setSourceLanguageCode(sourceLanguage).addTargetLanguageCodes(targetLanguage).addInputConfigs(inputConfig).setOutputConfig(outputConfig).putModels(targetLanguage, modelPath).build();
        // Start an asynchronous request
        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);
        // Display the translation for each input text provided
        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 27 with InputConfig

use of com.google.cloud.automl.v1beta1.InputConfig 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 28 with InputConfig

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

the class LabelTextIT method setUp.

@Before
public void setUp() {
    System.setOut(new PrintStream(new ByteArrayOutputStream()));
    try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) {
        // Create the dataset
        CreateDataset.createDataset(PROJECT_ID, datasetName);
        ProjectName projectName = ProjectName.of(PROJECT_ID);
        // Get the Dataset
        ListDatasetsRequest datasetsRequest = ListDatasetsRequest.newBuilder().setParent(projectName.toString()).build();
        ListDatasetsPagedResponse datasetsResponse = dataLabelingServiceClient.listDatasets(datasetsRequest);
        for (Dataset returnedDataset : datasetsResponse.getPage().iterateAll()) {
            if (returnedDataset.getDisplayName().equals("LABEL_TEXT_DATASET_NAME")) {
                dataset = returnedDataset;
            }
        }
        // Import the texts
        GcsSource gcsSource = GcsSource.newBuilder().setInputUri(DATASET_GCS_SOURCE_URI).setMimeType("text/csv").build();
        InputConfig inputConfig = InputConfig.newBuilder().setDataType(// DataTypes: AUDIO, IMAGE, VIDEO, TEXT
        DataType.TEXT).setGcsSource(gcsSource).build();
        ImportDataRequest importDataRequest = ImportDataRequest.newBuilder().setName(dataset.getName()).setInputConfig(inputConfig).build();
        ImportDataOperationResponse response = dataLabelingServiceClient.importDataAsync(importDataRequest).get();
        System.out.format("Imported items: %d\n", response.getImportCount());
        // Create the instruction
        CreateInstruction.createInstruction(PROJECT_ID, INSTRUCTION_GCS_SOURCE_URI);
        // Create the annotation spec set
        CreateAnnotationSpecSet.createAnnotationSpecSet(PROJECT_ID);
        // Get the instruction
        ListInstructionsRequest instructionsRequest = ListInstructionsRequest.newBuilder().setParent(projectName.toString()).build();
        ListInstructionsPagedResponse instructionsResponse = dataLabelingServiceClient.listInstructions(instructionsRequest);
        for (Instruction returnedInstruction : instructionsResponse.getPage().iterateAll()) {
            if (returnedInstruction.getDisplayName().equals("YOUR_INSTRUCTION_DISPLAY_NAME")) {
                instruction = returnedInstruction;
            }
        }
        // Get the annotation spec set
        ListAnnotationSpecSetsRequest annotationRequest = ListAnnotationSpecSetsRequest.newBuilder().setParent(projectName.toString()).build();
        ListAnnotationSpecSetsPagedResponse annotationsResponse = dataLabelingServiceClient.listAnnotationSpecSets(annotationRequest);
        for (AnnotationSpecSet returnedAnnotation : annotationsResponse.getPage().iterateAll()) {
            if (returnedAnnotation.getDisplayName().equals("YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME")) {
                annotationSpecSet = returnedAnnotation;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ListAnnotationSpecSetsPagedResponse(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListAnnotationSpecSetsPagedResponse) PrintStream(java.io.PrintStream) DataLabelingServiceClient(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient) ListDatasetsPagedResponse(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListDatasetsPagedResponse) GcsSource(com.google.cloud.datalabeling.v1beta1.GcsSource) ProjectName(com.google.cloud.datalabeling.v1beta1.ProjectName) ListAnnotationSpecSetsRequest(com.google.cloud.datalabeling.v1beta1.ListAnnotationSpecSetsRequest) Dataset(com.google.cloud.datalabeling.v1beta1.Dataset) AnnotationSpecSet(com.google.cloud.datalabeling.v1beta1.AnnotationSpecSet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Instruction(com.google.cloud.datalabeling.v1beta1.Instruction) IOException(java.io.IOException) ListDatasetsRequest(com.google.cloud.datalabeling.v1beta1.ListDatasetsRequest) ListInstructionsRequest(com.google.cloud.datalabeling.v1beta1.ListInstructionsRequest) ListInstructionsPagedResponse(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListInstructionsPagedResponse) InputConfig(com.google.cloud.datalabeling.v1beta1.InputConfig) ImportDataRequest(com.google.cloud.datalabeling.v1beta1.ImportDataRequest) ImportDataOperationResponse(com.google.cloud.datalabeling.v1beta1.ImportDataOperationResponse) Before(org.junit.Before)

Aggregations

Document (com.google.cloud.documentai.v1beta2.Document)7 DocumentUnderstandingServiceClient (com.google.cloud.documentai.v1beta2.DocumentUnderstandingServiceClient)7 GcsSource (com.google.cloud.documentai.v1beta2.GcsSource)7 InputConfig (com.google.cloud.documentai.v1beta2.InputConfig)7 ProcessDocumentRequest (com.google.cloud.documentai.v1beta2.ProcessDocumentRequest)7 InputConfig (com.google.cloud.vision.v1.InputConfig)6 BatchTranslateMetadata (com.google.cloud.translate.v3.BatchTranslateMetadata)4 BatchTranslateResponse (com.google.cloud.translate.v3.BatchTranslateResponse)4 BatchTranslateTextRequest (com.google.cloud.translate.v3.BatchTranslateTextRequest)4 GcsDestination (com.google.cloud.translate.v3.GcsDestination)4 GcsSource (com.google.cloud.translate.v3.GcsSource)4 InputConfig (com.google.cloud.translate.v3.InputConfig)4 LocationName (com.google.cloud.translate.v3.LocationName)4 OutputConfig (com.google.cloud.translate.v3.OutputConfig)4 TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)4 ByteString (com.google.protobuf.ByteString)4 OperationMetadata (com.google.cloud.automl.v1beta1.OperationMetadata)3 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)3 Feature (com.google.cloud.vision.v1.Feature)3 GcsSource (com.google.cloud.vision.v1.GcsSource)3