-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Basic implementation of Speech API. #2344
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
Changes from 5 commits
4920223
dce33a7
bdf141a
2a4a6b8
a542b6d
a5f9c0b
c64e5ff
988b7d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| Speech Client | ||
| ================ | ||
|
|
||
| .. automodule:: google.cloud.speech.client | ||
| :members: | ||
| :undoc-members: | ||
| :show-inheritance: | ||
|
|
||
| Connection | ||
| ~~~~~~~~~~ | ||
|
|
||
| .. automodule:: google.cloud.speech.connection | ||
| :members: | ||
| :undoc-members: | ||
| :show-inheritance: | ||
|
|
||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| Using the API | ||
| ============= | ||
|
|
||
| The `Google Speech`_ API enables developers to convert audio to text. | ||
| The API recognizes over 80 languages and variants, to support your global user base. | ||
|
|
||
| .. warning:: | ||
|
|
||
| This is a Beta release of Google Speech API. This | ||
| API is not intended for real-time usage in critical applications. | ||
|
|
||
| .. _Google Speech: https://cloud.google.com/speech/docs/getting-started | ||
|
|
||
| Client | ||
| ------ | ||
|
|
||
| :class:`~google.cloud.speech.client.Client` objects provide a | ||
| means to configure your application. Each instance holds | ||
| an authenticated connection to the Natural Language service. | ||
|
|
||
| For an overview of authentication in ``google-cloud-python``, see | ||
| :doc:`google-cloud-auth`. | ||
|
|
||
| Assuming your environment is set up as described in that document, | ||
| create an instance of :class:`~google.cloud.speech.client.Client`. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| >>> from google.cloud import speech | ||
| >>> client = speech.Client() | ||
|
|
||
| Methods | ||
| ------- | ||
|
|
||
| At this moment we only support one method of the Speech API: | ||
|
|
||
| - `syncrecognize`_ | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
| Synchronous Recognize | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| --------------------- | ||
|
|
||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| The :meth:`~google.cloud.speech.Client.syncrecognize` method | ||
| does speech to text on a file and returns the text | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| as a :class:`list` of tuples dicts (each containing a transcript an a confidence value). | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
| .. code-block:: python | ||
|
|
||
| >>> alternatives = client.syncrecognize(None,"gs://my-bucket/recording.flac", | ||
| ... "FLAC", 16000, max_alternatives=2): | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| >>> for alternative in alternatives: | ||
| ... print('=' * 20) | ||
| ... print(' transcript: %s' % (alternative["transcript"],)) | ||
| ... print(' confidence: %s' % (alternative["confidence"],)) | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| ==================== | ||
| transcript: Hello, this is a test | ||
| confidence: 0.81 | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| ==================== | ||
| transcript: Hello, this is one test | ||
| confidence: 0 | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
| .. _syncrecognize: https://cloud.google.com/speech/reference/rest/v1beta1/speech/syncrecognize | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Copyright 2016 Google Inc. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Google Cloud Speech API wrapper.""" | ||
|
|
||
| from google.cloud.speech.client import Client, Encoding | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| from google.cloud.speech.connection import Connection | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| # Copyright 2016 Google Inc. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Basic client for Google Cloud Speech API.""" | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
| from google.cloud import client as client_module | ||
| from google.cloud.speech.connection import Connection | ||
|
|
||
|
|
||
| class Encoding(object): | ||
| """Audio encoding types.""" | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
| LINEAR16 = 'LINEAR16' | ||
| """LINEAR16 encoding type.""" | ||
|
|
||
| FLAC = 'FLAC' | ||
| """FLAC encoding type.""" | ||
|
|
||
| MULAW = 'MULAW' | ||
| """MULAW encoding type.""" | ||
|
|
||
| AMR = 'AMR' | ||
| """AMR encoding type.""" | ||
|
|
||
| AMR_WB = 'AMR_WB' | ||
| """AMR_WB encoding type.""" | ||
|
|
||
|
|
||
| class Client(client_module.Client): | ||
| """Client to bundle configuration needed for API requests. | ||
|
|
||
| :type project: str | ||
| :param project: the project which the client acts on behalf of. Will be | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| passed when creating a dataset / job. If not passed, | ||
| falls back to the default inferred from the environment. | ||
|
|
||
| :type credentials: :class:`oauth2client.client.OAuth2Credentials` or | ||
| :class:`NoneType` | ||
| :param credentials: The OAuth2 Credentials to use for the connection | ||
| owned by this client. If not passed (and if no ``http`` | ||
| object is passed), falls back to the default inferred | ||
| from the environment. | ||
|
|
||
| :type http: :class:`httplib2.Http` or class that defines ``request()``. | ||
| :param http: An optional HTTP object to make requests. If not passed, an | ||
| ``http`` object is created that is bound to the | ||
| ``credentials`` for the current object. | ||
| """ | ||
|
|
||
| _connection_class = Connection | ||
|
|
||
| def syncrecognize(self, content, uri, encoding, sample_rate, | ||
| language_code=None, max_alternatives=None, | ||
| profanity_filter=None, | ||
| speech_context=None): | ||
| """Synchronous Speech Recognition. | ||
|
|
||
| .. _syncrecognize: https://cloud.google.com/speech/reference/\ | ||
| rest/v1beta1/speech/syncrecognize | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
| See `syncrecognize`_. | ||
|
|
||
| :type content: str | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| :param content: The content string containing the audio | ||
| data bytes encoded as specified in RecognitionConfig. | ||
| This is a base64-encoded string | ||
|
|
||
| :type uri: str | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| :param uri: URI that points to a file that contains audio | ||
| data bytes as specified in RecognitionConfig. | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| Currently, only Google Cloud Storage URIs are | ||
| supported, which must be specified in the following | ||
| format: gs://bucket_name/object_name | ||
|
|
||
| :type encoding: str | ||
| :param encoding: encoding of audio data sent in all RecognitionAudio | ||
| messages, can be one of: :attr:`~.Encoding.LINEAR16`, | ||
| :attr:`~.Encoding.FLAC`, :attr:`~.Encoding.MULAW`, | ||
| :attr:`~.Encoding.AMR`, :attr:`~.Encoding.AMR_WB` | ||
|
|
||
| :type sample_rate: int | ||
| :param sample_rate: Sample rate in Hertz of the audio data sent in all | ||
| RecognitionAudio messages. Valid values are: 8000- | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| 48000. 16000 is optimal. For best results, set the | ||
| sampling rate of the audio source to 16000 Hz. | ||
| If that's not possible, use the native sample rate | ||
| of the audio source (instead of re-sampling). | ||
|
|
||
| :type language_code: str | ||
| :param language_code: (Optional) The language of the supplied audio as | ||
| BCP-47 language tag. Example: "en-GB". | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| If omitted, defaults to "en-US". | ||
|
|
||
| :type max_alternatives: int | ||
| :param max_alternatives: (Optional) Maximum number of recognition | ||
| hypotheses to be returned. The server may | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| return fewer than maxAlternatives. | ||
| Valid values are 0-30. A value of 0 or 1 | ||
| will return a maximum of 1. Defaults to 1 | ||
|
|
||
| :type profanity_filter: bool | ||
| :param profanity_filter: If True, the server will attempt to filter | ||
| out profanities, replacing all but the | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| initialcharacter in each filtered word with | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| asterisks, e.g. "f***". If False or omitted, | ||
| profanities won't be filtered out. | ||
|
|
||
| :type speech_context: list | ||
| :param speech_context: A list of strings (max 50) containing words and | ||
| phrases "hints" so that the speech recognition | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| is more likely to recognize them. This can be | ||
| used to improve the accuracy for specific words | ||
| and phrases. This can also be used to add new | ||
| words to the vocabulary of the recognizer. | ||
|
|
||
| :rtype: list | ||
| :returns: A list of tuples. One tuple for each alternative. Each tuple | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| contains a transcript text and a confidence value (between | ||
| 0.0 and 1.0) | ||
| """ | ||
|
|
||
| if (content is None) and (uri is None): | ||
| message = 'content and uri cannot be both equal to None' | ||
| raise ValueError(message) | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
| if (content is not None) and (uri is not None): | ||
| message = 'content and uri cannot be both different from None' | ||
| raise ValueError(message) | ||
|
|
||
| if content is not None: | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| audio = {'content': content} | ||
| else: | ||
| audio = {'uri': uri} | ||
|
|
||
| required_params = [('encoding', encoding), ('sampleRate', sample_rate)] | ||
| for param_name, param in required_params: | ||
| if param is None: | ||
| message = '%r cannot be None' % (param_name) | ||
| raise ValueError(message) | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| config = dict(required_params) | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| for param_name, param in [ | ||
| ('languageCode', language_code), | ||
| ('maxAlternatives', max_alternatives), | ||
| ('profanityFilter', profanity_filter)]: | ||
| if param is not None: | ||
| config[param_name] = param | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| if speech_context is not None: | ||
| config["speechContext"] = {"phrases": speech_context} | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
| data = { | ||
| 'audio': audio, | ||
| 'config': config | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| } | ||
| api_response = self.connection.api_request( | ||
| method='POST', path='syncrecognize', data=data) | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
| return api_response['results'][0]['alternatives'] | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Copyright 2016 Google Inc. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Create / interact with Google Cloud Speech connections.""" | ||
|
|
||
| from google.cloud import connection as base_connection | ||
|
|
||
|
|
||
| class Connection(base_connection.JSONConnection): | ||
| """A connection to Google Cloud Speech JSON REST API.""" | ||
|
|
||
| API_BASE_URL = 'https://speech.googleapis.com' | ||
| """The base of the API call URL.""" | ||
|
|
||
| API_VERSION = 'v1beta1' | ||
| """The version of the API, used in building the API call's URL.""" | ||
|
|
||
| API_URL_TEMPLATE = '{api_base_url}/{api_version}/speech:{path}' | ||
| """A template for the URL of a particular API call.""" | ||
|
|
||
| SCOPE = ('https://www.googleapis.com/auth/cloud-platform',) | ||
| """The scopes required for authenticating as an API consumer.""" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright 2016 Google Inc. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.