Skip to content

Commit

Permalink
fix: update retry configs, adds generated samples (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshi-automation authored and chingor13 committed Oct 18, 2019
0 parents commit 70d1b1b
Show file tree
Hide file tree
Showing 20 changed files with 2,115 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2019 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
*
* https://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.
*/
// DO NOT EDIT! This is a generated sample ("LongRunningRequestAsync", "speech_transcribe_async")
// sample-metadata:
// title: Transcribe Audio File using Long Running Operation (Local File) (LRO)
// description: Transcribe a long audio file using asynchronous speech recognition
// usage: gradle run -PmainClass=com.google.cloud.examples.speech.v1.SpeechTranscribeAsync [--args='[--local_file_path "resources/brooklyn_bridge.raw"]']

package com.google.cloud.examples.speech.v1;

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.speech.v1.LongRunningRecognizeMetadata;
import com.google.cloud.speech.v1.LongRunningRecognizeRequest;
import com.google.cloud.speech.v1.LongRunningRecognizeResponse;
import com.google.cloud.speech.v1.RecognitionAudio;
import com.google.cloud.speech.v1.RecognitionConfig;
import com.google.cloud.speech.v1.SpeechClient;
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
import com.google.cloud.speech.v1.SpeechRecognitionResult;
import com.google.protobuf.ByteString;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

public class SpeechTranscribeAsync {
// [START speech_transcribe_async]
/*
* Please include the following imports to run this sample.
*
* import com.google.api.gax.longrunning.OperationFuture;
* import com.google.cloud.speech.v1.LongRunningRecognizeMetadata;
* import com.google.cloud.speech.v1.LongRunningRecognizeRequest;
* import com.google.cloud.speech.v1.LongRunningRecognizeResponse;
* import com.google.cloud.speech.v1.RecognitionAudio;
* import com.google.cloud.speech.v1.RecognitionConfig;
* import com.google.cloud.speech.v1.SpeechClient;
* import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
* import com.google.cloud.speech.v1.SpeechRecognitionResult;
* import com.google.protobuf.ByteString;
* import java.nio.file.Files;
* import java.nio.file.Path;
* import java.nio.file.Paths;
*/

/**
* Transcribe a long audio file using asynchronous speech recognition
*
* @param localFilePath Path to local audio file, e.g. /path/audio.wav
*/
public static void sampleLongRunningRecognize(String localFilePath) {
try (SpeechClient speechClient = SpeechClient.create()) {
// localFilePath = "resources/brooklyn_bridge.raw";

// The language of the supplied audio
String languageCode = "en-US";

// Sample rate in Hertz of the audio data sent
int sampleRateHertz = 16000;

// Encoding of audio data sent. This sample sets this explicitly.
// This field is optional for FLAC and WAV audio formats.
RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.LINEAR16;
RecognitionConfig config =
RecognitionConfig.newBuilder()
.setLanguageCode(languageCode)
.setSampleRateHertz(sampleRateHertz)
.setEncoding(encoding)
.build();
Path path = Paths.get(localFilePath);
byte[] data = Files.readAllBytes(path);
ByteString content = ByteString.copyFrom(data);
RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(content).build();
LongRunningRecognizeRequest request =
LongRunningRecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build();
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> future =
speechClient.longRunningRecognizeAsync(request);

System.out.println("Waiting for operation to complete...");
LongRunningRecognizeResponse response = future.get();
for (SpeechRecognitionResult result : response.getResultsList()) {
// First alternative is the most probable result
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcript: %s\n", alternative.getTranscript());
}
} catch (Exception exception) {
System.err.println("Failed to create the client due to: " + exception);
}
}
// [END speech_transcribe_async]

public static void main(String[] args) throws Exception {
Options options = new Options();
options.addOption(
Option.builder("").required(false).hasArg(true).longOpt("local_file_path").build());

CommandLine cl = (new DefaultParser()).parse(options, args);
String localFilePath = cl.getOptionValue("local_file_path", "resources/brooklyn_bridge.raw");

sampleLongRunningRecognize(localFilePath);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright 2019 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
*
* https://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.
*/
// DO NOT EDIT! This is a generated sample ("LongRunningRequestAsync", "speech_transcribe_async_gcs")
// sample-metadata:
// title: Transcript Audio File using Long Running Operation (Cloud Storage) (LRO)
// description: Transcribe long audio file from Cloud Storage using asynchronous speech recognition
// usage: gradle run -PmainClass=com.google.cloud.examples.speech.v1.SpeechTranscribeAsyncGcs [--args='[--storage_uri "gs://cloud-samples-data/speech/brooklyn_bridge.raw"]']

package com.google.cloud.examples.speech.v1;

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.speech.v1.LongRunningRecognizeMetadata;
import com.google.cloud.speech.v1.LongRunningRecognizeRequest;
import com.google.cloud.speech.v1.LongRunningRecognizeResponse;
import com.google.cloud.speech.v1.RecognitionAudio;
import com.google.cloud.speech.v1.RecognitionConfig;
import com.google.cloud.speech.v1.SpeechClient;
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
import com.google.cloud.speech.v1.SpeechRecognitionResult;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

public class SpeechTranscribeAsyncGcs {
// [START speech_transcribe_async_gcs]
/*
* Please include the following imports to run this sample.
*
* import com.google.api.gax.longrunning.OperationFuture;
* import com.google.cloud.speech.v1.LongRunningRecognizeMetadata;
* import com.google.cloud.speech.v1.LongRunningRecognizeRequest;
* import com.google.cloud.speech.v1.LongRunningRecognizeResponse;
* import com.google.cloud.speech.v1.RecognitionAudio;
* import com.google.cloud.speech.v1.RecognitionConfig;
* import com.google.cloud.speech.v1.SpeechClient;
* import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
* import com.google.cloud.speech.v1.SpeechRecognitionResult;
*/

/**
* Transcribe long audio file from Cloud Storage using asynchronous speech recognition
*
* @param storageUri URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE]
*/
public static void sampleLongRunningRecognize(String storageUri) {
try (SpeechClient speechClient = SpeechClient.create()) {
// storageUri = "gs://cloud-samples-data/speech/brooklyn_bridge.raw";

// Sample rate in Hertz of the audio data sent
int sampleRateHertz = 16000;

// The language of the supplied audio
String languageCode = "en-US";

// Encoding of audio data sent. This sample sets this explicitly.
// This field is optional for FLAC and WAV audio formats.
RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.LINEAR16;
RecognitionConfig config =
RecognitionConfig.newBuilder()
.setSampleRateHertz(sampleRateHertz)
.setLanguageCode(languageCode)
.setEncoding(encoding)
.build();
RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(storageUri).build();
LongRunningRecognizeRequest request =
LongRunningRecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build();
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> future =
speechClient.longRunningRecognizeAsync(request);

System.out.println("Waiting for operation to complete...");
LongRunningRecognizeResponse response = future.get();
for (SpeechRecognitionResult result : response.getResultsList()) {
// First alternative is the most probable result
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcript: %s\n", alternative.getTranscript());
}
} catch (Exception exception) {
System.err.println("Failed to create the client due to: " + exception);
}
}
// [END speech_transcribe_async_gcs]

public static void main(String[] args) throws Exception {
Options options = new Options();
options.addOption(
Option.builder("").required(false).hasArg(true).longOpt("storage_uri").build());

CommandLine cl = (new DefaultParser()).parse(options, args);
String storageUri =
cl.getOptionValue("storage_uri", "gs://cloud-samples-data/speech/brooklyn_bridge.raw");

sampleLongRunningRecognize(storageUri);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright 2019 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
*
* https://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.
*/
// DO NOT EDIT! This is a generated sample ("LongRunningRequestAsync", "speech_transcribe_async_word_time_offsets_gcs")
// sample-metadata:
// title: Getting word timestamps (Cloud Storage) (LRO)
// description: Print start and end time of each word spoken in audio file from Cloud Storage
// usage: gradle run -PmainClass=com.google.cloud.examples.speech.v1.SpeechTranscribeAsyncWordTimeOffsetsGcs [--args='[--storage_uri "gs://cloud-samples-data/speech/brooklyn_bridge.flac"]']

package com.google.cloud.examples.speech.v1;

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.speech.v1.LongRunningRecognizeMetadata;
import com.google.cloud.speech.v1.LongRunningRecognizeRequest;
import com.google.cloud.speech.v1.LongRunningRecognizeResponse;
import com.google.cloud.speech.v1.RecognitionAudio;
import com.google.cloud.speech.v1.RecognitionConfig;
import com.google.cloud.speech.v1.SpeechClient;
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
import com.google.cloud.speech.v1.SpeechRecognitionResult;
import com.google.cloud.speech.v1.WordInfo;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

public class SpeechTranscribeAsyncWordTimeOffsetsGcs {
// [START speech_transcribe_async_word_time_offsets_gcs]
/*
* Please include the following imports to run this sample.
*
* import com.google.api.gax.longrunning.OperationFuture;
* import com.google.cloud.speech.v1.LongRunningRecognizeMetadata;
* import com.google.cloud.speech.v1.LongRunningRecognizeRequest;
* import com.google.cloud.speech.v1.LongRunningRecognizeResponse;
* import com.google.cloud.speech.v1.RecognitionAudio;
* import com.google.cloud.speech.v1.RecognitionConfig;
* import com.google.cloud.speech.v1.SpeechClient;
* import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
* import com.google.cloud.speech.v1.SpeechRecognitionResult;
* import com.google.cloud.speech.v1.WordInfo;
*/

/**
* Print start and end time of each word spoken in audio file from Cloud Storage
*
* @param storageUri URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE]
*/
public static void sampleLongRunningRecognize(String storageUri) {
try (SpeechClient speechClient = SpeechClient.create()) {
// storageUri = "gs://cloud-samples-data/speech/brooklyn_bridge.flac";

// When enabled, the first result returned by the API will include a list
// of words and the start and end time offsets (timestamps) for those words.
boolean enableWordTimeOffsets = true;

// The language of the supplied audio
String languageCode = "en-US";
RecognitionConfig config =
RecognitionConfig.newBuilder()
.setEnableWordTimeOffsets(enableWordTimeOffsets)
.setLanguageCode(languageCode)
.build();
RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(storageUri).build();
LongRunningRecognizeRequest request =
LongRunningRecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build();
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> future =
speechClient.longRunningRecognizeAsync(request);

System.out.println("Waiting for operation to complete...");
LongRunningRecognizeResponse response = future.get();
// The first result includes start and end time word offsets
SpeechRecognitionResult result = response.getResultsList().get(0);
// First alternative is the most probable result
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcript: %s\n", alternative.getTranscript());
// Print the start and end time of each word
for (WordInfo word : alternative.getWordsList()) {
System.out.printf("Word: %s\n", word.getWord());
System.out.printf(
"Start time: %s seconds %s nanos\n",
word.getStartTime().getSeconds(), word.getStartTime().getNanos());
System.out.printf(
"End time: %s seconds %s nanos\n",
word.getEndTime().getSeconds(), word.getEndTime().getNanos());
}
} catch (Exception exception) {
System.err.println("Failed to create the client due to: " + exception);
}
}
// [END speech_transcribe_async_word_time_offsets_gcs]

public static void main(String[] args) throws Exception {
Options options = new Options();
options.addOption(
Option.builder("").required(false).hasArg(true).longOpt("storage_uri").build());

CommandLine cl = (new DefaultParser()).parse(options, args);
String storageUri =
cl.getOptionValue("storage_uri", "gs://cloud-samples-data/speech/brooklyn_bridge.flac");

sampleLongRunningRecognize(storageUri);
}
}
Loading

0 comments on commit 70d1b1b

Please sign in to comment.