|
14 | 14 |
|
15 | 15 | """GAX/GAPIC module for managing Speech API requests.""" |
16 | 16 |
|
| 17 | +from google.longrunning import operations_grpc |
| 18 | + |
17 | 19 | from google.cloud.gapic.speech.v1beta1.speech_api import SpeechApi |
| 20 | +from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import ( |
| 21 | + AsyncRecognizeMetadata) |
| 22 | +from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import ( |
| 23 | + AsyncRecognizeResponse) |
18 | 24 | from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import SpeechContext |
19 | 25 | from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import RecognitionConfig |
20 | 26 | from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import RecognitionAudio |
|
23 | 29 | from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import ( |
24 | 30 | StreamingRecognizeRequest) |
25 | 31 |
|
26 | | - |
| 32 | +from google.cloud._helpers import make_secure_stub |
| 33 | +from google.cloud.connection import DEFAULT_USER_AGENT |
27 | 34 | from google.cloud.speech.transcript import Transcript |
| 35 | +from google.cloud.operation import Operation |
| 36 | +from google.cloud.operation import register_type |
| 37 | + |
| 38 | + |
| 39 | +OPERATIONS_API_HOST = 'speech.googleapis.com' |
| 40 | + |
| 41 | +register_type(AsyncRecognizeMetadata) |
| 42 | +register_type(AsyncRecognizeResponse) |
28 | 43 |
|
29 | 44 |
|
30 | 45 | class GAPICSpeechAPI(object): |
31 | 46 | """Manage calls through GAPIC wrappers to the Speech API.""" |
32 | | - def __init__(self): |
| 47 | + def __init__(self, client=None): |
| 48 | + self._client = client |
33 | 49 | self._gapic_api = SpeechApi() |
34 | 50 |
|
35 | 51 | def async_recognize(self, sample, language_code=None, |
@@ -72,9 +88,26 @@ def async_recognize(self, sample, language_code=None, |
72 | 88 | and phrases. This can also be used to add new |
73 | 89 | words to the vocabulary of the recognizer. |
74 | 90 |
|
75 | | - :raises NotImplementedError: Always. |
| 91 | + :rtype: :class:`~google.cloud.operation.Opeartion` |
| 92 | + :returns: Instance of ``Operation`` to poll for results. |
76 | 93 | """ |
77 | | - raise NotImplementedError |
| 94 | + config = RecognitionConfig( |
| 95 | + encoding=sample.encoding, sample_rate=sample.sample_rate, |
| 96 | + language_code=language_code, max_alternatives=max_alternatives, |
| 97 | + profanity_filter=profanity_filter, |
| 98 | + speech_context=SpeechContext(phrases=speech_context)) |
| 99 | + |
| 100 | + audio = RecognitionAudio(content=sample.content, |
| 101 | + uri=sample.source_uri) |
| 102 | + api = self._gapic_api |
| 103 | + response = api.async_recognize(config=config, audio=audio) |
| 104 | + |
| 105 | + self._client._operations_stub = make_secure_stub( |
| 106 | + self._client.connection.credentials, |
| 107 | + DEFAULT_USER_AGENT, |
| 108 | + operations_grpc.OperationsStub, |
| 109 | + OPERATIONS_API_HOST) |
| 110 | + return Operation.from_pb(response, self._client) |
78 | 111 |
|
79 | 112 | def sync_recognize(self, sample, language_code=None, max_alternatives=None, |
80 | 113 | profanity_filter=None, speech_context=None): |
|
0 commit comments