From e06f4a33d4622d30b7724a4bf087d02ffee5cc43 Mon Sep 17 00:00:00 2001 From: Mike <45373284+munkhuushmgl@users.noreply.github.com> Date: Fri, 4 Jun 2021 12:31:55 -0600 Subject: [PATCH] samples: adds export to GCS sample (#544) * samples: adds export to GCS sample * fixed checkstyle * added missing depends in pom files --- speech/snippets/pom.xml | 4 + .../example/speech/ExportToStorageBeta.java | 90 +++++++++++++++++ .../speech/ExportToStorageBetaTest.java | 96 +++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 speech/snippets/src/main/java/com/example/speech/ExportToStorageBeta.java create mode 100644 speech/snippets/src/test/java/com/example/speech/ExportToStorageBetaTest.java diff --git a/speech/snippets/pom.xml b/speech/snippets/pom.xml index 4c52f580f61..95f095460cd 100644 --- a/speech/snippets/pom.xml +++ b/speech/snippets/pom.xml @@ -43,6 +43,10 @@ google-cloud-speech + + com.google.cloud + google-cloud-storage + commons-cli commons-cli diff --git a/speech/snippets/src/main/java/com/example/speech/ExportToStorageBeta.java b/speech/snippets/src/main/java/com/example/speech/ExportToStorageBeta.java new file mode 100644 index 00000000000..7c60725924b --- /dev/null +++ b/speech/snippets/src/main/java/com/example/speech/ExportToStorageBeta.java @@ -0,0 +1,90 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.speech; + +// [START speech_export_to_gcs] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata; +import com.google.cloud.speech.v1p1beta1.LongRunningRecognizeRequest; +import com.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse; +import com.google.cloud.speech.v1p1beta1.RecognitionAudio; +import com.google.cloud.speech.v1p1beta1.RecognitionConfig; +import com.google.cloud.speech.v1p1beta1.RecognitionConfig.AudioEncoding; +import com.google.cloud.speech.v1p1beta1.SpeechClient; +import com.google.cloud.speech.v1p1beta1.TranscriptOutputConfig; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; + +public class ExportToStorageBeta { + + public static void main(String[] args) throws Exception { + String inputUri = "gs://YOUR_BUCKET_ID/path/to/your/audio_file.wav"; + String outputStorageUri = "gs://YOUR_BUCKET_ID/output_dir_prefix/"; + String encoding = "LINEAR16"; // encoding of the audio + int sampleRateHertz = 8000; + String languageCode = "en-US"; // language code BCP-47_LANGUAGE_CODE_OF_AUDIO + exportToStorage(inputUri, outputStorageUri, encoding, sampleRateHertz, languageCode); + } + + // Exports the recognized output to specified GCS destination. + public static void exportToStorage( + String inputUri, String outputStorageUri, String encoding, int sampleRateHertz, + String languageCode) + throws IOException, ExecutionException, InterruptedException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SpeechClient speechClient = SpeechClient.create()) { + RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(inputUri).build(); + + AudioEncoding audioEncoding = AudioEncoding.valueOf(encoding); + + // Pass in the URI of the Cloud Storage bucket to hold the transcription + TranscriptOutputConfig outputConfig = TranscriptOutputConfig.newBuilder() + .setGcsUri(outputStorageUri).build(); + + RecognitionConfig config = RecognitionConfig.newBuilder() + .setEncoding(audioEncoding) + .setSampleRateHertz(sampleRateHertz) + .setLanguageCode(languageCode) + .build(); + + LongRunningRecognizeRequest request = + LongRunningRecognizeRequest.newBuilder() + .setConfig(config) + .setAudio(audio) + .setOutputConfig(outputConfig) + .build(); + + OperationFuture future = + speechClient.longRunningRecognizeAsync(request); + + System.out.println("Waiting for operation to complete..."); + LongRunningRecognizeResponse response = future.get(); + + System.out.println("Results saved to specified output Cloud Storage bucket."); + + String output = response.getResultsList().stream().map( + result -> String.valueOf(result.getAlternatives(0).getTranscript())) + .collect(Collectors.joining("\n")); + System.out.printf("Transcription: %s", output); + } + } +} +// [END speech_export_to_gcs] diff --git a/speech/snippets/src/test/java/com/example/speech/ExportToStorageBetaTest.java b/speech/snippets/src/test/java/com/example/speech/ExportToStorageBetaTest.java new file mode 100644 index 00000000000..64d2f5bc505 --- /dev/null +++ b/speech/snippets/src/test/java/com/example/speech/ExportToStorageBetaTest.java @@ -0,0 +1,96 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.speech; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ExportToStorageBetaTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String AUDIO_STORAGE_URI = + "gs://cloud-samples-data/speech/commercial_mono.wav"; + private static final String PREFIX = "EXPORT_TEST_OUTPUTS"; + private static final String OUTPUT_STORAGE_URI = + String.format("gs://%s/%s/%s/", PROJECT_ID, PREFIX, UUID.randomUUID()); + private static final String ENCODING = "LINEAR16"; + private static final String LANGUAGE_CODE = "en-US"; + + private static final int SAMPLE_RATE_HERTZ = 8000; + + private ByteArrayOutputStream bout; + private PrintStream originalPrintStream; + private PrintStream out; + + private static void cleanUpBucket() { + Storage storage = StorageOptions.getDefaultInstance().getService(); + Page blobs = + storage.list( + PROJECT_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix(PREFIX)); + + deleteDirectory(storage, blobs); + } + + private static void deleteDirectory(Storage storage, Page blobs) { + for (Blob blob : blobs.iterateAll()) { + if (!blob.delete()) { + Page subBlobs = + storage.list( + PROJECT_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix(blob.getName())); + + deleteDirectory(storage, subBlobs); + } + } + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + cleanUpBucket(); + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testExportToStorageBeta() throws Exception { + ExportToStorageBeta.exportToStorage( + AUDIO_STORAGE_URI, OUTPUT_STORAGE_URI, ENCODING, SAMPLE_RATE_HERTZ, LANGUAGE_CODE); + String got = bout.toString(); + assertThat(got).contains("Transcription:"); + } +}