Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make each speech sample independent #357

Merged
merged 1 commit into from
Oct 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.speech.v1beta1.RecognitionConfig;
import com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding;
import com.google.cloud.speech.v1beta1.SpeechGrpc;
Expand All @@ -28,6 +29,8 @@
import com.google.protobuf.TextFormat;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.auth.ClientAuthInterceptor;
import io.grpc.stub.StreamObserver;

import org.apache.commons.cli.CommandLine;
Expand All @@ -47,6 +50,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


Expand Down Expand Up @@ -84,7 +88,7 @@ public StreamingRecognizeClient(ManagedChannel channel, String file, int samplin
//Send log4j logs to Console
//If you are going to run this on GCE, you might wish to integrate with gcloud-java logging.
//See https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/README.md#stackdriver-logging-alpha

ConsoleAppender appender = new ConsoleAppender(new SimpleLayout(), SYSTEM_OUT);
logger.addAppender(appender);
}
Expand All @@ -93,6 +97,17 @@ public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}

static ManagedChannel createChannel(String host, int port) throws IOException {
GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
creds = creds.createScoped(OAUTH2_SCOPES);
ManagedChannel channel =
ManagedChannelBuilder.forAddress(host, port)
.intercept(new ClientAuthInterceptor(creds, Executors.newSingleThreadExecutor()))
.build();

return channel;
}

/** Send streaming recognize requests to server. */
public void recognize() throws InterruptedException, IOException {
final CountDownLatch finishLatch = new CountDownLatch(1);
Expand Down Expand Up @@ -242,7 +257,7 @@ public static void main(String[] args) throws Exception {
System.exit(1);
}

ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port);
ManagedChannel channel = createChannel(host, port);
StreamingRecognizeClient client = new StreamingRecognizeClient(channel, audioFile, sampling);
try {
client.recognize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.examples.cloud.speech;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.speech.v1beta1.RecognitionAudio;
import com.google.cloud.speech.v1beta1.RecognitionConfig;
import com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding;
Expand All @@ -25,7 +26,9 @@
import com.google.protobuf.TextFormat;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import io.grpc.auth.ClientAuthInterceptor;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
Expand All @@ -38,6 +41,7 @@
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -49,15 +53,15 @@ public class SyncRecognizeClient {

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

private static final List<String> OAUTH2_SCOPES =
Arrays.asList("https://www.googleapis.com/auth/cloud-platform");

private final URI input;
private final int samplingRate;

private final ManagedChannel channel;
private final SpeechGrpc.SpeechBlockingStub speechClient;

private static final List<String> OAUTH2_SCOPES =
Arrays.asList("https://www.googleapis.com/auth/cloud-platform");

/**
* Construct client connecting to Cloud Speech server at {@code host:port}.
*/
Expand All @@ -78,6 +82,17 @@ public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}

static ManagedChannel createChannel(String host, int port) throws IOException {
GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
creds = creds.createScoped(OAUTH2_SCOPES);
ManagedChannel channel =
ManagedChannelBuilder.forAddress(host, port)
.intercept(new ClientAuthInterceptor(creds, Executors.newSingleThreadExecutor()))
.build();

return channel;
}

/** Send a non-streaming-recognize request to server. */
public void recognize() {
RecognitionAudio audio;
Expand Down Expand Up @@ -179,7 +194,7 @@ public static void main(String[] args) throws Exception {
System.exit(1);
}

ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port);
ManagedChannel channel = createChannel(host, port);
SyncRecognizeClient client = new SyncRecognizeClient(channel, URI.create(audioFile), sampling);
try {
client.recognize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public void test16KHzAudio() throws InterruptedException, IOException {

String host = "speech.googleapis.com";
int port = 443;
ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port);
ManagedChannel channel = StreamingRecognizeClient.createChannel(host, port);
StreamingRecognizeClient client = new StreamingRecognizeClient(channel, path.toString(), 16000);

client.recognize();
assertThat(writer.toString()).contains("transcript: \"how old is the Brooklyn Bridge\"");
}
Expand All @@ -78,7 +78,7 @@ public void test32KHzAudio() throws InterruptedException, IOException {

String host = "speech.googleapis.com";
int port = 443;
ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port);
ManagedChannel channel = StreamingRecognizeClient.createChannel(host, port);
StreamingRecognizeClient client = new StreamingRecognizeClient(channel, path.toString(), 32000);

client.recognize();
Expand Down