Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[WIP] Speech v1
  • Loading branch information
lukesneeringer committed Apr 2, 2017
commit 8454815518eca80fbc389ecdc6d65ada7e85902d
48 changes: 22 additions & 26 deletions speech/google/cloud/speech/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
"""GAX/GAPIC module for managing Speech API requests."""


from google.cloud.gapic.speech.v1beta1.speech_client import SpeechClient
from google.cloud.proto.speech.v1beta1.cloud_speech_pb2 import RecognitionAudio
from google.cloud.proto.speech.v1beta1.cloud_speech_pb2 import (
from google.cloud.gapic.speech.v1.speech_client import SpeechClient
from google.cloud.proto.speech.v1.cloud_speech_pb2 import RecognitionAudio
from google.cloud.proto.speech.v1.cloud_speech_pb2 import (
RecognitionConfig)
from google.cloud.proto.speech.v1beta1.cloud_speech_pb2 import (
from google.cloud.proto.speech.v1.cloud_speech_pb2 import (
SpeechContext)
from google.cloud.proto.speech.v1beta1.cloud_speech_pb2 import (
from google.cloud.proto.speech.v1.cloud_speech_pb2 import (
StreamingRecognitionConfig)
from google.cloud.proto.speech.v1beta1.cloud_speech_pb2 import (
from google.cloud.proto.speech.v1.cloud_speech_pb2 import (
StreamingRecognizeRequest)
from google.longrunning import operations_grpc

Expand Down Expand Up @@ -62,23 +62,22 @@ def __init__(self, client=None):
OPERATIONS_API_HOST,
)

def async_recognize(self, sample, language_code=None,
def async_recognize(self, sample, language_code,
max_alternatives=None, profanity_filter=None,
speech_context=None):
"""Asychronous Recognize request to Google Speech API.

.. _async_recognize: https://cloud.google.com/speech/reference/\
rest/v1beta1/speech/asyncrecognize
rest/v1/speech/asyncrecognize

See `async_recognize`_.

:type sample: :class:`~google.cloud.speech.sample.Sample`
:param sample: Instance of ``Sample`` containing audio information.

:type language_code: str
:param language_code: (Optional) The language of the supplied audio as
BCP-47 language tag. Example: ``'en-GB'``.
If omitted, defaults to ``'en-US'``.
:param language_code: The language of the supplied audio as
BCP-47 language tag. Example: ``'en-US'``.

:type max_alternatives: int
:param max_alternatives: (Optional) Maximum number of recognition
Expand Down Expand Up @@ -118,7 +117,7 @@ def async_recognize(self, sample, language_code=None,

return Operation.from_pb(operation_future.last_operation_data(), self)

def streaming_recognize(self, sample, language_code=None,
def streaming_recognize(self, sample, language_code,
max_alternatives=None, profanity_filter=None,
speech_context=None, single_utterance=False,
interim_results=False):
Expand All @@ -136,9 +135,8 @@ def streaming_recognize(self, sample, language_code=None,
:param sample: Instance of ``Sample`` containing audio information.

:type language_code: str
:param language_code: (Optional) The language of the supplied audio as
BCP-47 language tag. Example: ``'en-GB'``.
If omitted, defaults to ``'en-US'``.
:param language_code: The language of the supplied audio as
BCP-47 language tag. Example: ``'en-US'``.

:type max_alternatives: int
:param max_alternatives: (Optional) Maximum number of recognition
Expand Down Expand Up @@ -190,7 +188,7 @@ def streaming_recognize(self, sample, language_code=None,
:raises: :class:`ValueError` if sample.content is not a file-like
object. :class:`ValueError` if stream has closed.

:rtype: :class:`~google.cloud.grpc.speech.v1beta1\
:rtype: :class:`~google.cloud.grpc.speech.v1\
.cloud_speech_pb2.StreamingRecognizeResponse`
:returns: ``StreamingRecognizeResponse`` instances.
"""
Expand All @@ -207,22 +205,21 @@ def streaming_recognize(self, sample, language_code=None,
responses = api.streaming_recognize(requests)
return responses

def sync_recognize(self, sample, language_code=None, max_alternatives=None,
def sync_recognize(self, sample, language_code, max_alternatives=None,
profanity_filter=None, speech_context=None):
"""Synchronous Speech Recognition.

.. _sync_recognize: https://cloud.google.com/speech/reference/\
rest/v1beta1/speech/syncrecognize
rest/v1/speech/syncrecognize

See `sync_recognize`_.

:type sample: :class:`~google.cloud.speech.sample.Sample`
:param sample: Instance of ``Sample`` containing audio information.

:type language_code: str
:param language_code: (Optional) The language of the supplied audio as
BCP-47 language tag. Example: ``'en-GB'``.
If omitted, defaults to ``'en-US'``.
:param language_code: The language of the supplied audio as
BCP-47 language tag. Example: ``'en-US'``.

:type max_alternatives: int
:param max_alternatives: (Optional) Maximum number of recognition
Expand Down Expand Up @@ -269,7 +266,7 @@ def sync_recognize(self, sample, language_code=None, max_alternatives=None,
return [Result.from_pb(result) for result in api_response.results]


def _stream_requests(sample, language_code=None, max_alternatives=None,
def _stream_requests(sample, language_code, max_alternatives=None,
profanity_filter=None, speech_context=None,
single_utterance=None, interim_results=None):
"""Generate stream of requests from sample.
Expand All @@ -278,9 +275,8 @@ def _stream_requests(sample, language_code=None, max_alternatives=None,
:param sample: Instance of ``Sample`` containing audio information.

:type language_code: str
:param language_code: (Optional) The language of the supplied audio as
BCP-47 language tag. Example: ``'en-GB'``.
If omitted, defaults to ``'en-US'``.
:param language_code: The language of the supplied audio as
BCP-47 language tag. Example: ``'en-US'``.

:type max_alternatives: int
:param max_alternatives: (Optional) Maximum number of recognition
Expand Down Expand Up @@ -409,7 +405,7 @@ def _make_streaming_request(sample, language_code,
returned.

:rtype:
:class:`~grpc.speech.v1beta1.cloud_speech_pb2.StreamingRecognizeRequest`
:class:`~grpc.speech.v1.cloud_speech_pb2.StreamingRecognizeRequest`
:returns: Instance of ``StreamingRecognizeRequest``.
"""
config = RecognitionConfig(
Expand Down
36 changes: 17 additions & 19 deletions speech/google/cloud/speech/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Connection(_http.JSONConnection):
API_BASE_URL = 'https://speech.googleapis.com'
"""The base of the API call URL."""

API_VERSION = 'v1beta1'
API_VERSION = 'v1'
"""The version of the API, used in building the API call's URL."""

API_URL_TEMPLATE = '{api_base_url}/{api_version}/{path}'
Expand All @@ -59,23 +59,22 @@ def __init__(self, client):
self._client = client
self._connection = Connection(client)

def async_recognize(self, sample, language_code=None,
def async_recognize(self, sample, language_code,
max_alternatives=None, profanity_filter=None,
speech_context=None):
"""Asychronous Recognize request to Google Speech API.

.. _async_recognize: https://cloud.google.com/speech/reference/\
rest/v1beta1/speech/asyncrecognize
rest/v1/speech/asyncrecognize

See `async_recognize`_.

:type sample: :class:`~google.cloud.speech.sample.Sample`
:param sample: Instance of ``Sample`` containing audio information.

:type language_code: str
:param language_code: (Optional) The language of the supplied audio as
BCP-47 language tag. Example: ``'en-GB'``.
If omitted, defaults to ``'en-US'``.
:param language_code: The language of the supplied audio as
BCP-47 language tag. Example: ``'en-US'``.

:type max_alternatives: int
:param max_alternatives: (Optional) Maximum number of recognition
Expand Down Expand Up @@ -108,15 +107,15 @@ def async_recognize(self, sample, language_code=None,
method='POST', path='speech:asyncrecognize', data=data)

operation = Operation.from_dict(api_response, self._client)
operation.caller_metadata['request_type'] = 'AsyncRecognize'
operation.caller_metadata['request_type'] = 'LongRunningRecognize'
return operation

def sync_recognize(self, sample, language_code=None, max_alternatives=None,
def sync_recognize(self, sample, language_code, max_alternatives=None,
profanity_filter=None, speech_context=None):
"""Synchronous Speech Recognition.

.. _sync_recognize: https://cloud.google.com/speech/reference/\
rest/v1beta1/speech/syncrecognize
rest/v1/speech/syncrecognize

See `sync_recognize`_.

Expand All @@ -125,8 +124,7 @@ def sync_recognize(self, sample, language_code=None, max_alternatives=None,

:type language_code: str
:param language_code: (Optional) The language of the supplied audio as
BCP-47 language tag. Example: ``'en-GB'``.
If omitted, defaults to ``'en-US'``.
BCP-47 language tag. Example: ``'en-US'``.

:type max_alternatives: int
:param max_alternatives: (Optional) Maximum number of recognition
Expand Down Expand Up @@ -173,17 +171,16 @@ def sync_recognize(self, sample, language_code=None, max_alternatives=None,
raise ValueError('No results were returned from the API')


def _build_request_data(sample, language_code=None, max_alternatives=None,
def _build_request_data(sample, language_code, max_alternatives=None,
profanity_filter=None, speech_context=None):
"""Builds the request data before making API request.

:type sample: :class:`~google.cloud.speech.sample.Sample`
:param sample: Instance of ``Sample`` containing audio information.

:type language_code: str
:param language_code: (Optional) The language of the supplied audio as
BCP-47 language tag. Example: ``'en-GB'``.
If omitted, defaults to ``'en-US'``.
:param language_code: The language of the supplied audio as
BCP-47 language tag. Example: ``'en-US'``.

:type max_alternatives: int
:param max_alternatives: (Optional) Maximum number of recognition
Expand Down Expand Up @@ -216,11 +213,12 @@ def _build_request_data(sample, language_code=None, max_alternatives=None,
else:
audio = {'uri': sample.source_uri}

config = {'encoding': sample.encoding,
'sampleRate': sample.sample_rate}
config = {
'encoding': sample.encoding,
'languageCode': language_code,
'sampleRate': sample.sample_rate,
}

if language_code is not None:
config['languageCode'] = language_code
if max_alternatives is not None:
config['maxAlternatives'] = max_alternatives
if profanity_filter is not None:
Expand Down
2 changes: 1 addition & 1 deletion speech/google/cloud/speech/alternative.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def from_pb(cls, alternative):
"""Factory: construct ``Alternative`` from protobuf response.

:type alternative:
:class:`google.cloud.speech.v1beta1.SpeechRecognitionAlternative`
:class:`google.cloud.speech.v1.SpeechRecognitionAlternative`
:param alternative: Instance of ``SpeechRecognitionAlternative``
from protobuf.

Expand Down
2 changes: 1 addition & 1 deletion speech/google/cloud/speech/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Encoding(object):
"""Audio encoding types.

See:
https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionConfig#AudioEncoding
https://cloud.google.com/speech/reference/rest/v1/RecognitionConfig#AudioEncoding
"""

LINEAR16 = 'LINEAR16'
Expand Down
6 changes: 3 additions & 3 deletions speech/google/cloud/speech/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

"""Long running operation representation for Google Speech API"""

from google.cloud.proto.speech.v1beta1 import cloud_speech_pb2
from google.cloud.proto.speech.v1 import cloud_speech_pb2

from google.cloud import operation
from google.cloud.speech.result import Result


operation.register_type(cloud_speech_pb2.AsyncRecognizeMetadata)
operation.register_type(cloud_speech_pb2.AsyncRecognizeResponse)
operation.register_type(cloud_speech_pb2.LongRunningRecognizeMetadata)
operation.register_type(cloud_speech_pb2.LongRunningRecognizeResponse)


class Operation(operation.Operation):
Expand Down
6 changes: 3 additions & 3 deletions speech/google/cloud/speech/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, alternatives):
def from_pb(cls, result):
"""Factory: construct instance of ``Result``.

:type result: :class:`~google.cloud.proto.speech.v1beta1\
:type result: :class:`~google.cloud.proto.speech.v1\
.cloud_speech_pb2.SpeechRecognitionResult`
:param result: Instance of ``SpeechRecognitionResult`` protobuf.

Expand All @@ -51,7 +51,7 @@ def from_api_repr(cls, result):

:type result: dict
:param result: Dictionary of a :class:`~google.cloud.proto.speech.\
v1beta1.cloud_speech_pb2.SpeechRecognitionResult`
v1.cloud_speech_pb2.SpeechRecognitionResult`

:rtype: :class:`~google.cloud.speech.result.Result`
:returns: Instance of ``Result``.
Expand Down Expand Up @@ -101,7 +101,7 @@ def __init__(self, alternatives, is_final=False, stability=0.0):
def from_pb(cls, response):
"""Factory: construct instance of ``StreamingSpeechResult``.

:type response: :class:`~google.cloud.proto.speech.v1beta1\
:type response: :class:`~google.cloud.proto.speech.v1\
.cloud_speech_pb2.StreamingRecognizeResult`
:param response: Instance of ``StreamingRecognizeResult`` protobuf.

Expand Down
4 changes: 2 additions & 2 deletions speech/google/cloud/speech/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def async_recognize(self, language_code=None, max_alternatives=None,
"""Asychronous Recognize request to Google Speech API.

.. _async_recognize: https://cloud.google.com/speech/reference/\
rest/v1beta1/speech/asyncrecognize
rest/v1/speech/asyncrecognize

See `async_recognize`_.

Expand Down Expand Up @@ -265,7 +265,7 @@ def sync_recognize(self, language_code=None, max_alternatives=None,
"""Synchronous Speech Recognition.

.. _sync_recognize: https://cloud.google.com/speech/reference/\
rest/v1beta1/speech/syncrecognize
rest/v1/speech/syncrecognize

See `sync_recognize`_.

Expand Down
6 changes: 3 additions & 3 deletions speech/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@
}

REQUIREMENTS = [
'google-cloud-core >= 0.24.0, < 0.25dev',
'google-cloud-core >= 0.24.0, < 0.26dev',

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

'grpcio >= 1.0.2, < 2.0dev',
'gapic-google-cloud-speech-v1beta1 >= 0.15.2, < 0.16dev',
'gapic-google-cloud-speech-v1 >= 0.15.3, < 0.16dev',
]

setup(
name='google-cloud-speech',
version='0.24.0',
version='0.25.0',

This comment was marked as spam.

description='Python Client for Google Cloud Speech',
long_description=README,
namespace_packages=[
Expand Down
6 changes: 3 additions & 3 deletions speech/tests/unit/_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
'name': '123456789',
'metadata': {
'@type': ('type.googleapis.com/'
'google.cloud.speech.v1beta1.AsyncRecognizeMetadata'),
'google.cloud.speech.v1.LongRunningRecognizeMetadata'),
'progressPercent': 100,
'startTime': '2016-09-22T17:52:25.536964Z',
'lastUpdateTime': '2016-09-22T17:52:27.802902Z',
},
'done': True,
'response': {
'@type': ('type.googleapis.com/'
'google.cloud.speech.v1beta1.AsyncRecognizeResponse'),
'google.cloud.speech.v1.LongRunningRecognizeResponse'),
'results': [
{
'alternatives': [
Expand All @@ -63,7 +63,7 @@
'name': '123456789',
'metadata': {
'@type': ('type.googleapis.com/'
'google.cloud.speech.v1beta1.AsyncRecognizeMetadata'),
'google.cloud.speech.v1.LongRunningRecognizeMetadata'),
'progressPercent': 27,
'startTime': '2016-09-22T17:52:25.536964Z',
'lastUpdateTime': '2016-09-22T17:52:27.802902Z',
Expand Down
6 changes: 3 additions & 3 deletions speech/tests/unit/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _make_one(self, *args, **kw):
'google.cloud._helpers.make_secure_channel',
return_value=mock.sentinel.channel)
@mock.patch(
'google.cloud.gapic.speech.v1beta1.speech_client.SpeechClient',
'google.cloud.gapic.speech.v1.speech_client.SpeechClient',
SERVICE_ADDRESS='hey.you.guys')
@mock.patch(
'google.cloud._helpers.make_secure_stub',
Expand Down Expand Up @@ -89,7 +89,7 @@ def _call_fut(self, sample, language_code, max_alternatives,
def test_ctor(self):
from google.cloud import speech
from google.cloud.speech.sample import Sample
from google.cloud.proto.speech.v1beta1.cloud_speech_pb2 import (
from google.cloud.proto.speech.v1.cloud_speech_pb2 import (
RecognitionConfig, SpeechContext, StreamingRecognitionConfig,
StreamingRecognizeRequest)

Expand Down Expand Up @@ -148,7 +148,7 @@ def test_stream_requests(self):
from io import BytesIO
from google.cloud import speech
from google.cloud.speech.sample import Sample
from google.cloud.proto.speech.v1beta1.cloud_speech_pb2 import (
from google.cloud.proto.speech.v1.cloud_speech_pb2 import (
StreamingRecognitionConfig, StreamingRecognizeRequest)

sample = Sample(
Expand Down
Loading