Search in sources :

Example 11 with OutputConfig

use of com.google.cloud.datalabeling.v1beta1.OutputConfig 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 12 with OutputConfig

use of com.google.cloud.datalabeling.v1beta1.OutputConfig in project gapic-generator-java by googleapis.

the class SyncExportInstanceStringOutputconfig method syncExportInstanceStringOutputconfig.

public static void syncExportInstanceStringOutputconfig() throws Exception {
    // It may require modifications to work in your environment.
    try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
        String name = "name3373707";
        OutputConfig outputConfig = OutputConfig.newBuilder().build();
        Instance response = cloudRedisClient.exportInstanceAsync(name, outputConfig).get();
    }
}
Also used : OutputConfig(com.google.cloud.redis.v1beta1.OutputConfig) CloudRedisClient(com.google.cloud.redis.v1beta1.CloudRedisClient) Instance(com.google.cloud.redis.v1beta1.Instance)

Example 13 with OutputConfig

use of com.google.cloud.datalabeling.v1beta1.OutputConfig in project java-asset by googleapis.

the class ExportAssetsBigqueryExample method exportBigQuery.

// Export assets to BigQuery for a project.
public static void exportBigQuery(String bigqueryDataset, String bigqueryTable, ContentType contentType, boolean isPerType) throws IOException, IllegalArgumentException, InterruptedException, ExecutionException {
    try (AssetServiceClient client = AssetServiceClient.create()) {
        ProjectName parent = ProjectName.of(projectId);
        OutputConfig outputConfig;
        // Outputs to per-type BigQuery table.
        if (isPerType) {
            outputConfig = OutputConfig.newBuilder().setBigqueryDestination(BigQueryDestination.newBuilder().setDataset(bigqueryDataset).setTable(bigqueryTable).setForce(true).setSeparateTablesPerAssetType(true).setPartitionSpec(PartitionSpec.newBuilder().setPartitionKey(PartitionSpec.PartitionKey.READ_TIME).build()).build()).build();
        } else {
            outputConfig = OutputConfig.newBuilder().setBigqueryDestination(BigQueryDestination.newBuilder().setDataset(bigqueryDataset).setTable(bigqueryTable).setForce(true).build()).build();
        }
        ExportAssetsRequest request = ExportAssetsRequest.newBuilder().setParent(parent.toString()).setContentType(contentType).setOutputConfig(outputConfig).build();
        ExportAssetsResponse response = client.exportAssetsAsync(request).get();
        System.out.println(response);
    }
}
Also used : OutputConfig(com.google.cloud.asset.v1.OutputConfig) ExportAssetsRequest(com.google.cloud.asset.v1.ExportAssetsRequest) ProjectName(com.google.cloud.asset.v1.ProjectName) AssetServiceClient(com.google.cloud.asset.v1.AssetServiceClient) ExportAssetsResponse(com.google.cloud.asset.v1.ExportAssetsResponse)

Example 14 with OutputConfig

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

the class TranslateSnippetsBeta method batchTranslateText.

// [END translate_translate_text_beta]
/**
 * Translates a batch of texts on GCS and stores the result in a GCS location.
 *
 * @param projectId - Id of the project.
 * @param location - location name.
 * @param sourceUri - Google Cloud Storage URI. Location where text is stored.
 * @param destinationUri - Google Cloud Storage URI where result will be stored.
 */
// [START translate_batch_translate_text_beta]
static BatchTranslateResponse batchTranslateText(String projectId, String location, String sourceUri, String destinationUri) {
    try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
        LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
        GcsSource gcsSource = GcsSource.newBuilder().setInputUri(sourceUri).build();
        InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
        GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(destinationUri).build();
        OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
        BatchTranslateTextRequest batchTranslateTextRequest = BatchTranslateTextRequest.newBuilder().setParent(locationName.toString()).setSourceLanguageCode("en").addTargetLanguageCodes("sr").addInputConfigs(inputConfig).setOutputConfig(outputConfig).build();
        // Call the API
        BatchTranslateResponse response = translationServiceClient.batchTranslateTextAsync(batchTranslateTextRequest).get(300, TimeUnit.SECONDS);
        System.out.printf("Total Characters: %d\n", response.getTotalCharacters());
        System.out.printf("Translated Characters: %d\n", response.getTranslatedCharacters());
        return response;
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) GcsSource(com.google.cloud.translate.v3beta1.GcsSource) OutputConfig(com.google.cloud.translate.v3beta1.OutputConfig) BatchTranslateTextRequest(com.google.cloud.translate.v3beta1.BatchTranslateTextRequest) InputConfig(com.google.cloud.translate.v3beta1.InputConfig) GlossaryInputConfig(com.google.cloud.translate.v3beta1.GlossaryInputConfig) GcsDestination(com.google.cloud.translate.v3beta1.GcsDestination) BatchTranslateResponse(com.google.cloud.translate.v3beta1.BatchTranslateResponse) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Example 15 with OutputConfig

use of com.google.cloud.datalabeling.v1beta1.OutputConfig in project java-document-ai by googleapis.

the class BatchParseTableBeta method batchParseTableGcs.

public static void batchParseTableGcs(String projectId, String location, String outputGcsBucketName, String outputGcsPrefix, String inputGcsUri) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (DocumentUnderstandingServiceClient client = DocumentUnderstandingServiceClient.create()) {
        // Configure the request for processing the PDF
        String parent = String.format("projects/%s/locations/%s", projectId, location);
        TableBoundHint tableBoundHints = TableBoundHint.newBuilder().setBoundingBox(// Each vertice coordinate must be a number between 0 and 1
        BoundingPoly.newBuilder().addNormalizedVertices(NormalizedVertex.newBuilder().setX(0).setX(0).build()).addNormalizedVertices(NormalizedVertex.newBuilder().setX(1).setX(0).build()).addNormalizedVertices(NormalizedVertex.newBuilder().setX(1).setX(1).build()).addNormalizedVertices(NormalizedVertex.newBuilder().setX(0).setX(1).build()).build()).setPageNumber(1).build();
        TableExtractionParams params = TableExtractionParams.newBuilder().setEnabled(true).addTableBoundHints(tableBoundHints).build();
        GcsSource inputUri = GcsSource.newBuilder().setUri(inputGcsUri).build();
        // mime_type can be application/pdf, image/tiff,
        // and image/gif, or application/json
        InputConfig config = InputConfig.newBuilder().setGcsSource(inputUri).setMimeType("application/pdf").build();
        GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(String.format("gs://%s/%s", outputGcsBucketName, outputGcsPrefix)).build();
        OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).setPagesPerShard(1).build();
        ProcessDocumentRequest request = ProcessDocumentRequest.newBuilder().setTableExtractionParams(params).setInputConfig(config).setOutputConfig(outputConfig).build();
        BatchProcessDocumentsRequest requests = BatchProcessDocumentsRequest.newBuilder().addRequests(request).setParent(parent).build();
        // Batch process document using a long-running operation.
        OperationFuture<BatchProcessDocumentsResponse, OperationMetadata> future = client.batchProcessDocumentsAsync(requests);
        // Wait for operation to complete.
        System.out.println("Waiting for operation to complete...");
        future.get(360, TimeUnit.SECONDS);
        System.out.println("Document processing complete.");
        Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
        Bucket bucket = storage.get(outputGcsBucketName);
        // List all of the files in the Storage bucket.
        Page<Blob> blobs = bucket.list(Storage.BlobListOption.currentDirectory(), Storage.BlobListOption.prefix(outputGcsPrefix));
        int idx = 0;
        for (Blob blob : blobs.iterateAll()) {
            if (!blob.isDirectory()) {
                System.out.printf("Fetched file #%d\n", ++idx);
                // Read the results
                // Download and store json data in a temp file.
                File tempFile = File.createTempFile("file", ".json");
                Blob fileInfo = storage.get(BlobId.of(outputGcsBucketName, blob.getName()));
                fileInfo.downloadTo(tempFile.toPath());
                // Parse json file into Document.
                FileReader reader = new FileReader(tempFile);
                Document.Builder builder = Document.newBuilder();
                JsonFormat.parser().merge(reader, builder);
                Document document = builder.build();
                // Get all of the document text as one big string.
                String text = document.getText();
                // Process the output.
                if (document.getPagesCount() > 0) {
                    Document.Page page1 = document.getPages(0);
                    if (page1.getTablesCount() > 0) {
                        Document.Page.Table table = page1.getTables(0);
                        System.out.println("Results from first table processed:");
                        System.out.println("Header row:");
                        if (table.getHeaderRowsCount() > 0) {
                            Document.Page.Table.TableRow headerRow = table.getHeaderRows(0);
                            for (Document.Page.Table.TableCell tableCell : headerRow.getCellsList()) {
                                if (!tableCell.getLayout().getTextAnchor().getTextSegmentsList().isEmpty()) {
                                    // Extract shards from the text field
                                    // First shard in document doesn't have startIndex property
                                    List<Document.TextAnchor.TextSegment> textSegments = tableCell.getLayout().getTextAnchor().getTextSegmentsList();
                                    int startIdx = textSegments.size() > 0 ? (int) textSegments.get(0).getStartIndex() : 0;
                                    int endIdx = (int) textSegments.get(0).getEndIndex();
                                    System.out.printf("\t%s", text.substring(startIdx, endIdx));
                                }
                            }
                        }
                    }
                }
                // Clean up temp file.
                tempFile.deleteOnExit();
            }
        }
    }
}
Also used : BatchProcessDocumentsResponse(com.google.cloud.documentai.v1beta2.BatchProcessDocumentsResponse) DocumentUnderstandingServiceClient(com.google.cloud.documentai.v1beta2.DocumentUnderstandingServiceClient) GcsSource(com.google.cloud.documentai.v1beta2.GcsSource) Page(com.google.api.gax.paging.Page) Document(com.google.cloud.documentai.v1beta2.Document) TableExtractionParams(com.google.cloud.documentai.v1beta2.TableExtractionParams) InputConfig(com.google.cloud.documentai.v1beta2.InputConfig) FileReader(java.io.FileReader) BatchProcessDocumentsRequest(com.google.cloud.documentai.v1beta2.BatchProcessDocumentsRequest) OperationMetadata(com.google.cloud.documentai.v1beta2.OperationMetadata) ProcessDocumentRequest(com.google.cloud.documentai.v1beta2.ProcessDocumentRequest) Blob(com.google.cloud.storage.Blob) TableBoundHint(com.google.cloud.documentai.v1beta2.TableBoundHint) TableBoundHint(com.google.cloud.documentai.v1beta2.TableBoundHint) OutputConfig(com.google.cloud.documentai.v1beta2.OutputConfig) Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) GcsDestination(com.google.cloud.documentai.v1beta2.GcsDestination) File(java.io.File)

Aggregations

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 GcsDestination (com.google.cloud.vision.v1.GcsDestination)4 OutputConfig (com.google.cloud.vision.v1.OutputConfig)4 Blob (com.google.cloud.storage.Blob)3 Bucket (com.google.cloud.storage.Bucket)3 Storage (com.google.cloud.storage.Storage)3 Page (com.google.api.gax.paging.Page)2 AssetServiceClient (com.google.cloud.asset.v1.AssetServiceClient)2 ExportAssetsRequest (com.google.cloud.asset.v1.ExportAssetsRequest)2 ExportAssetsResponse (com.google.cloud.asset.v1.ExportAssetsResponse)2 OutputConfig (com.google.cloud.asset.v1.OutputConfig)2 ProjectName (com.google.cloud.asset.v1.ProjectName)2