use of com.google.cloud.translate.v3.LocationName in project java-translate by googleapis.
the class TranslateTextWithGlossaryAndModel method translateTextWithGlossaryAndModel.
// Translating Text with Glossary and Model
public static void translateTextWithGlossaryAndModel(String projectId, String sourceLanguage, String targetLanguage, String text, String glossaryId, String modelId) 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();
String modelPath = String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
// 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).setModel(modelPath).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.translate.v3.LocationName in project java-translate by googleapis.
the class TranslateDocument method translateDocument.
// Translating Document
public static void translateDocument(String projectId, String filePath) throws IOException {
// up any remaining background resources.
try (TranslationServiceClient client = TranslationServiceClient.create()) {
// The ``global`` location is not supported for batch translation
LocationName parent = LocationName.of(projectId, "us-central1");
// Supported file types: https://cloud.google.com/translate/docs/supported-formats
ByteString content = ByteString.readFrom(new FileInputStream(filePath));
DocumentInputConfig documentInputConfig = DocumentInputConfig.newBuilder().setContent(content).setMimeType("application/pdf").build();
TranslateDocumentRequest request = TranslateDocumentRequest.newBuilder().setParent(parent.toString()).setTargetLanguageCode("fr-FR").setDocumentInputConfig(documentInputConfig).build();
TranslateDocumentResponse response = client.translateDocument(request);
// To view translated document, write `response.document_translation.byte_stream_outputs`
// to file. If not provided in the TranslationRequest, the translated file will only be
// returned through a byte-stream and its output mime type will be the same as the input
// file's mime type
System.out.println("Response: Detected Language Code - " + response.getDocumentTranslation().getDetectedLanguageCode());
}
}
use of com.google.cloud.translate.v3.LocationName in project java-tasks by googleapis.
the class TasksSampleApplication method main.
/**
* Runs the Cloud Tasks sample application.
*/
public static void main(String[] args) throws IOException {
String projectId = ServiceOptions.getDefaultProjectId();
LocationName parent = LocationName.of(projectId, LOCATION_ID);
QueueName queueName = QueueName.of(parent.getProject(), parent.getLocation(), GRAALVM_TEST_QUEUE_NAME + UUID.randomUUID().toString());
try (CloudTasksClient client = CloudTasksClient.create()) {
// Create queue
Queue queue = Queue.newBuilder().setName(queueName.toString()).setRateLimits(RateLimits.newBuilder().setMaxConcurrentDispatches(1).build()).build();
CreateQueueRequest createQueueRequest = CreateQueueRequest.newBuilder().setParent(parent.toString()).setQueue(queue).build();
Queue createdQueue = client.createQueue(createQueueRequest);
System.out.println("Test queue ready: " + createdQueue);
// Create task
HttpRequest taskTarget = HttpRequest.newBuilder().setUrl("https://google.com").setHttpMethod(HttpMethod.GET).build();
Task taskRequest = Task.newBuilder().setHttpRequest(taskTarget).build();
Task task = client.createTask(queueName, taskRequest);
System.out.println("Created task: " + task);
// Cleanup
client.purgeQueue(queueName);
System.out.println("Queue purged");
client.deleteQueue(queueName);
System.out.println("Queue deleted");
}
}
use of com.google.cloud.translate.v3.LocationName in project native-image-support-java by GoogleCloudPlatform.
the class TasksSampleApplication method main.
/**
* Runs the Cloud Tasks sample application.
*/
public static void main(String[] args) throws IOException {
String projectId = ServiceOptions.getDefaultProjectId();
LocationName parent = LocationName.of(projectId, "us-east1");
QueueName queueName = QueueName.of(parent.getProject(), parent.getLocation(), GRAALVM_TEST_QUEUE_NAME + UUID.randomUUID().toString());
try (CloudTasksClient client = CloudTasksClient.create()) {
// Create queue
Queue queue = Queue.newBuilder().setName(queueName.toString()).setRateLimits(RateLimits.newBuilder().setMaxConcurrentDispatches(1).build()).build();
CreateQueueRequest createQueueRequest = CreateQueueRequest.newBuilder().setParent(parent.toString()).setQueue(queue).build();
Queue createdQueue = client.createQueue(createQueueRequest);
System.out.println("Test queue ready: " + createdQueue);
// Create task
HttpRequest taskTarget = HttpRequest.newBuilder().setUrl("https://google.com").setHttpMethod(HttpMethod.GET).build();
Task taskRequest = Task.newBuilder().setHttpRequest(taskTarget).build();
Task task = client.createTask(queueName, taskRequest);
System.out.println("Created task: " + task);
// Cleanup
client.purgeQueue(queueName);
System.out.println("Queue purged");
client.deleteQueue(queueName);
System.out.println("Queue deleted");
}
}
use of com.google.cloud.translate.v3.LocationName in project java-speech by googleapis.
the class SpeechModelAdaptationBeta method transcribeWithModelAdaptation.
/**
* Transcribe with model adaptation
*
* @param projectId your project id
* @param location the region
* @param gcsUri the path to the audio file
*/
public static void transcribeWithModelAdaptation(String projectId, String location, String gcsUri, String customClassId, String phraseSetId) throws Exception {
// the "close" method on the client to safely clean up any remaining background resources.
try (AdaptationClient adaptationClient = AdaptationClient.create()) {
// Create `PhraseSet` and `CustomClasses` to create custom lists of similar
// items that are likely to occur in your input data.
// The parent resource where the custom class and phrase set will be created.
LocationName parent = LocationName.of(projectId, location);
// Create the custom class
CreateCustomClassRequest classRequest = CreateCustomClassRequest.newBuilder().setParent(parent.toString()).setCustomClassId(customClassId).setCustomClass(CustomClass.newBuilder().addItems(ClassItem.newBuilder().setValue("sushido")).addItems(ClassItem.newBuilder().setValue("altura")).addItems(ClassItem.newBuilder().setValue("taneda")).build()).build();
CustomClass classResponse = adaptationClient.createCustomClass(classRequest);
// Create the phrase set
CreatePhraseSetRequest phraseRequest = CreatePhraseSetRequest.newBuilder().setParent(parent.toString()).setPhraseSetId(phraseSetId).setPhraseSet(PhraseSet.newBuilder().setBoost(10).addPhrases(Phrase.newBuilder().setValue(String.format("Visit restaurants like %s%n", customClassId))).build()).build();
PhraseSet phraseResponse = adaptationClient.createPhraseSet(phraseRequest);
// Next section shows how to use the newly created custom class and phrase set
// to send a transcription request with speech adaptation
// Speech adaptation configuration
SpeechAdaptation speechAdaptation = SpeechAdaptation.newBuilder().addCustomClasses(classResponse).addPhraseSets(phraseResponse).build();
// the "close" method on the client to safely clean up any remaining background resources.
try (SpeechClient speechClient = SpeechClient.create()) {
// The path to the audio file to transcribe
// gcsUri URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE]
// Builds the sync recognize request
RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.FLAC).setSampleRateHertz(16000).setLanguageCode("en-US").setAdaptation(// Set the adaptation object
speechAdaptation).build();
RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build();
// Performs speech recognition on the audio file.
RecognizeResponse response = speechClient.recognize(config, audio);
List<SpeechRecognitionResult> results = response.getResultsList();
for (SpeechRecognitionResult result : results) {
// There can be several alternative transcripts for a given chunk of speech. Just use the
// first (most likely) one here.
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Adapted Transcription: %s%n", alternative.getTranscript());
}
}
} catch (ApiException e) {
System.out.println("Client Interaction Error: \n" + e.toString());
}
}
Aggregations