Skip to content

Commit 703ac9c

Browse files
author
Jerjou Cheng
committed
Make each speech sample independent
1 parent 409b024 commit 703ac9c

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

speech/grpc/src/main/java/com/examples/cloud/speech/StreamingRecognizeClient.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.apache.log4j.ConsoleAppender.SYSTEM_OUT;
2020

21+
import com.google.auth.oauth2.GoogleCredentials;
2122
import com.google.cloud.speech.v1beta1.RecognitionConfig;
2223
import com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding;
2324
import com.google.cloud.speech.v1beta1.SpeechGrpc;
@@ -28,6 +29,8 @@
2829
import com.google.protobuf.TextFormat;
2930

3031
import io.grpc.ManagedChannel;
32+
import io.grpc.ManagedChannelBuilder;
33+
import io.grpc.auth.ClientAuthInterceptor;
3134
import io.grpc.stub.StreamObserver;
3235

3336
import org.apache.commons.cli.CommandLine;
@@ -47,6 +50,7 @@
4750
import java.util.Arrays;
4851
import java.util.List;
4952
import java.util.concurrent.CountDownLatch;
53+
import java.util.concurrent.Executors;
5054
import java.util.concurrent.TimeUnit;
5155

5256

@@ -84,7 +88,7 @@ public StreamingRecognizeClient(ManagedChannel channel, String file, int samplin
8488
//Send log4j logs to Console
8589
//If you are going to run this on GCE, you might wish to integrate with gcloud-java logging.
8690
//See https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/README.md#stackdriver-logging-alpha
87-
91+
8892
ConsoleAppender appender = new ConsoleAppender(new SimpleLayout(), SYSTEM_OUT);
8993
logger.addAppender(appender);
9094
}
@@ -93,6 +97,17 @@ public void shutdown() throws InterruptedException {
9397
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
9498
}
9599

100+
static ManagedChannel createChannel(String host, int port) throws IOException {
101+
GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
102+
creds = creds.createScoped(OAUTH2_SCOPES);
103+
ManagedChannel channel =
104+
ManagedChannelBuilder.forAddress(host, port)
105+
.intercept(new ClientAuthInterceptor(creds, Executors.newSingleThreadExecutor()))
106+
.build();
107+
108+
return channel;
109+
}
110+
96111
/** Send streaming recognize requests to server. */
97112
public void recognize() throws InterruptedException, IOException {
98113
final CountDownLatch finishLatch = new CountDownLatch(1);
@@ -242,7 +257,7 @@ public static void main(String[] args) throws Exception {
242257
System.exit(1);
243258
}
244259

245-
ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port);
260+
ManagedChannel channel = createChannel(host, port);
246261
StreamingRecognizeClient client = new StreamingRecognizeClient(channel, audioFile, sampling);
247262
try {
248263
client.recognize();

speech/grpc/src/main/java/com/examples/cloud/speech/SyncRecognizeClient.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.examples.cloud.speech;
1818

19+
import com.google.auth.oauth2.GoogleCredentials;
1920
import com.google.cloud.speech.v1beta1.RecognitionAudio;
2021
import com.google.cloud.speech.v1beta1.RecognitionConfig;
2122
import com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding;
@@ -25,7 +26,9 @@
2526
import com.google.protobuf.TextFormat;
2627

2728
import io.grpc.ManagedChannel;
29+
import io.grpc.ManagedChannelBuilder;
2830
import io.grpc.StatusRuntimeException;
31+
import io.grpc.auth.ClientAuthInterceptor;
2932

3033
import org.apache.commons.cli.CommandLine;
3134
import org.apache.commons.cli.CommandLineParser;
@@ -38,6 +41,7 @@
3841
import java.net.URI;
3942
import java.util.Arrays;
4043
import java.util.List;
44+
import java.util.concurrent.Executors;
4145
import java.util.concurrent.TimeUnit;
4246
import java.util.logging.Level;
4347
import java.util.logging.Logger;
@@ -49,15 +53,15 @@ public class SyncRecognizeClient {
4953

5054
private static final Logger logger = Logger.getLogger(SyncRecognizeClient.class.getName());
5155

52-
private static final List<String> OAUTH2_SCOPES =
53-
Arrays.asList("https://www.googleapis.com/auth/cloud-platform");
54-
5556
private final URI input;
5657
private final int samplingRate;
5758

5859
private final ManagedChannel channel;
5960
private final SpeechGrpc.SpeechBlockingStub speechClient;
6061

62+
private static final List<String> OAUTH2_SCOPES =
63+
Arrays.asList("https://www.googleapis.com/auth/cloud-platform");
64+
6165
/**
6266
* Construct client connecting to Cloud Speech server at {@code host:port}.
6367
*/
@@ -78,6 +82,17 @@ public void shutdown() throws InterruptedException {
7882
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
7983
}
8084

85+
static ManagedChannel createChannel(String host, int port) throws IOException {
86+
GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
87+
creds = creds.createScoped(OAUTH2_SCOPES);
88+
ManagedChannel channel =
89+
ManagedChannelBuilder.forAddress(host, port)
90+
.intercept(new ClientAuthInterceptor(creds, Executors.newSingleThreadExecutor()))
91+
.build();
92+
93+
return channel;
94+
}
95+
8196
/** Send a non-streaming-recognize request to server. */
8297
public void recognize() {
8398
RecognitionAudio audio;
@@ -179,7 +194,7 @@ public static void main(String[] args) throws Exception {
179194
System.exit(1);
180195
}
181196

182-
ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port);
197+
ManagedChannel channel = createChannel(host, port);
183198
SyncRecognizeClient client = new SyncRecognizeClient(channel, URI.create(audioFile), sampling);
184199
try {
185200
client.recognize();

speech/grpc/src/test/java/com/examples/cloud/speech/StreamingRecognizeClientTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public void test16KHzAudio() throws InterruptedException, IOException {
6464

6565
String host = "speech.googleapis.com";
6666
int port = 443;
67-
ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port);
67+
ManagedChannel channel = StreamingRecognizeClient.createChannel(host, port);
6868
StreamingRecognizeClient client = new StreamingRecognizeClient(channel, path.toString(), 16000);
69-
69+
7070
client.recognize();
7171
assertThat(writer.toString()).contains("transcript: \"how old is the Brooklyn Bridge\"");
7272
}
@@ -78,7 +78,7 @@ public void test32KHzAudio() throws InterruptedException, IOException {
7878

7979
String host = "speech.googleapis.com";
8080
int port = 443;
81-
ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port);
81+
ManagedChannel channel = StreamingRecognizeClient.createChannel(host, port);
8282
StreamingRecognizeClient client = new StreamingRecognizeClient(channel, path.toString(), 32000);
8383

8484
client.recognize();

0 commit comments

Comments
 (0)