Skip to content

Commit f221210

Browse files
committed
added async samples
Author: puneith <puneith@google.com>
1 parent 6f513ca commit f221210

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

speech/grpc/README.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ note that the audio file must be in RAW format. You can use `sox`
7373
(available, e.g. via [http://sox.sourceforge.net/](http://sox.sourceforge.net/)
7474
or [homebrew](http://brew.sh/)) to convert audio files to raw format.
7575

76-
### Run the non-streaming client
76+
### Run the sync client
7777

78-
You can run the batch client like this:
78+
You can run the sync client like this:
7979

8080
```sh
8181
$ bin/speech-sample-sync.sh --host=speech.googleapis.com --port=443 \
@@ -89,6 +89,21 @@ $ bin/speech-sample-sync.sh --host=speech.googleapis.com --port=443 \
8989
--uri=resources/audio.raw --sampling=16000
9090
```
9191

92+
### Run the async client
93+
94+
You can run the async client like this:
95+
96+
```sh
97+
bin/speech-sample-async.sh --host=speech.googleapis.com --port=443 \
98+
--uri=<audio file uri> --sampling=<sample rate>
99+
```
100+
101+
Try a streaming rate of 16000 and the included sample audio file, as follows:
102+
```sh
103+
$ bin/speech-sample-async.sh --host=speech.googleapis.com --port=443 \
104+
--uri=resources/audio.raw --sampling=16000
105+
```
106+
92107
### Run the streaming client
93108

94109
You can run the streaming client as follows:
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
# Copyright 2016 Google Inc. All Rights Reserved.
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+
# http://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+
SRC_DIR=$(cd "$(dirname "$0")/.."; pwd)
17+
java -cp ${SRC_DIR}/target/grpc-sample-1.0-jar-with-dependencies.jar \
18+
com.google.cloud.speech.grpc.demos.AsyncRecognizeClient "$@"

speech/grpc/src/main/java/com/google/cloud/speech/grpc/demos/AsyncRecognizeClient.java

+28-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import com.google.cloud.speech.v1beta1.AsyncRecognizeResponse;
3535
import com.google.protobuf.TextFormat;
3636

37-
import google.longrunning.Operation;
37+
import com.google.longrunning.GetOperationRequest;
38+
import com.google.longrunning.Operation;
39+
import com.google.longrunning.OperationsGrpc;
3840

3941
import io.grpc.ManagedChannel;
4042
import io.grpc.StatusRuntimeException;
@@ -76,6 +78,7 @@ public class AsyncRecognizeClient {
7678

7779
private final ManagedChannel channel;
7880
private final SpeechGrpc.SpeechBlockingStub stub;
81+
private final OperationsGrpc.OperationsBlockingStub op;
7982

8083
/**
8184
* Construct client connecting to Cloud Speech server at {@code host:port}.
@@ -94,6 +97,8 @@ public AsyncRecognizeClient(String host, int port, URI input, int samplingRate)
9497
.intercept(new ClientAuthInterceptor(creds, Executors.newSingleThreadExecutor()))
9598
.build();
9699
stub = SpeechGrpc.newBlockingStub(channel);
100+
op = OperationsGrpc.newBlockingStub(channel);
101+
97102
logger.info("Created stub for " + host + ":" + port);
98103
}
99104

@@ -124,23 +129,38 @@ public void recognize() {
124129
.setAudio(audio)
125130
.build();
126131

127-
Operation operation;
132+
Operation operation, status;
128133
try {
129134
operation = stub.asyncRecognize(request);
130135

131136
//Print the long running operation handle
132-
System.out.println(operation);
137+
logger.log(Level.INFO, String.format("Operation handle: %s, URI: %s", operation.getName(),
138+
input.toString()));
133139
} catch (StatusRuntimeException e) {
134140
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
135141
return;
136142
}
137143

138-
while(operation.hasDone()) {
139-
System.out.println(operation);
140-
//Sleep for 1 s
141-
Thread.sleep(1000);
144+
while(true) {
145+
try {
146+
logger.log(Level.INFO, "Waiting 2s for operation, {0} processing...", operation.getName());
147+
Thread.sleep(2000);
148+
GetOperationRequest operationReq = GetOperationRequest.newBuilder()
149+
.setName(operation.getName())
150+
.build();
151+
status = op.getOperation(
152+
GetOperationRequest.newBuilder()
153+
.setName(operation.getName())
154+
.build()
155+
);
156+
157+
if(status.getDone()) break;
158+
}catch(Exception ex) {
159+
logger.log(Level.WARNING, ex.getMessage());
160+
}
142161
}
143-
//logger.info("Received response: " + TextFormat.printToString(response));
162+
163+
logger.info("Received response: " + status.getResponse());
144164
}
145165

146166
public static void main(String[] args) throws Exception {

0 commit comments

Comments
 (0)