|
| 1 | +#!/usr/bin/python |
| 2 | +# Copyright (C) 2016 Google Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""Sample that transcribes a FLAC audio file stored in Google Cloud Storage, |
| 16 | +using GRPC.""" |
| 17 | + |
| 18 | +import argparse |
| 19 | + |
| 20 | +from gcloud.credentials import get_credentials |
| 21 | +from google.cloud.speech.v1 import cloud_speech_pb2 as cloud_speech |
| 22 | +from grpc.beta import implementations |
| 23 | + |
| 24 | +# Keep the request alive for this many seconds |
| 25 | +DEADLINE_SECS = 10 |
| 26 | +SPEECH_SCOPE = 'https://www.googleapis.com/auth/cloud-platform' |
| 27 | + |
| 28 | + |
| 29 | +def make_channel(host, port): |
| 30 | + """Creates an SSL channel with auth credentials from the environment.""" |
| 31 | + # In order to make an https call, use an ssl channel with defaults |
| 32 | + ssl_channel = implementations.ssl_channel_credentials(None, None, None) |
| 33 | + |
| 34 | + # Grab application default credentials from the environment |
| 35 | + creds = get_credentials().create_scoped([SPEECH_SCOPE]) |
| 36 | + # Add a plugin to inject the creds into the header |
| 37 | + auth_header = ( |
| 38 | + 'Authorization', |
| 39 | + 'Bearer ' + creds.get_access_token().access_token) |
| 40 | + auth_plugin = implementations.metadata_call_credentials( |
| 41 | + lambda _, cb: cb([auth_header], None), |
| 42 | + name='google_creds') |
| 43 | + |
| 44 | + # compose the two together for both ssl and google auth |
| 45 | + composite_channel = implementations.composite_channel_credentials( |
| 46 | + ssl_channel, auth_plugin) |
| 47 | + |
| 48 | + return implementations.secure_channel(host, port, composite_channel) |
| 49 | + |
| 50 | + |
| 51 | +def main(input_uri, output_uri, encoding, sample_rate): |
| 52 | + service = cloud_speech.beta_create_Speech_stub( |
| 53 | + make_channel('speech.googleapis.com', 443)) |
| 54 | + # The method and parameters can be inferred from the proto from which the |
| 55 | + # grpc client lib was generated. See: |
| 56 | + # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1/cloud_speech.proto |
| 57 | + response = service.NonStreamingRecognize(cloud_speech.RecognizeRequest( |
| 58 | + initial_request=cloud_speech.InitialRecognizeRequest( |
| 59 | + encoding=encoding, |
| 60 | + sample_rate=sample_rate, |
| 61 | + output_uri=output_uri, |
| 62 | + ), |
| 63 | + audio_request=cloud_speech.AudioRequest( |
| 64 | + uri=input_uri, |
| 65 | + ) |
| 66 | + ), DEADLINE_SECS) |
| 67 | + # This shouldn't actually print anything, since the transcription is output |
| 68 | + # to the GCS uri specified |
| 69 | + print(response.responses) |
| 70 | + |
| 71 | + |
| 72 | +def _gcs_uri(text): |
| 73 | + if not text.startswith('gs://'): |
| 74 | + raise ValueError( |
| 75 | + 'Cloud Storage uri must be of the form gs://bucket/path/') |
| 76 | + return text |
| 77 | + |
| 78 | + |
| 79 | +PROTO_URL = ('https://github.com/googleapis/googleapis/blob/master/' |
| 80 | + 'google/cloud/speech/v1/cloud_speech.proto') |
| 81 | +if __name__ == '__main__': |
| 82 | + parser = argparse.ArgumentParser() |
| 83 | + parser.add_argument('input_uri', type=_gcs_uri) |
| 84 | + parser.add_argument('output_uri', type=_gcs_uri) |
| 85 | + parser.add_argument( |
| 86 | + '--encoding', default='FLAC', choices=[ |
| 87 | + 'LINEAR16', 'FLAC', 'MULAW', 'AMR', 'AMR_WB'], |
| 88 | + help='How the audio file is encoded. See {}#L67'.format(PROTO_URL)) |
| 89 | + parser.add_argument('--sample_rate', default=16000) |
| 90 | + |
| 91 | + args = parser.parse_args() |
| 92 | + main(args.input_uri, args.output_uri, args.encoding, args.sample_rate) |
0 commit comments