Search in sources :

Example 26 with GcsSource

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

the class TablesImportDataset method importDataset.

// Import a dataset via BigQuery or Google Cloud Storage
static void importDataset(String projectId, String datasetId, String path) throws IOException, ExecutionException, InterruptedException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (AutoMlClient client = AutoMlClient.create()) {
        // Get the complete path of the dataset.
        DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
        InputConfig.Builder inputConfigBuilder = InputConfig.newBuilder();
        // Determine which source type was used for the input path (BigQuery or GCS)
        if (path.startsWith("bq")) {
            // Get training data file to be imported from a BigQuery source.
            BigQuerySource.Builder bigQuerySource = BigQuerySource.newBuilder();
            bigQuerySource.setInputUri(path);
            inputConfigBuilder.setBigquerySource(bigQuerySource);
        } else {
            // Get multiple Google Cloud Storage URIs to import data from
            GcsSource gcsSource = GcsSource.newBuilder().addAllInputUris(Arrays.asList(path.split(","))).build();
            inputConfigBuilder.setGcsSource(gcsSource);
        }
        // Import data from the input URI
        System.out.println("Processing import...");
        Empty response = client.importDataAsync(datasetFullId, inputConfigBuilder.build()).get();
        System.out.format("Dataset imported. %s%n", response);
    }
}
Also used : Empty(com.google.protobuf.Empty) GcsSource(com.google.cloud.automl.v1beta1.GcsSource) DatasetName(com.google.cloud.automl.v1beta1.DatasetName) InputConfig(com.google.cloud.automl.v1beta1.InputConfig) BigQuerySource(com.google.cloud.automl.v1beta1.BigQuerySource) AutoMlClient(com.google.cloud.automl.v1beta1.AutoMlClient)

Example 27 with GcsSource

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

the class ImportDataset method importDataset.

// Import a dataset
static void importDataset(String projectId, String datasetId, String path) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (AutoMlClient client = AutoMlClient.create()) {
        // Get the complete path of the dataset.
        DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
        // Get multiple Google Cloud Storage URIs to import data from
        GcsSource gcsSource = GcsSource.newBuilder().addAllInputUris(Arrays.asList(path.split(","))).build();
        // Import data from the input URI
        InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build();
        System.out.println("Processing import...");
        // Start the import job
        OperationFuture<Empty, OperationMetadata> operation = client.importDataAsync(datasetFullId, inputConfig);
        System.out.format("Operation name: %s%n", operation.getName());
        // If you want to wait for the operation to finish, adjust the timeout appropriately. The
        // operation will still run if you choose not to wait for it to complete. You can check the
        // status of your operation using the operation's name.
        Empty response = operation.get(45, TimeUnit.MINUTES);
        System.out.format("Dataset imported. %s%n", response);
    } catch (TimeoutException e) {
        System.out.println("The operation's polling period was not long enough.");
        System.out.println("You can use the Operation's name to get the current status.");
        System.out.println("The import job is still running and will complete as expected.");
        throw e;
    }
}
Also used : Empty(com.google.protobuf.Empty) GcsSource(com.google.cloud.automl.v1.GcsSource) DatasetName(com.google.cloud.automl.v1.DatasetName) InputConfig(com.google.cloud.automl.v1.InputConfig) OperationMetadata(com.google.cloud.automl.v1.OperationMetadata) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient) TimeoutException(java.util.concurrent.TimeoutException)

Example 28 with GcsSource

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

the class BatchPredict method batchPredict.

static void batchPredict(String projectId, String modelId, String inputUri, String outputUri) throws IOException, ExecutionException, InterruptedException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient client = PredictionServiceClient.create()) {
        // Get the full path of the model.
        ModelName name = ModelName.of(projectId, "us-central1", modelId);
        GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build();
        BatchPredictInputConfig inputConfig = BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build();
        GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
        BatchPredictOutputConfig outputConfig = BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
        BatchPredictRequest request = BatchPredictRequest.newBuilder().setName(name.toString()).setInputConfig(inputConfig).setOutputConfig(outputConfig).build();
        OperationFuture<BatchPredictResult, OperationMetadata> future = client.batchPredictAsync(request);
        System.out.println("Waiting for operation to complete...");
        BatchPredictResult response = future.get();
        System.out.println("Batch Prediction results saved to specified Cloud Storage bucket.");
    }
}
Also used : BatchPredictRequest(com.google.cloud.automl.v1.BatchPredictRequest) ModelName(com.google.cloud.automl.v1.ModelName) GcsSource(com.google.cloud.automl.v1.GcsSource) BatchPredictInputConfig(com.google.cloud.automl.v1.BatchPredictInputConfig) BatchPredictOutputConfig(com.google.cloud.automl.v1.BatchPredictOutputConfig) BatchPredictResult(com.google.cloud.automl.v1.BatchPredictResult) GcsDestination(com.google.cloud.automl.v1.GcsDestination) OperationMetadata(com.google.cloud.automl.v1.OperationMetadata) PredictionServiceClient(com.google.cloud.automl.v1.PredictionServiceClient)

Example 29 with GcsSource

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

the class DatasetApi method importData.

// [START automl_translate_import_data]
/**
 * Import sentence pairs to the dataset.
 *
 * @param projectId the Google Cloud Project ID.
 * @param computeRegion the Region name. (e.g., "us-central1").
 * @param datasetId the Id of the dataset.
 * @param path the remote Path of the training data csv file.
 */
public static void importData(String projectId, String computeRegion, String datasetId, String path) throws IOException, InterruptedException, ExecutionException {
    // Instantiates a client
    try (AutoMlClient client = AutoMlClient.create()) {
        // Get the complete path of the dataset.
        DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId);
        GcsSource.Builder gcsSource = GcsSource.newBuilder();
        // Get multiple Google Cloud Storage URIs to import data from
        String[] inputUris = path.split(",");
        for (String inputUri : inputUris) {
            gcsSource.addInputUris(inputUri);
        }
        // Import data from the input URI
        InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build();
        System.out.println("Processing import...");
        Empty response = client.importDataAsync(datasetFullId, inputConfig).get();
        System.out.println(String.format("Dataset imported. %s", response));
    }
}
Also used : Empty(com.google.protobuf.Empty) GcsSource(com.google.cloud.automl.v1.GcsSource) DatasetName(com.google.cloud.automl.v1.DatasetName) InputConfig(com.google.cloud.automl.v1.InputConfig) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient)

Example 30 with GcsSource

use of com.google.cloud.datalabeling.v1beta1.GcsSource 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)

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