|
1 |
| -# Copyright 2016 Google Inc. All rights reserved. |
| 1 | +# Copyright 2016 Google Inc. |
2 | 2 | #
|
3 | 3 | # Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 4 | # you may not use this file except in compliance with the License.
|
|
14 | 14 |
|
15 | 15 | """Long running operation representation for Google Speech API"""
|
16 | 16 |
|
17 |
| -from google.cloud.speech.metadata import Metadata |
18 |
| -from google.cloud.speech.transcript import Transcript |
| 17 | +from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2 |
| 18 | + |
19 | 19 | from google.cloud import operation
|
| 20 | +from google.cloud.speech.transcript import Transcript |
20 | 21 |
|
21 | 22 |
|
22 |
| -class Operation(operation.Operation): |
23 |
| - """Representation of a Google API Long-Running Operation. |
| 23 | +operation.register_type(cloud_speech_pb2.AsyncRecognizeMetadata) |
| 24 | +operation.register_type(cloud_speech_pb2.AsyncRecognizeResponse) |
24 | 25 |
|
25 |
| - :type client: :class:`~google.cloud.speech.client.Client` |
26 |
| - :param client: Instance of speech client. |
27 | 26 |
|
28 |
| - :type name: int |
29 |
| - :param name: ID assigned to an operation. |
| 27 | +class Operation(operation.Operation): |
| 28 | + """Custom Long-Running Operation for Google Speech API. |
30 | 29 |
|
31 |
| - :type complete: bool |
32 |
| - :param complete: True if operation is complete, else False. |
| 30 | + :type name: str |
| 31 | + :param name: The fully-qualified path naming the operation. |
33 | 32 |
|
34 |
| - :type metadata: :class:`~google.cloud.speech.metadata.Metadata` |
35 |
| - :param metadata: Instance of ``Metadata`` with operation information. |
| 33 | + :type client: :class:`~google.cloud.speech.client.Client` |
| 34 | + :param client: Client that created the current operation. |
36 | 35 |
|
37 |
| - :type results: dict |
38 |
| - :param results: Dictionary with transcript and score of operation. |
| 36 | + :type caller_metadata: dict |
| 37 | + :param caller_metadata: caller-assigned metadata about the operation |
39 | 38 | """
|
40 |
| - def __init__(self, client, name, complete=False, metadata=None, |
41 |
| - results=None): |
42 |
| - self.client = client |
43 |
| - self.name = name |
44 |
| - self._complete = complete |
45 |
| - self._metadata = metadata |
46 |
| - self._results = results |
47 |
| - |
48 |
| - @classmethod |
49 |
| - def from_api_repr(cls, client, response): |
50 |
| - """Factory: construct an instance from Google Speech API. |
51 |
| -
|
52 |
| - :type client: :class:`~google.cloud.speech.client.Client` |
53 |
| - :param client: Instance of speech client. |
54 |
| -
|
55 |
| - :type response: dict |
56 |
| - :param response: Dictionary response from Google Speech Operations API. |
57 |
| -
|
58 |
| - :rtype: :class:`Operation` |
59 |
| - :returns: Instance of `~google.cloud.speech.operations.Operation`. |
60 |
| - """ |
61 |
| - name = response['name'] |
62 |
| - complete = response.get('done', False) |
63 | 39 |
|
64 |
| - operation_instance = cls(client, name, complete) |
65 |
| - operation_instance._update(response) |
66 |
| - return operation_instance |
| 40 | + results = None |
| 41 | + """List of transcriptions from the speech-to-text process.""" |
67 | 42 |
|
68 |
| - @property |
69 |
| - def complete(self): |
70 |
| - """Completion state of the `Operation`. |
| 43 | + def _update_state(self, operation_pb): |
| 44 | + """Update the state of the current object based on operation. |
71 | 45 |
|
72 |
| - :rtype: bool |
73 |
| - :returns: True if already completed, else false. |
74 |
| - """ |
75 |
| - return self._complete |
| 46 | + This mostly does what the base class does, but all populates |
| 47 | + results. |
76 | 48 |
|
77 |
| - @property |
78 |
| - def metadata(self): |
79 |
| - """Metadata of operation. |
| 49 | + :type operation_pb: |
| 50 | + :class:`~google.longrunning.operations_pb2.Operation` |
| 51 | + :param operation_pb: Protobuf to be parsed. |
80 | 52 |
|
81 |
| - :rtype: :class:`~google.cloud.speech.metadata.Metadata` |
82 |
| - :returns: Instance of ``Metadata``. |
| 53 | + :raises ValueError: If there is more than one entry in ``results``. |
83 | 54 | """
|
84 |
| - return self._metadata |
| 55 | + super(Operation, self)._update_state(operation_pb) |
85 | 56 |
|
86 |
| - @property |
87 |
| - def results(self): |
88 |
| - """Results dictionary with transcript information. |
| 57 | + result_type = operation_pb.WhichOneof('result') |
| 58 | + if result_type != 'response': |
| 59 | + return |
89 | 60 |
|
90 |
| - :rtype: dict |
91 |
| - :returns: Dictionary with transcript and confidence score. |
92 |
| - """ |
93 |
| - return self._results |
94 |
| - |
95 |
| - def poll(self): |
96 |
| - """Check if the operation has finished. |
97 |
| -
|
98 |
| - :rtype: bool |
99 |
| - :returns: A boolean indicating if the current operation has completed. |
100 |
| - :raises: :class:`ValueError <exceptions.ValueError>` if the operation |
101 |
| - has already completed. |
102 |
| - """ |
103 |
| - if self.complete: |
104 |
| - raise ValueError('The operation has completed.') |
| 61 | + pb_results = self.response.results |
| 62 | + if len(pb_results) != 1: |
| 63 | + raise ValueError('Expected exactly one result, found:', |
| 64 | + pb_results) |
105 | 65 |
|
106 |
| - path = 'operations/%s' % (self.name,) |
107 |
| - api_response = self.client.connection.api_request(method='GET', |
108 |
| - path=path) |
109 |
| - self._update(api_response) |
110 |
| - return self.complete |
111 |
| - |
112 |
| - def _update(self, response): |
113 |
| - """Update Operation instance with latest data from Speech API. |
114 |
| -
|
115 |
| - .. _speech_operations: https://cloud.google.com/speech/reference/\ |
116 |
| - rest/v1beta1/operations |
117 |
| -
|
118 |
| - :type response: dict |
119 |
| - :param response: Response from Speech API Operations endpoint. |
120 |
| - See: `speech_operations`_. |
121 |
| - """ |
122 |
| - metadata = response.get('metadata', None) |
123 |
| - raw_results = response.get('response', {}).get('results', None) |
124 |
| - results = [] |
125 |
| - if raw_results: |
126 |
| - for result in raw_results: |
127 |
| - for alternative in result['alternatives']: |
128 |
| - results.append(Transcript.from_api_repr(alternative)) |
129 |
| - if metadata: |
130 |
| - self._metadata = Metadata.from_api_repr(metadata) |
131 |
| - |
132 |
| - self._results = results |
133 |
| - self._complete = response.get('done', False) |
| 66 | + result = pb_results[0] |
| 67 | + self.results = [Transcript.from_pb(alternative) |
| 68 | + for alternative in result.alternatives] |
0 commit comments