|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.speech; |
| 18 | + |
| 19 | +// [START speech_adaptation_v2_custom_class_reference] |
| 20 | +import com.google.api.gax.longrunning.OperationFuture; |
| 21 | +import com.google.cloud.speech.v2.AutoDetectDecodingConfig; |
| 22 | +import com.google.cloud.speech.v2.CreateCustomClassRequest; |
| 23 | +import com.google.cloud.speech.v2.CreatePhraseSetRequest; |
| 24 | +import com.google.cloud.speech.v2.CustomClass; |
| 25 | +import com.google.cloud.speech.v2.CustomClass.ClassItem; |
| 26 | +import com.google.cloud.speech.v2.OperationMetadata; |
| 27 | +import com.google.cloud.speech.v2.PhraseSet; |
| 28 | +import com.google.cloud.speech.v2.PhraseSet.Phrase; |
| 29 | +import com.google.cloud.speech.v2.RecognitionConfig; |
| 30 | +import com.google.cloud.speech.v2.RecognizeRequest; |
| 31 | +import com.google.cloud.speech.v2.RecognizeResponse; |
| 32 | +import com.google.cloud.speech.v2.SpeechAdaptation; |
| 33 | +import com.google.cloud.speech.v2.SpeechAdaptation.AdaptationPhraseSet; |
| 34 | +import com.google.cloud.speech.v2.SpeechClient; |
| 35 | +import com.google.cloud.speech.v2.SpeechRecognitionAlternative; |
| 36 | +import com.google.cloud.speech.v2.SpeechRecognitionResult; |
| 37 | +import com.google.protobuf.ByteString; |
| 38 | +import java.io.IOException; |
| 39 | +import java.nio.file.Files; |
| 40 | +import java.nio.file.Path; |
| 41 | +import java.nio.file.Paths; |
| 42 | +import java.util.List; |
| 43 | +import java.util.concurrent.ExecutionException; |
| 44 | + |
| 45 | +public class AdaptationCustomClassReferenceV2 { |
| 46 | + public static void main(String[] args) throws IOException, InterruptedException, |
| 47 | + ExecutionException { |
| 48 | + String projectId = "my-project-id"; |
| 49 | + String recognizerName = "projects/[PROJECT_ID]/locations/global/recognizers/[RECOGNIZER_ID]"; |
| 50 | + String customClassId = "my-class-id"; |
| 51 | + String phraseSetId = "my-phrase-set-id"; |
| 52 | + String audioFilePath = "path/to/audiofile"; |
| 53 | + |
| 54 | + createCustomClassV2(projectId, recognizerName, customClassId, phraseSetId, audioFilePath); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + public static void createCustomClassV2(String projectId, String recognizerName, |
| 59 | + String customClassId, String phraseSetId, String audioFilePath) throws |
| 60 | + IOException, InterruptedException, ExecutionException { |
| 61 | + |
| 62 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 63 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 64 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 65 | + try (SpeechClient speechClient = SpeechClient.create()) { |
| 66 | + String parent = String.format("projects/%s/locations/global", projectId); |
| 67 | + |
| 68 | + // Create a persistent CustomClass to reference in phrases. |
| 69 | + ClassItem.Builder classItem = ClassItem.newBuilder() |
| 70 | + .setValue("Chromecast"); |
| 71 | + |
| 72 | + CustomClass.Builder customClassBuilder = CustomClass.newBuilder() |
| 73 | + .addItems(classItem); |
| 74 | + |
| 75 | + CreateCustomClassRequest createCustomClassRequest = CreateCustomClassRequest.newBuilder() |
| 76 | + .setParent(parent) |
| 77 | + .setCustomClassId(customClassId) |
| 78 | + .setCustomClass(customClassBuilder) |
| 79 | + .build(); |
| 80 | + |
| 81 | + OperationFuture<CustomClass, OperationMetadata> classOperation = |
| 82 | + speechClient.createCustomClassAsync(createCustomClassRequest); |
| 83 | + CustomClass customClass = classOperation.get(); |
| 84 | + |
| 85 | + // Create a persistent PhraseSet to reference in a recognition request |
| 86 | + Phrase.Builder phrase = Phrase.newBuilder() |
| 87 | + .setValue(String.format("${%s}", customClass.getName())) |
| 88 | + .setBoost(20); |
| 89 | + |
| 90 | + PhraseSet.Builder phraseSetBuilder = PhraseSet.newBuilder() |
| 91 | + .addPhrases(phrase); |
| 92 | + |
| 93 | + CreatePhraseSetRequest createPhraseSetRequest = CreatePhraseSetRequest.newBuilder() |
| 94 | + .setParent(parent) |
| 95 | + .setPhraseSetId(phraseSetId) |
| 96 | + .setPhraseSet(phraseSetBuilder) |
| 97 | + .build(); |
| 98 | + |
| 99 | + OperationFuture<PhraseSet, OperationMetadata> phraseOperation = |
| 100 | + speechClient.createPhraseSetAsync(createPhraseSetRequest); |
| 101 | + PhraseSet phraseSet = phraseOperation.get(); |
| 102 | + |
| 103 | + System.out.printf("Custom class name: %s\n", customClass.getName()); |
| 104 | + System.out.printf("Phrase set name: %s\n", phraseSet.getName()); |
| 105 | + |
| 106 | + // Transcribe audio using speech adaptation |
| 107 | + Path path = Paths.get(audioFilePath); |
| 108 | + byte[] data = Files.readAllBytes(path); |
| 109 | + ByteString audioBytes = ByteString.copyFrom(data); |
| 110 | + |
| 111 | + // Add a reference to the PhraseSet into the recognition request |
| 112 | + AdaptationPhraseSet.Builder adaptationPhraseSet = AdaptationPhraseSet.newBuilder() |
| 113 | + .setPhraseSet(phraseSet.getName()); |
| 114 | + |
| 115 | + SpeechAdaptation.Builder adaptation = SpeechAdaptation.newBuilder() |
| 116 | + .addPhraseSets(adaptationPhraseSet); |
| 117 | + |
| 118 | + RecognitionConfig recognitionConfig = RecognitionConfig.newBuilder() |
| 119 | + .setAutoDecodingConfig(AutoDetectDecodingConfig.newBuilder().build()) |
| 120 | + .setAdaptation(adaptation) |
| 121 | + .build(); |
| 122 | + |
| 123 | + RecognizeRequest request = RecognizeRequest.newBuilder() |
| 124 | + .setConfig(recognitionConfig) |
| 125 | + .setRecognizer(recognizerName) |
| 126 | + .setContent(audioBytes) |
| 127 | + .build(); |
| 128 | + |
| 129 | + RecognizeResponse response = speechClient.recognize(request); |
| 130 | + List<SpeechRecognitionResult> results = response.getResultsList(); |
| 131 | + |
| 132 | + for (SpeechRecognitionResult result : results) { |
| 133 | + // There can be several alternative transcripts for a given chunk of speech. Just use the |
| 134 | + // first (most likely) one here. |
| 135 | + SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0); |
| 136 | + System.out.printf("Transcription: %s%n", alternative.getTranscript()); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | +// [END speech_adaptation_v2_custom_class_reference] |
0 commit comments