use of com.google.cloud.speech.v1p1beta1.LocationName in project java-translate by googleapis.
the class BatchTranslateDocument method batchTranslateDocument.
// Batch translate document
public static void batchTranslateDocument(String projectId, String sourceLanguage, String targetLanguage, String inputUri, String outputUri, int timeout) throws IOException, ExecutionException, InterruptedException, TimeoutException {
// refer to https://github.com/googleapis/java-translate/issues/613
TranslationServiceSettings.Builder translationServiceSettingsBuilder = TranslationServiceSettings.newBuilder();
TimedRetryAlgorithm timedRetryAlgorithm = OperationTimedPollAlgorithm.create(RetrySettings.newBuilder().setTotalTimeout(Duration.ofSeconds(1000)).build());
translationServiceSettingsBuilder.batchTranslateDocumentOperationSettings().setPollingAlgorithm(timedRetryAlgorithm);
TranslationServiceSettings translationServiceSettings = translationServiceSettingsBuilder.build();
// up any remaining background resources.
try (TranslationServiceClient client = TranslationServiceClient.create(translationServiceSettings)) {
// The ``global`` location is not supported for batch translation
LocationName parent = LocationName.of(projectId, "us-central1");
// Google Cloud Storage location for the source input. This can be a single file
// (for example, ``gs://translation-test/input.docx``) or a wildcard
// (for example, ``gs://translation-test/*``).
// Supported file types: https://cloud.google.com/translate/docs/supported-formats
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
BatchDocumentInputConfig batchDocumentInputConfig = BatchDocumentInputConfig.newBuilder().setGcsSource(gcsSource).build();
GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
BatchDocumentOutputConfig batchDocumentOutputConfig = BatchDocumentOutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
BatchTranslateDocumentRequest request = BatchTranslateDocumentRequest.newBuilder().setParent(parent.toString()).setSourceLanguageCode(sourceLanguage).addTargetLanguageCodes(targetLanguage).addInputConfigs(batchDocumentInputConfig).setOutputConfig(batchDocumentOutputConfig).build();
OperationFuture<BatchTranslateDocumentResponse, BatchTranslateDocumentMetadata> future = client.batchTranslateDocumentAsync(request);
System.out.println("Waiting for operation to complete...");
// random number between timeout
long randomNumber = ThreadLocalRandom.current().nextInt(timeout, timeout + 100);
BatchTranslateDocumentResponse response = future.get(randomNumber, TimeUnit.SECONDS);
System.out.println("Total Pages: " + response.getTotalPages());
}
}
use of com.google.cloud.speech.v1p1beta1.LocationName in project java-translate by googleapis.
the class BatchTranslateTextWithGlossary method batchTranslateTextWithGlossary.
// Batch Translate Text with a Glossary.
public static void batchTranslateTextWithGlossary(String projectId, String sourceLanguage, String targetLanguage, String inputUri, String outputUri, String glossaryId) 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 glossary used in the request
GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
TranslateTextGlossaryConfig glossaryConfig = TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
// 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).putGlossaries(targetLanguage, glossaryConfig).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());
}
}
use of com.google.cloud.speech.v1p1beta1.LocationName in project java-translate by googleapis.
the class GetSupportedLanguagesForTarget method getSupportedLanguagesForTarget.
// Listing supported languages with target language name
public static void getSupportedLanguagesForTarget(String projectId, String languageCode) throws IOException {
// 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)
LocationName parent = LocationName.of(projectId, "global");
GetSupportedLanguagesRequest request = GetSupportedLanguagesRequest.newBuilder().setParent(parent.toString()).setDisplayLanguageCode(languageCode).build();
SupportedLanguages response = client.getSupportedLanguages(request);
// List language codes of supported languages
for (SupportedLanguage language : response.getLanguagesList()) {
System.out.printf("Language Code: %s\n", language.getLanguageCode());
System.out.printf("Display Name: %s\n", language.getDisplayName());
}
}
}
use of com.google.cloud.speech.v1p1beta1.LocationName in project java-translate by googleapis.
the class TranslateTextWithGlossary method translateTextWithGlossary.
// Translates a given text using a glossary.
public static void translateTextWithGlossary(String projectId, String sourceLanguage, String targetLanguage, String text, String glossaryId) throws IOException {
// 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);
GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
TranslateTextGlossaryConfig glossaryConfig = TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
// Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
TranslateTextRequest request = TranslateTextRequest.newBuilder().setParent(parent.toString()).setMimeType("text/plain").setSourceLanguageCode(sourceLanguage).setTargetLanguageCode(targetLanguage).addContents(text).setGlossaryConfig(glossaryConfig).build();
TranslateTextResponse response = client.translateText(request);
// Display the translation for each input text provided
for (Translation translation : response.getGlossaryTranslationsList()) {
System.out.printf("Translated text: %s\n", translation.getTranslatedText());
}
}
}
use of com.google.cloud.speech.v1p1beta1.LocationName in project java-automl by googleapis.
the class VisionObjectDetectionCreateModel method createModel.
// Create a model
static void createModel(String projectId, String datasetId, String displayName) throws IOException, ExecutionException, InterruptedException {
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// A resource that represents Google Cloud Platform location.
LocationName projectLocation = LocationName.of(projectId, "us-central1");
// Set model metadata.
ImageObjectDetectionModelMetadata metadata = ImageObjectDetectionModelMetadata.newBuilder().build();
Model model = Model.newBuilder().setDisplayName(displayName).setDatasetId(datasetId).setImageObjectDetectionModelMetadata(metadata).build();
// Create a model with the model metadata in the region.
OperationFuture<Model, OperationMetadata> future = client.createModelAsync(projectLocation, model);
// OperationFuture.get() will block until the model is created, which may take several hours.
// You can use OperationFuture.getInitialFuture to get a future representing the initial
// response to the request, which contains information while the operation is in progress.
System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName());
System.out.println("Training started...");
}
}
Aggregations