Skip to content

Commit

Permalink
Merge pull request #231 from GoogleCloudPlatform/speech
Browse files Browse the repository at this point in the history
Cloud Speech API samples
  • Loading branch information
amygdala committed Mar 25, 2016
2 parents 240f7ad + 98f9277 commit 4831117
Show file tree
Hide file tree
Showing 5 changed files with 474 additions and 0 deletions.
46 changes: 46 additions & 0 deletions speech/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

# Google Cloud Speech API Sample (REST API)

This example demos accessing the [Google Cloud Speech API](http://cloud.google.com/speech)
via its REST API.

## Prerequisites

### Enable the Speech API

If you have not already done so,
[enable the Google Cloud Speech API for your project](https://console.cloud.google.com/apis/api/speech.googleapis.com/overview).
You must be whitelisted to do this.


### Set Up to Authenticate With Your Project's Credentials

The example uses a service account for OAuth2 authentication.
So next, set up to authenticate with the Speech API using your project's
service account credentials.

Visit the [Cloud Console](https://console.cloud.google.com), and navigate to:
`API Manager > Credentials > Create credentials >
Service account key > New service account`.
Create a new service account, and download the json credentials file.

Then, set
the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to your
downloaded service account credentials before running this example:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials-key.json

If you do not do this, you will see an error that looks something like this when
you run the example script:
`...<HttpError 403 when requesting https://speech.googleapis.com/v1/speech:recognize?alt=json returned "Request had insufficient authentication scopes.">`.
See the
[Cloud Platform Auth Guide](https://cloud.google.com/docs/authentication#developer_workflow)
for more information.

## Run the example

```sh
$ python speechrest.py resources/audio.raw
```

You should see a response with the transcription result.
Binary file added speech/api/resources/audio.raw
Binary file not shown.
Binary file added speech/api/resources/audio2.raw
Binary file not shown.
342 changes: 342 additions & 0 deletions speech/api/speech-discovery_google_rest_v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
{
"kind": "discovery#restDescription",
"discoveryVersion": "v1",
"id": "speech:v1",
"name": "speech",
"version": "v1",
"revision": "0",
"title": "Google Cloud Speech API",
"description": "Google Cloud Speech API.",
"ownerDomain": "google.com",
"ownerName": "Google",
"icons": {
"x16": "http://www.google.com/images/icons/product/search-16.gif",
"x32": "http://www.google.com/images/icons/product/search-32.gif"
},
"documentationLink": "https://cloud.google.com/speech",
"protocol": "rest",
"rootUrl": "https://speech.googleapis.com/",
"servicePath": "",
"baseUrl": "https://speech.googleapis.com/",
"batchPath": "batch",
"version_module": "True",
"parameters": {
"access_token": {
"type": "string",
"description": "OAuth access token.",
"location": "query"
},
"alt": {
"type": "string",
"description": "Data format for response.",
"default": "json",
"enum": [
"json",
"media",
"proto"
],
"enumDescriptions": [
"Responses with Content-Type of application/json",
"Media download with context-dependent Content-Type",
"Responses with Content-Type of application/x-protobuf"
],
"location": "query"
},
"bearer_token": {
"type": "string",
"description": "OAuth bearer token.",
"location": "query"
},
"callback": {
"type": "string",
"description": "JSONP",
"location": "query"
},
"fields": {
"type": "string",
"description": "Selector specifying which fields to include in a partial response.",
"location": "query"
},
"key": {
"type": "string",
"description": "API key. Your API key identifies your project and provides you with API access, quota, and reports. Required unless you provide an OAuth 2.0 token.",
"location": "query"
},
"oauth_token": {
"type": "string",
"description": "OAuth 2.0 token for the current user.",
"location": "query"
},
"pp": {
"type": "boolean",
"description": "Pretty-print response.",
"default": "true",
"location": "query"
},
"prettyPrint": {
"type": "boolean",
"description": "Returns response with indentations and line breaks.",
"default": "true",
"location": "query"
},
"quotaUser": {
"type": "string",
"description": "Available to use for quota purposes for server-side applications. Can be any arbitrary string assigned to a user, but should not exceed 40 characters.",
"location": "query"
},
"upload_protocol": {
"type": "string",
"description": "Upload protocol for media (e.g. \"raw\", \"multipart\").",
"location": "query"
},
"uploadType": {
"type": "string",
"description": "Legacy upload protocol for media (e.g. \"media\", \"multipart\").",
"location": "query"
},
"$.xgafv": {
"type": "string",
"description": "V1 error format.",
"enum": [
"1",
"2"
],
"enumDescriptions": [
"v1 error format",
"v2 error format"
],
"location": "query"
}
},
"schemas": {
"RecognizeRequest": {
"id": "RecognizeRequest",
"description": "`RecognizeRequest` is the only message type sent by the client.\n\n`NonStreamingRecognize` sends only one `RecognizeRequest` message and it\nmust contain both an `initial_request` and an 'audio_request`.\n\nStreaming `Recognize` sends one or more `RecognizeRequest` messages. The\nfirst message must contain an `initial_request` and may contain an\n'audio_request`. Any subsequent messages must not contain an\n`initial_request` and must contain an 'audio_request`.",
"type": "object",
"properties": {
"initialRequest": {
"description": "The `initial_request` message provides information to the recognizer\nthat specifies how to process the request.\n\nThe first `RecognizeRequest` message must contain an `initial_request`.\nAny subsequent `RecognizeRequest` messages must not contain an\n`initial_request`.",
"$ref": "InitialRecognizeRequest"
},
"audioRequest": {
"description": "The audio data to be recognized. For `NonStreamingRecognize`, all the\naudio data must be contained in the first (and only) `RecognizeRequest`\n message. For streaming `Recognize`, sequential chunks of audio data are\nsent in sequential `RecognizeRequest` messages.",
"$ref": "AudioRequest"
}
}
},
"InitialRecognizeRequest": {
"id": "InitialRecognizeRequest",
"description": "The `InitialRecognizeRequest` message provides information to the recognizer\nthat specifies how to process the request.",
"type": "object",
"properties": {
"encoding": {
"description": "[Required] Encoding of audio data sent in all `AudioRequest` messages.",
"enumDescriptions": [
"Not specified. Will return result `INVALID_ARGUMENT`.",
"Uncompressed 16-bit signed little-endian samples.\nThis is the simplest encoding format, useful for getting started.\nHowever, because it is uncompressed, it is not recommended for deployed\nclients.",
"This is the recommended encoding format because it uses lossless\ncompression; therefore recognition accuracy is not compromised by a lossy\ncodec.\n\nThe stream FLAC format is specified at:\nhttp:\/\/flac.sourceforge.net\/documentation.html.\nOnly 16-bit samples are supported.\nNot all fields in STREAMINFO are supported.",
"8-bit samples that compand 14-bit audio samples using PCMU\/mu-law.",
"Adaptive Multi-Rate Narrowband codec. `sample_rate` must be 8000 Hz.",
"Adaptive Multi-Rate Wideband codec. `sample_rate` must be 16000 Hz."
],
"type": "string",
"enum": [
"ENCODING_UNSPECIFIED",
"LINEAR16",
"FLAC",
"MULAW",
"AMR",
"AMR_WB"
]
},
"sampleRate": {
"description": "[Required] Sample rate in Hertz of the audio data sent in all\nAudioRequest messages.\n16000 is optimal. Valid values are: 8000-48000.",
"type": "integer",
"format": "int32"
},
"languageCode": {
"description": "[Optional] The language of the supplied audio as a BCP-47 language tag.\nExample: \"en-GB\" https:\/\/www.rfc-editor.org\/rfc\/bcp\/bcp47.txt\nIf omitted, defaults to \"en-US\".",
"type": "string"
},
"maxAlternatives": {
"description": "[Optional] Maximum number of recognition hypotheses to be returned.\nSpecifically, the maximum number of `SpeechRecognitionAlternative` messages\nwithin each `SpeechRecognitionResult`.\nThe server may return fewer than `max_alternatives`.\nValid values are `0`-`30`. A value of `0` or `1` will return a maximum of\n`1`. If omitted, defaults to `1`.",
"type": "integer",
"format": "int32"
},
"profanityFilter": {
"description": "[Optional] If set to `true`, the server will attempt to filter out\nprofanities, replacing all but the initial character in each filtered word\nwith asterisks, e.g. \"f***\". If set to `false` or omitted, profanities\nwon't be filtered out.\nNote that profanity filtering is not implemented for all languages.\nIf the language is not supported, this setting has no effect.",
"type": "boolean"
},
"continuous": {
"description": "[Optional] If `false` or omitted, the recognizer will detect a single\nspoken utterance, and it will cease recognition when the user stops\nspeaking. If `enable_endpointer_events` is `true`, it will return\n`END_OF_UTTERANCE` when it detects that the user has stopped speaking.\nIn all cases, it will return no more than one `SpeechRecognitionResult`,\nand set the `is_final` flag to `true`.\n\nIf `true`, the recognizer will continue recognition (even if the user\npauses speaking) until the client sends an `end_of_data` message or when\nthe maximum time limit has been reached. Multiple\n`SpeechRecognitionResult`s with the `is_final` flag set to `true` may be\nreturned to indicate that the recognizer will not return any further\nhypotheses for this portion of the transcript.",
"type": "boolean"
},
"interimResults": {
"description": "[Optional] If this parameter is `true`, interim results may be returned as\nthey become available.\nIf `false` or omitted, only `is_final=true` result(s) are returned.",
"type": "boolean"
},
"enableEndpointerEvents": {
"description": "[Optional] If this parameter is `true`, `EndpointerEvents` may be returned\nas they become available.\nIf `false` or omitted, no `EndpointerEvents` are returned.",
"type": "boolean"
}
}
},
"AudioRequest": {
"id": "AudioRequest",
"description": "Contains audio data in the format specified in the `InitialRecognizeRequest`.",
"type": "object",
"properties": {
"content": {
"description": "[Required] The audio data bytes encoded as specified in\n`InitialRecognizeRequest`.",
"type": "string",
"format": "byte"
}
}
},
"NonStreamingRecognizeResponse": {
"id": "NonStreamingRecognizeResponse",
"description": "`NonStreamingRecognizeResponse` is the only message returned to the client by\n`NonStreamingRecognize`. It contains the result as zero or more sequential\n`RecognizeResponse` messages.\n\nNote that streaming `Recognize` will also return multiple `RecognizeResponse`\nmessages, but each message is individually streamed.",
"type": "object",
"properties": {
"responses": {
"description": "[Output-only] Sequential list of messages returned by the recognizer.",
"type": "array",
"items": {
"$ref": "RecognizeResponse"
}
}
}
},
"RecognizeResponse": {
"id": "RecognizeResponse",
"description": "`RecognizeResponse` is the only message type returned to the client.",
"type": "object",
"properties": {
"error": {
"description": "[Output-only] If set, returns a google.rpc.Status message that\nspecifies the error for the operation.",
"$ref": "Status"
},
"results": {
"description": "[Output-only] May contain zero or one `is_final=true` result (the newly\nsettled portion). May also contain zero or more `is_final=false` results.",
"type": "array",
"items": {
"$ref": "SpeechRecognitionResult"
}
},
"resultIndex": {
"description": "[Output-only] Indicates the lowest index in the `results` array that has\nchanged. The repeated `SpeechRecognitionResult` results overwrite past\nresults at this index and higher.",
"type": "integer",
"format": "int32"
},
"endpoint": {
"description": "[Output-only] Indicates the type of endpointer event.",
"enumDescriptions": [
"No endpointer event specified.",
"Speech has been detected in the audio stream.",
"Speech has ceased to be detected in the audio stream.",
"The end of the audio stream has been reached. and it is being processed.",
"This event is only sent when continuous is `false`. It indicates that the\nserver has detected the end of the user's speech utterance and expects no\nadditional speech. Therefore, the server will not process additional\naudio. The client should stop sending additional audio data."
],
"type": "string",
"enum": [
"ENDPOINTER_EVENT_UNSPECIFIED",
"START_OF_SPEECH",
"END_OF_SPEECH",
"END_OF_AUDIO",
"END_OF_UTTERANCE"
]
}
}
},
"Status": {
"id": "Status",
"description": "The `Status` type defines a logical error model that is suitable for different\nprogramming environments, including REST APIs and RPC APIs. It is used by\n[gRPC](https:\/\/github.com\/grpc). The error model is designed to be:\n\n- Simple to use and understand for most users\n- Flexible enough to meet unexpected needs\n\n# Overview\n\nThe `Status` message contains three pieces of data: error code, error message,\nand error details. The error code should be an enum value of\ngoogle.rpc.Code, but it may accept additional error codes if needed. The\nerror message should be a developer-facing English message that helps\ndevelopers *understand* and *resolve* the error. If a localized user-facing\nerror message is needed, put the localized message in the error details or\nlocalize it in the client. The optional error details may contain arbitrary\ninformation about the error. There is a predefined set of error detail types\nin the package `google.rpc` which can be used for common error conditions.\n\n# Language mapping\n\nThe `Status` message is the logical representation of the error model, but it\nis not necessarily the actual wire format. When the `Status` message is\nexposed in different client libraries and different wire protocols, it can be\nmapped differently. For example, it will likely be mapped to some exceptions\nin Java, but more likely mapped to some error codes in C.\n\n# Other uses\n\nThe error model and the `Status` message can be used in a variety of\nenvironments, either with or without APIs, to provide a\nconsistent developer experience across different environments.\n\nExample uses of this error model include:\n\n- Partial errors. If a service needs to return partial errors to the client,\n it may embed the `Status` in the normal response to indicate the partial\n errors.\n\n- Workflow errors. A typical workflow has multiple steps. Each step may\n have a `Status` message for error reporting purpose.\n\n- Batch operations. If a client uses batch request and batch response, the\n `Status` message should be used directly inside batch response, one for\n each error sub-response.\n\n- Asynchronous operations. If an API call embeds asynchronous operation\n results in its response, the status of those operations should be\n represented directly using the `Status` message.\n\n- Logging. If some API errors are stored in logs, the message `Status` could\n be used directly after any stripping needed for security\/privacy reasons.",
"type": "object",
"properties": {
"code": {
"description": "The status code, which should be an enum value of google.rpc.Code.",
"type": "integer",
"format": "int32"
},
"message": {
"description": "A developer-facing error message, which should be in English. Any\nuser-facing error message should be localized and sent in the\ngoogle.rpc.Status.details field, or localized by the client.",
"type": "string"
},
"details": {
"description": "A list of messages that carry the error details. There will be a\ncommon set of message types for APIs to use.",
"type": "array",
"items": {
"type": "object",
"additionalProperties": {
"type": "any",
"description": "Properties of the object. Contains field @ype with type URL."
}
}
}
}
},
"SpeechRecognitionResult": {
"id": "SpeechRecognitionResult",

"type": "object",
"properties": {
"alternatives": {

"type": "array",
"items": {
"$ref": "SpeechRecognitionAlternative"
}
},
"isFinal": {
"description": "[Output-only] Set `true` if this is the final time the speech service will\nreturn this particular `SpeechRecognitionResult`. If `false`, this\nrepresents an interim result that may change.",
"type": "boolean"
},
"stability": {
"description": "[Output-only] An estimate of the probability that the recognizer will not\nchange its guess about this interim result. Values range from 0.0\n(completely unstable) to 1.0 (completely stable). Note that this is not the\nsame as `confidence`, which estimates the probability that a recognition\nresult is correct.\nThis field is only provided for interim results (`is_final=false`).\nThe default of 0.0 is a sentinel value indicating stability was not set.",
"type": "number",
"format": "float"
}
}
},
"SpeechRecognitionAlternative": {
"id": "SpeechRecognitionAlternative",
"description": "Alternative hypotheses (a.k.a. n-best list).",
"type": "object",
"properties": {
"transcript": {
"description": "[Output-only] Transcript text representing the words that the user spoke.",
"type": "string"
},
"confidence": {
"description": "[Output-only] The confidence estimate between 0.0 and 1.0. A higher number\nmeans the system is more confident that the recognition is correct.\nThis field is typically provided only for the top hypothesis. and only for\n`is_final=true` results.\nThe default of 0.0 is a sentinel value indicating confidence was not set.",
"type": "number",
"format": "float"
}
}
}
},
"resources": {
"speech": {
"methods": {
"recognize": {
"id": "speech.speech.recognize",
"path": "v1/speech:recognize",
"flatPath": "v1/speech:recognize",
"httpMethod": "POST",
"description": "Perform non-streaming speech recognition on audio using HTTPS.",
"parameters": {
},
"parameterOrder": [
],
"request": {
"$ref": "RecognizeRequest"
},
"response": {
"$ref": "NonStreamingRecognizeResponse"
}
}
}
}
},
"basePath": ""
}
Loading

0 comments on commit 4831117

Please sign in to comment.