-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Speech api changes #267
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
Speech api changes #267
Changes from 1 commit
89f445c
1408b15
6ac06c5
34f300c
259ed6a
4415993
3892470
e1516d2
ed629b6
20e6b88
6f513ca
f221210
f9ac582
2b949f6
cecb3b9
f0e8755
03b5c76
c3b811e
e81d249
3976813
e56ffc9
e47d295
e84a63f
a2daba0
9e0563c
5ba3e19
e957cd3
115bbf8
e7fd065
01a7fa8
b5bacda
5a6d2ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,8 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
// Client that sends audio to Speech.AsyncRecognize via gRPC and returns transcription. | ||
// Client that sends audio to Speech.AsyncRecognize via gRPC and returns longrunning operation. | ||
// The results are received via the google.longrunning.Operations interface. | ||
// | ||
// Uses a service account for OAuth2 authentication, which you may obtain at | ||
// https://console.developers.google.com | ||
|
@@ -26,14 +27,12 @@ | |
package com.google.cloud.speech.grpc.demos; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.cloud.speech.v1beta1.AsyncRecognizeRequest; | ||
import com.google.cloud.speech.v1beta1.AsyncRecognizeResponse; | ||
import com.google.cloud.speech.v1beta1.RecognitionAudio; | ||
import com.google.cloud.speech.v1beta1.RecognitionConfig; | ||
import com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding; | ||
import com.google.cloud.speech.v1beta1.SpeechGrpc; | ||
import com.google.cloud.speech.v1beta1.AsyncRecognizeRequest; | ||
import com.google.cloud.speech.v1beta1.AsyncRecognizeResponse; | ||
import com.google.protobuf.TextFormat; | ||
|
||
import com.google.longrunning.GetOperationRequest; | ||
import com.google.longrunning.Operation; | ||
|
@@ -79,7 +78,7 @@ public class AsyncRecognizeClient { | |
|
||
private final ManagedChannel channel; | ||
private final SpeechGrpc.SpeechBlockingStub stub; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's in this list you should explain why a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stub is a gRPC term (inherited from stubby, it's predecessor). Client would be a better name, probably. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we using them? Shouldn't the name be clearer for what we expose to users? |
||
private final OperationsGrpc.OperationsBlockingStub op; | ||
private final OperationsGrpc.OperationsBlockingStub statusStub; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same Q here? |
||
|
||
/** | ||
* Construct client connecting to Cloud Speech server at {@code host:port}. | ||
|
@@ -98,7 +97,7 @@ public AsyncRecognizeClient(String host, int port, URI input, int samplingRate) | |
.intercept(new ClientAuthInterceptor(creds, Executors.newSingleThreadExecutor())) | ||
.build(); | ||
stub = SpeechGrpc.newBlockingStub(channel); | ||
op = OperationsGrpc.newBlockingStub(channel); | ||
statusStub = OperationsGrpc.newBlockingStub(channel); | ||
|
||
logger.info("Created stub for " + host + ":" + port); | ||
} | ||
|
@@ -130,33 +129,36 @@ public void recognize() { | |
.setAudio(audio) | ||
.build(); | ||
|
||
Operation operation, status; | ||
Operation operation; | ||
Operation status; | ||
try { | ||
operation = stub.asyncRecognize(request); | ||
|
||
//Print the long running operation handle | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] Space before Print after |
||
logger.log(Level.INFO, String.format("Operation handle: %s, URI: %s", operation.getName(), | ||
input.toString())); | ||
input.toString())); | ||
} catch (StatusRuntimeException e) { | ||
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); | ||
return; | ||
} | ||
|
||
while(true) { | ||
while (true) { | ||
try { | ||
logger.log(Level.INFO, "Waiting 2s for operation, {0} processing...", operation.getName()); | ||
Thread.sleep(2000); | ||
GetOperationRequest operationReq = GetOperationRequest.newBuilder() | ||
.setName(operation.getName()) | ||
.build(); | ||
status = op.getOperation( | ||
status = statusStub.getOperation( | ||
GetOperationRequest.newBuilder() | ||
.setName(operation.getName()) | ||
.build() | ||
); | ||
|
||
if(status.getDone()) break; | ||
}catch(Exception ex) { | ||
if (status.getDone()) { | ||
break; | ||
} | ||
} catch (Exception ex) { | ||
logger.log(Level.WARNING, ex.getMessage()); | ||
} | ||
} | ||
|
@@ -165,7 +167,7 @@ public void recognize() { | |
AsyncRecognizeResponse asyncRes = status.getResponse().unpack(AsyncRecognizeResponse.class); | ||
|
||
logger.info("Received response: " + asyncRes); | ||
} catch(com.google.protobuf.InvalidProtocolBufferException ex) { | ||
} catch (com.google.protobuf.InvalidProtocolBufferException ex) { | ||
logger.log(Level.WARNING, "Unpack error, {0}",ex.getMessage()); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For new samples we have been using
com.example
package instead ofcom.google
. This is to make it more clear to the user that they can change the package to match their own project.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This structure already existed. I didn't mess up with the package name. Change it still?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about pinging on the package name, it looks like whom ever reviewed the first version wasn't on the Java team. :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lesv @tswast Its common to put company name in the package even samples. This is google code so why not? https://github.com/linkedin/api-get-started/tree/master/java/src/com/linkedin/sample
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See RFC 2606