Search in sources :

Example 1 with CustomClass

use of com.google.cloud.speech.v1p1beta1.CustomClass in project java-speech by googleapis.

the class AdaptationClientTest method listCustomClassesTest.

@Test
public void listCustomClassesTest() throws Exception {
    CustomClass responsesElement = CustomClass.newBuilder().build();
    ListCustomClassesResponse expectedResponse = ListCustomClassesResponse.newBuilder().setNextPageToken("").addAllCustomClasses(Arrays.asList(responsesElement)).build();
    mockAdaptation.addResponse(expectedResponse);
    LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
    ListCustomClassesPagedResponse pagedListResponse = client.listCustomClasses(parent);
    List<CustomClass> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getCustomClassesList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockAdaptation.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListCustomClassesRequest actualRequest = ((ListCustomClassesRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : ListCustomClassesPagedResponse(com.google.cloud.speech.v1p1beta1.AdaptationClient.ListCustomClassesPagedResponse) AbstractMessage(com.google.protobuf.AbstractMessage) Test(org.junit.Test)

Example 2 with CustomClass

use of com.google.cloud.speech.v1p1beta1.CustomClass in project java-speech by googleapis.

the class AdaptationClientTest method listCustomClassesTest2.

@Test
public void listCustomClassesTest2() throws Exception {
    CustomClass responsesElement = CustomClass.newBuilder().build();
    ListCustomClassesResponse expectedResponse = ListCustomClassesResponse.newBuilder().setNextPageToken("").addAllCustomClasses(Arrays.asList(responsesElement)).build();
    mockAdaptation.addResponse(expectedResponse);
    String parent = "parent-995424086";
    ListCustomClassesPagedResponse pagedListResponse = client.listCustomClasses(parent);
    List<CustomClass> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getCustomClassesList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockAdaptation.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListCustomClassesRequest actualRequest = ((ListCustomClassesRequest) actualRequests.get(0));
    Assert.assertEquals(parent, actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : ListCustomClassesPagedResponse(com.google.cloud.speech.v1p1beta1.AdaptationClient.ListCustomClassesPagedResponse) AbstractMessage(com.google.protobuf.AbstractMessage) Test(org.junit.Test)

Example 3 with CustomClass

use of com.google.cloud.speech.v1p1beta1.CustomClass 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

ListCustomClassesPagedResponse (com.google.cloud.speech.v1p1beta1.AdaptationClient.ListCustomClassesPagedResponse)2 AbstractMessage (com.google.protobuf.AbstractMessage)2 Test (org.junit.Test)2 ApiException (com.google.api.gax.rpc.ApiException)1 AdaptationClient (com.google.cloud.speech.v1p1beta1.AdaptationClient)1 CreateCustomClassRequest (com.google.cloud.speech.v1p1beta1.CreateCustomClassRequest)1 CreatePhraseSetRequest (com.google.cloud.speech.v1p1beta1.CreatePhraseSetRequest)1 CustomClass (com.google.cloud.speech.v1p1beta1.CustomClass)1 LocationName (com.google.cloud.speech.v1p1beta1.LocationName)1 PhraseSet (com.google.cloud.speech.v1p1beta1.PhraseSet)1 RecognitionAudio (com.google.cloud.speech.v1p1beta1.RecognitionAudio)1 RecognitionConfig (com.google.cloud.speech.v1p1beta1.RecognitionConfig)1 RecognizeResponse (com.google.cloud.speech.v1p1beta1.RecognizeResponse)1 SpeechAdaptation (com.google.cloud.speech.v1p1beta1.SpeechAdaptation)1 SpeechClient (com.google.cloud.speech.v1p1beta1.SpeechClient)1 SpeechRecognitionAlternative (com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative)1 SpeechRecognitionResult (com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult)1