Skip to content

Commit 70d1b1b

Browse files
yoshi-automationchingor13
authored andcommitted
fix: update retry configs, adds generated samples (#26)
0 parents  commit 70d1b1b

20 files changed

+2115
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2019 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+
* https://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+
// DO NOT EDIT! This is a generated sample ("LongRunningRequestAsync", "speech_transcribe_async")
17+
// sample-metadata:
18+
// title: Transcribe Audio File using Long Running Operation (Local File) (LRO)
19+
// description: Transcribe a long audio file using asynchronous speech recognition
20+
// usage: gradle run -PmainClass=com.google.cloud.examples.speech.v1.SpeechTranscribeAsync [--args='[--local_file_path "resources/brooklyn_bridge.raw"]']
21+
22+
package com.google.cloud.examples.speech.v1;
23+
24+
import com.google.api.gax.longrunning.OperationFuture;
25+
import com.google.cloud.speech.v1.LongRunningRecognizeMetadata;
26+
import com.google.cloud.speech.v1.LongRunningRecognizeRequest;
27+
import com.google.cloud.speech.v1.LongRunningRecognizeResponse;
28+
import com.google.cloud.speech.v1.RecognitionAudio;
29+
import com.google.cloud.speech.v1.RecognitionConfig;
30+
import com.google.cloud.speech.v1.SpeechClient;
31+
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
32+
import com.google.cloud.speech.v1.SpeechRecognitionResult;
33+
import com.google.protobuf.ByteString;
34+
import java.nio.file.Files;
35+
import java.nio.file.Path;
36+
import java.nio.file.Paths;
37+
import org.apache.commons.cli.CommandLine;
38+
import org.apache.commons.cli.DefaultParser;
39+
import org.apache.commons.cli.Option;
40+
import org.apache.commons.cli.Options;
41+
42+
public class SpeechTranscribeAsync {
43+
// [START speech_transcribe_async]
44+
/*
45+
* Please include the following imports to run this sample.
46+
*
47+
* import com.google.api.gax.longrunning.OperationFuture;
48+
* import com.google.cloud.speech.v1.LongRunningRecognizeMetadata;
49+
* import com.google.cloud.speech.v1.LongRunningRecognizeRequest;
50+
* import com.google.cloud.speech.v1.LongRunningRecognizeResponse;
51+
* import com.google.cloud.speech.v1.RecognitionAudio;
52+
* import com.google.cloud.speech.v1.RecognitionConfig;
53+
* import com.google.cloud.speech.v1.SpeechClient;
54+
* import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
55+
* import com.google.cloud.speech.v1.SpeechRecognitionResult;
56+
* import com.google.protobuf.ByteString;
57+
* import java.nio.file.Files;
58+
* import java.nio.file.Path;
59+
* import java.nio.file.Paths;
60+
*/
61+
62+
/**
63+
* Transcribe a long audio file using asynchronous speech recognition
64+
*
65+
* @param localFilePath Path to local audio file, e.g. /path/audio.wav
66+
*/
67+
public static void sampleLongRunningRecognize(String localFilePath) {
68+
try (SpeechClient speechClient = SpeechClient.create()) {
69+
// localFilePath = "resources/brooklyn_bridge.raw";
70+
71+
// The language of the supplied audio
72+
String languageCode = "en-US";
73+
74+
// Sample rate in Hertz of the audio data sent
75+
int sampleRateHertz = 16000;
76+
77+
// Encoding of audio data sent. This sample sets this explicitly.
78+
// This field is optional for FLAC and WAV audio formats.
79+
RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.LINEAR16;
80+
RecognitionConfig config =
81+
RecognitionConfig.newBuilder()
82+
.setLanguageCode(languageCode)
83+
.setSampleRateHertz(sampleRateHertz)
84+
.setEncoding(encoding)
85+
.build();
86+
Path path = Paths.get(localFilePath);
87+
byte[] data = Files.readAllBytes(path);
88+
ByteString content = ByteString.copyFrom(data);
89+
RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(content).build();
90+
LongRunningRecognizeRequest request =
91+
LongRunningRecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build();
92+
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> future =
93+
speechClient.longRunningRecognizeAsync(request);
94+
95+
System.out.println("Waiting for operation to complete...");
96+
LongRunningRecognizeResponse response = future.get();
97+
for (SpeechRecognitionResult result : response.getResultsList()) {
98+
// First alternative is the most probable result
99+
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
100+
System.out.printf("Transcript: %s\n", alternative.getTranscript());
101+
}
102+
} catch (Exception exception) {
103+
System.err.println("Failed to create the client due to: " + exception);
104+
}
105+
}
106+
// [END speech_transcribe_async]
107+
108+
public static void main(String[] args) throws Exception {
109+
Options options = new Options();
110+
options.addOption(
111+
Option.builder("").required(false).hasArg(true).longOpt("local_file_path").build());
112+
113+
CommandLine cl = (new DefaultParser()).parse(options, args);
114+
String localFilePath = cl.getOptionValue("local_file_path", "resources/brooklyn_bridge.raw");
115+
116+
sampleLongRunningRecognize(localFilePath);
117+
}
118+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2019 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+
* https://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+
// DO NOT EDIT! This is a generated sample ("LongRunningRequestAsync", "speech_transcribe_async_gcs")
17+
// sample-metadata:
18+
// title: Transcript Audio File using Long Running Operation (Cloud Storage) (LRO)
19+
// description: Transcribe long audio file from Cloud Storage using asynchronous speech recognition
20+
// usage: gradle run -PmainClass=com.google.cloud.examples.speech.v1.SpeechTranscribeAsyncGcs [--args='[--storage_uri "gs://cloud-samples-data/speech/brooklyn_bridge.raw"]']
21+
22+
package com.google.cloud.examples.speech.v1;
23+
24+
import com.google.api.gax.longrunning.OperationFuture;
25+
import com.google.cloud.speech.v1.LongRunningRecognizeMetadata;
26+
import com.google.cloud.speech.v1.LongRunningRecognizeRequest;
27+
import com.google.cloud.speech.v1.LongRunningRecognizeResponse;
28+
import com.google.cloud.speech.v1.RecognitionAudio;
29+
import com.google.cloud.speech.v1.RecognitionConfig;
30+
import com.google.cloud.speech.v1.SpeechClient;
31+
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
32+
import com.google.cloud.speech.v1.SpeechRecognitionResult;
33+
import org.apache.commons.cli.CommandLine;
34+
import org.apache.commons.cli.DefaultParser;
35+
import org.apache.commons.cli.Option;
36+
import org.apache.commons.cli.Options;
37+
38+
public class SpeechTranscribeAsyncGcs {
39+
// [START speech_transcribe_async_gcs]
40+
/*
41+
* Please include the following imports to run this sample.
42+
*
43+
* import com.google.api.gax.longrunning.OperationFuture;
44+
* import com.google.cloud.speech.v1.LongRunningRecognizeMetadata;
45+
* import com.google.cloud.speech.v1.LongRunningRecognizeRequest;
46+
* import com.google.cloud.speech.v1.LongRunningRecognizeResponse;
47+
* import com.google.cloud.speech.v1.RecognitionAudio;
48+
* import com.google.cloud.speech.v1.RecognitionConfig;
49+
* import com.google.cloud.speech.v1.SpeechClient;
50+
* import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
51+
* import com.google.cloud.speech.v1.SpeechRecognitionResult;
52+
*/
53+
54+
/**
55+
* Transcribe long audio file from Cloud Storage using asynchronous speech recognition
56+
*
57+
* @param storageUri URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE]
58+
*/
59+
public static void sampleLongRunningRecognize(String storageUri) {
60+
try (SpeechClient speechClient = SpeechClient.create()) {
61+
// storageUri = "gs://cloud-samples-data/speech/brooklyn_bridge.raw";
62+
63+
// Sample rate in Hertz of the audio data sent
64+
int sampleRateHertz = 16000;
65+
66+
// The language of the supplied audio
67+
String languageCode = "en-US";
68+
69+
// Encoding of audio data sent. This sample sets this explicitly.
70+
// This field is optional for FLAC and WAV audio formats.
71+
RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.LINEAR16;
72+
RecognitionConfig config =
73+
RecognitionConfig.newBuilder()
74+
.setSampleRateHertz(sampleRateHertz)
75+
.setLanguageCode(languageCode)
76+
.setEncoding(encoding)
77+
.build();
78+
RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(storageUri).build();
79+
LongRunningRecognizeRequest request =
80+
LongRunningRecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build();
81+
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> future =
82+
speechClient.longRunningRecognizeAsync(request);
83+
84+
System.out.println("Waiting for operation to complete...");
85+
LongRunningRecognizeResponse response = future.get();
86+
for (SpeechRecognitionResult result : response.getResultsList()) {
87+
// First alternative is the most probable result
88+
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
89+
System.out.printf("Transcript: %s\n", alternative.getTranscript());
90+
}
91+
} catch (Exception exception) {
92+
System.err.println("Failed to create the client due to: " + exception);
93+
}
94+
}
95+
// [END speech_transcribe_async_gcs]
96+
97+
public static void main(String[] args) throws Exception {
98+
Options options = new Options();
99+
options.addOption(
100+
Option.builder("").required(false).hasArg(true).longOpt("storage_uri").build());
101+
102+
CommandLine cl = (new DefaultParser()).parse(options, args);
103+
String storageUri =
104+
cl.getOptionValue("storage_uri", "gs://cloud-samples-data/speech/brooklyn_bridge.raw");
105+
106+
sampleLongRunningRecognize(storageUri);
107+
}
108+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2019 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+
* https://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+
// DO NOT EDIT! This is a generated sample ("LongRunningRequestAsync", "speech_transcribe_async_word_time_offsets_gcs")
17+
// sample-metadata:
18+
// title: Getting word timestamps (Cloud Storage) (LRO)
19+
// description: Print start and end time of each word spoken in audio file from Cloud Storage
20+
// usage: gradle run -PmainClass=com.google.cloud.examples.speech.v1.SpeechTranscribeAsyncWordTimeOffsetsGcs [--args='[--storage_uri "gs://cloud-samples-data/speech/brooklyn_bridge.flac"]']
21+
22+
package com.google.cloud.examples.speech.v1;
23+
24+
import com.google.api.gax.longrunning.OperationFuture;
25+
import com.google.cloud.speech.v1.LongRunningRecognizeMetadata;
26+
import com.google.cloud.speech.v1.LongRunningRecognizeRequest;
27+
import com.google.cloud.speech.v1.LongRunningRecognizeResponse;
28+
import com.google.cloud.speech.v1.RecognitionAudio;
29+
import com.google.cloud.speech.v1.RecognitionConfig;
30+
import com.google.cloud.speech.v1.SpeechClient;
31+
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
32+
import com.google.cloud.speech.v1.SpeechRecognitionResult;
33+
import com.google.cloud.speech.v1.WordInfo;
34+
import org.apache.commons.cli.CommandLine;
35+
import org.apache.commons.cli.DefaultParser;
36+
import org.apache.commons.cli.Option;
37+
import org.apache.commons.cli.Options;
38+
39+
public class SpeechTranscribeAsyncWordTimeOffsetsGcs {
40+
// [START speech_transcribe_async_word_time_offsets_gcs]
41+
/*
42+
* Please include the following imports to run this sample.
43+
*
44+
* import com.google.api.gax.longrunning.OperationFuture;
45+
* import com.google.cloud.speech.v1.LongRunningRecognizeMetadata;
46+
* import com.google.cloud.speech.v1.LongRunningRecognizeRequest;
47+
* import com.google.cloud.speech.v1.LongRunningRecognizeResponse;
48+
* import com.google.cloud.speech.v1.RecognitionAudio;
49+
* import com.google.cloud.speech.v1.RecognitionConfig;
50+
* import com.google.cloud.speech.v1.SpeechClient;
51+
* import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
52+
* import com.google.cloud.speech.v1.SpeechRecognitionResult;
53+
* import com.google.cloud.speech.v1.WordInfo;
54+
*/
55+
56+
/**
57+
* Print start and end time of each word spoken in audio file from Cloud Storage
58+
*
59+
* @param storageUri URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE]
60+
*/
61+
public static void sampleLongRunningRecognize(String storageUri) {
62+
try (SpeechClient speechClient = SpeechClient.create()) {
63+
// storageUri = "gs://cloud-samples-data/speech/brooklyn_bridge.flac";
64+
65+
// When enabled, the first result returned by the API will include a list
66+
// of words and the start and end time offsets (timestamps) for those words.
67+
boolean enableWordTimeOffsets = true;
68+
69+
// The language of the supplied audio
70+
String languageCode = "en-US";
71+
RecognitionConfig config =
72+
RecognitionConfig.newBuilder()
73+
.setEnableWordTimeOffsets(enableWordTimeOffsets)
74+
.setLanguageCode(languageCode)
75+
.build();
76+
RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(storageUri).build();
77+
LongRunningRecognizeRequest request =
78+
LongRunningRecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build();
79+
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> future =
80+
speechClient.longRunningRecognizeAsync(request);
81+
82+
System.out.println("Waiting for operation to complete...");
83+
LongRunningRecognizeResponse response = future.get();
84+
// The first result includes start and end time word offsets
85+
SpeechRecognitionResult result = response.getResultsList().get(0);
86+
// First alternative is the most probable result
87+
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
88+
System.out.printf("Transcript: %s\n", alternative.getTranscript());
89+
// Print the start and end time of each word
90+
for (WordInfo word : alternative.getWordsList()) {
91+
System.out.printf("Word: %s\n", word.getWord());
92+
System.out.printf(
93+
"Start time: %s seconds %s nanos\n",
94+
word.getStartTime().getSeconds(), word.getStartTime().getNanos());
95+
System.out.printf(
96+
"End time: %s seconds %s nanos\n",
97+
word.getEndTime().getSeconds(), word.getEndTime().getNanos());
98+
}
99+
} catch (Exception exception) {
100+
System.err.println("Failed to create the client due to: " + exception);
101+
}
102+
}
103+
// [END speech_transcribe_async_word_time_offsets_gcs]
104+
105+
public static void main(String[] args) throws Exception {
106+
Options options = new Options();
107+
options.addOption(
108+
Option.builder("").required(false).hasArg(true).longOpt("storage_uri").build());
109+
110+
CommandLine cl = (new DefaultParser()).parse(options, args);
111+
String storageUri =
112+
cl.getOptionValue("storage_uri", "gs://cloud-samples-data/speech/brooklyn_bridge.flac");
113+
114+
sampleLongRunningRecognize(storageUri);
115+
}
116+
}

0 commit comments

Comments
 (0)