|
| 1 | +# Copyright 2016 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import unittest |
| 17 | + |
| 18 | +from google.cloud import exceptions |
| 19 | +from google.cloud import speech |
| 20 | +from google.cloud import storage |
| 21 | +from google.cloud.speech.transcript import Transcript |
| 22 | + |
| 23 | +from system_test_utils import unique_resource_id |
| 24 | +from retry import RetryErrors |
| 25 | + |
| 26 | +AUDIO_FILE = os.path.join(os.path.dirname(__file__), 'data', 'hello.wav') |
| 27 | + |
| 28 | + |
| 29 | +class Config(object): |
| 30 | + """Run-time configuration to be modified at set-up. |
| 31 | +
|
| 32 | + This is a mutable stand-in to allow test set-up to modify |
| 33 | + global state. |
| 34 | + """ |
| 35 | + CLIENT = None |
| 36 | + TEST_BUCKET = None |
| 37 | + |
| 38 | + |
| 39 | +def setUpModule(): |
| 40 | + Config.CLIENT = speech.Client() |
| 41 | + # Now create a bucket for GCS stored content. |
| 42 | + storage_client = storage.Client() |
| 43 | + bucket_name = 'new' + unique_resource_id() |
| 44 | + Config.TEST_BUCKET = storage_client.bucket(bucket_name) |
| 45 | + # 429 Too Many Requests in case API requests rate-limited. |
| 46 | + retry_429 = RetryErrors(exceptions.TooManyRequests) |
| 47 | + retry_429(Config.TEST_BUCKET.create)() |
| 48 | + |
| 49 | + |
| 50 | +def tearDownModule(): |
| 51 | + # 409 Conflict if the bucket is full. |
| 52 | + # 429 Too Many Requests in case API requests rate-limited. |
| 53 | + bucket_retry = RetryErrors( |
| 54 | + (exceptions.TooManyRequests, exceptions.Conflict)) |
| 55 | + bucket_retry(Config.TEST_BUCKET.delete)(force=True) |
| 56 | + |
| 57 | + |
| 58 | +class TestSpeechClient(unittest.TestCase): |
| 59 | + ASSERT_TEXT = 'thank you for using Google Cloud platform' |
| 60 | + |
| 61 | + def setUp(self): |
| 62 | + self.to_delete_by_case = [] |
| 63 | + |
| 64 | + def tearDown(self): |
| 65 | + for value in self.to_delete_by_case: |
| 66 | + value.delete() |
| 67 | + |
| 68 | + def _make_sync_request(self, content=None, source_uri=None, |
| 69 | + max_alternatives=None): |
| 70 | + client = Config.CLIENT |
| 71 | + sample = client.sample(content=content, |
| 72 | + source_uri=source_uri, |
| 73 | + encoding=speech.Encoding.LINEAR16, |
| 74 | + sample_rate=16000) |
| 75 | + result = client.sync_recognize(sample, |
| 76 | + language_code='en-US', |
| 77 | + max_alternatives=max_alternatives, |
| 78 | + profanity_filter=True, |
| 79 | + speech_context=['Google', 'cloud']) |
| 80 | + return result |
| 81 | + |
| 82 | + def _check_best_results(self, results): |
| 83 | + top_result = results[0] |
| 84 | + self.assertIsInstance(top_result, Transcript) |
| 85 | + self.assertEqual(top_result.transcript, |
| 86 | + 'hello ' + self.ASSERT_TEXT) |
| 87 | + self.assertGreater(top_result.confidence, 0.90) |
| 88 | + |
| 89 | + def test_sync_recognize_local_file(self): |
| 90 | + with open(Config.AUDIO_FILE, 'rb') as file_obj: |
| 91 | + results = self._make_sync_request(content=file_obj.read(), |
| 92 | + max_alternatives=2) |
| 93 | + second_alternative = results[1] |
| 94 | + self.assertEqual(len(results), 2) |
| 95 | + self._check_best_results(results) |
| 96 | + self.assertIsInstance(second_alternative, Transcript) |
| 97 | + self.assertEqual(second_alternative.transcript, self.ASSERT_TEXT) |
| 98 | + self.assertEqual(second_alternative.confidence, 0.0) |
| 99 | + |
| 100 | + def test_sync_recognize_gcs_file(self): |
| 101 | + bucket_name = Config.TEST_BUCKET.name |
| 102 | + blob_name = 'hello.wav' |
| 103 | + blob = Config.TEST_BUCKET.blob(blob_name) |
| 104 | + self.to_delete_by_case.append(blob) # Clean-up. |
| 105 | + with open(Config.AUDIO_FILE, 'rb') as file_obj: |
| 106 | + blob.upload_from_file(file_obj) |
| 107 | + |
| 108 | + source_uri = 'gs://%s/%s' % (bucket_name, blob_name) |
| 109 | + result = self._make_sync_request(source_uri=source_uri, |
| 110 | + max_alternatives=1) |
| 111 | + self._check_best_results(result) |
0 commit comments