Search in sources :

Example 96 with LocationName

use of com.google.cloud.servicedirectory.v1.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());
        }
    }
}
Also used : Translation(com.google.cloud.translate.v3.Translation) TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) TranslateTextRequest(com.google.cloud.translate.v3.TranslateTextRequest) GlossaryName(com.google.cloud.translate.v3.GlossaryName) TranslateTextGlossaryConfig(com.google.cloud.translate.v3.TranslateTextGlossaryConfig) TranslateTextResponse(com.google.cloud.translate.v3.TranslateTextResponse) LocationName(com.google.cloud.translate.v3.LocationName)

Example 97 with LocationName

use of com.google.cloud.servicedirectory.v1.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());
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) ByteString(com.google.protobuf.ByteString) TranslateDocumentRequest(com.google.cloud.translate.v3beta1.TranslateDocumentRequest) TranslateDocumentResponse(com.google.cloud.translate.v3beta1.TranslateDocumentResponse) DocumentInputConfig(com.google.cloud.translate.v3beta1.DocumentInputConfig) FileInputStream(java.io.FileInputStream) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Example 98 with LocationName

use of com.google.cloud.servicedirectory.v1.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");
    }
}
Also used : HttpRequest(com.google.cloud.tasks.v2.HttpRequest) Task(com.google.cloud.tasks.v2.Task) CreateQueueRequest(com.google.cloud.tasks.v2.CreateQueueRequest) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) QueueName(com.google.cloud.tasks.v2.QueueName) Queue(com.google.cloud.tasks.v2.Queue) LocationName(com.google.cloud.tasks.v2.LocationName)

Example 99 with LocationName

use of com.google.cloud.servicedirectory.v1.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");
    }
}
Also used : HttpRequest(com.google.cloud.tasks.v2.HttpRequest) Task(com.google.cloud.tasks.v2.Task) CreateQueueRequest(com.google.cloud.tasks.v2.CreateQueueRequest) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) QueueName(com.google.cloud.tasks.v2.QueueName) Queue(com.google.cloud.tasks.v2.Queue) LocationName(com.google.cloud.tasks.v2.LocationName)

Example 100 with LocationName

use of com.google.cloud.servicedirectory.v1.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());
    }
}
Also used : PhraseSet(com.google.cloud.speech.v1p1beta1.PhraseSet) RecognizeResponse(com.google.cloud.speech.v1p1beta1.RecognizeResponse) SpeechRecognitionResult(com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult) LocationName(com.google.cloud.speech.v1p1beta1.LocationName) CustomClass(com.google.cloud.speech.v1p1beta1.CustomClass) CreatePhraseSetRequest(com.google.cloud.speech.v1p1beta1.CreatePhraseSetRequest) CreateCustomClassRequest(com.google.cloud.speech.v1p1beta1.CreateCustomClassRequest) SpeechRecognitionAlternative(com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative) RecognitionAudio(com.google.cloud.speech.v1p1beta1.RecognitionAudio) SpeechAdaptation(com.google.cloud.speech.v1p1beta1.SpeechAdaptation) RecognitionConfig(com.google.cloud.speech.v1p1beta1.RecognitionConfig) SpeechClient(com.google.cloud.speech.v1p1beta1.SpeechClient) AdaptationClient(com.google.cloud.speech.v1p1beta1.AdaptationClient) ApiException(com.google.api.gax.rpc.ApiException)

Aggregations

Test (org.junit.Test)33 LocationName (com.google.privacy.dlp.v2.LocationName)22 OrganizationLocationName (com.google.privacy.dlp.v2.OrganizationLocationName)22 AutoMlClient (com.google.cloud.automl.v1.AutoMlClient)17 LocationName (com.google.cloud.automl.v1.LocationName)17 AbstractMessage (com.google.protobuf.AbstractMessage)17 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)16 StatusRuntimeException (io.grpc.StatusRuntimeException)14 LocationName (com.google.cloud.translate.v3.LocationName)13 TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)13 OperationMetadata (com.google.cloud.automl.v1.OperationMetadata)12 AutoMlClient (com.google.cloud.automl.v1beta1.AutoMlClient)12 LocationName (com.google.cloud.automl.v1beta1.LocationName)12 LocationName (com.google.cloud.translate.v3beta1.LocationName)10 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)10 Model (com.google.cloud.automl.v1.Model)8 LocationName (com.google.cloud.bigquery.connection.v1.LocationName)8 Dataset (com.google.cloud.automl.v1.Dataset)7 Dataset (com.google.cloud.automl.v1beta1.Dataset)6 CloudTasksClient (com.google.cloud.tasks.v2.CloudTasksClient)5