use of com.google.cloud.aiplatform.v1.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);
}
}
use of com.google.cloud.aiplatform.v1.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;
}
}
use of com.google.cloud.aiplatform.v1.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.");
}
}
use of com.google.cloud.aiplatform.v1.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));
}
}
use of com.google.cloud.aiplatform.v1.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());
}
}
Aggregations