|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -import time |
| 15 | +import os |
| 16 | +import io |
| 17 | +import requests |
16 | 18 |
|
17 | 19 | from google.cloud import speech_v1
|
18 |
| -from google.cloud.speech_v1 import enums |
19 |
| -from google.cloud.speech_v1.proto import cloud_speech_pb2 |
20 | 20 |
|
21 | 21 |
|
22 | 22 | class TestSystemSpeech(object):
|
23 | 23 | def test_recognize(self):
|
24 | 24 |
|
| 25 | + try: |
| 26 | + BUCKET = os.environ["GOOGLE_CLOUD_TESTS_SPEECH_BUCKET"] |
| 27 | + except KeyError: |
| 28 | + BUCKET = "cloud-samples-tests" |
| 29 | + |
25 | 30 | client = speech_v1.SpeechClient()
|
26 |
| - language_code = "en-US" |
27 |
| - sample_rate_hertz = 44100 |
28 |
| - encoding = enums.RecognitionConfig.AudioEncoding.FLAC |
| 31 | + |
29 | 32 | config = {
|
30 |
| - "language_code": language_code, |
31 |
| - "sample_rate_hertz": sample_rate_hertz, |
32 |
| - "encoding": encoding, |
| 33 | + "encoding": speech_v1.enums.RecognitionConfig.AudioEncoding.FLAC, |
| 34 | + "language_code": "en-US", |
| 35 | + "sample_rate_hertz": 16000, |
33 | 36 | }
|
34 |
| - uri = "gs://gapic-toolkit/hello.flac" |
| 37 | + |
| 38 | + uri = "gs://{}/speech/brooklyn.flac".format(BUCKET) |
35 | 39 | audio = {"uri": uri}
|
| 40 | + |
36 | 41 | response = client.recognize(config, audio)
|
| 42 | + |
| 43 | + assert response.results[0].alternatives[0].transcript is not None |
| 44 | + |
| 45 | + def test_long_running_recognize(self): |
| 46 | + |
| 47 | + try: |
| 48 | + BUCKET = os.environ["GOOGLE_CLOUD_TESTS_SPEECH_BUCKET"] |
| 49 | + except KeyError: |
| 50 | + BUCKET = "cloud-samples-tests" |
| 51 | + |
| 52 | + client = speech_v1.SpeechClient() |
| 53 | + |
| 54 | + config = speech_v1.types.RecognitionConfig( |
| 55 | + encoding=speech_v1.enums.RecognitionConfig.AudioEncoding.FLAC, |
| 56 | + language_code="en-US", |
| 57 | + sample_rate_hertz=16000, |
| 58 | + ) |
| 59 | + |
| 60 | + uri = "gs://{}/speech/brooklyn.flac".format(BUCKET) |
| 61 | + audio = {"uri": uri} |
| 62 | + |
| 63 | + response = client.long_running_recognize(config, audio) |
| 64 | + |
| 65 | + assert response.result() is not None |
| 66 | + |
| 67 | + def test_streaming_recognize(self): |
| 68 | + |
| 69 | + try: |
| 70 | + BUCKET = os.environ["GOOGLE_CLOUD_TESTS_SPEECH_BUCKET"] |
| 71 | + except KeyError: |
| 72 | + BUCKET = "cloud-samples-tests" |
| 73 | + |
| 74 | + client = speech_v1.SpeechClient() |
| 75 | + |
| 76 | + config = speech_v1.types.RecognitionConfig( |
| 77 | + encoding=speech_v1.enums.RecognitionConfig.AudioEncoding.FLAC, |
| 78 | + language_code="en-US", |
| 79 | + sample_rate_hertz=16000, |
| 80 | + ) |
| 81 | + streamingConfig = speech_v1.types.StreamingRecognitionConfig(config=config) |
| 82 | + |
| 83 | + uri = "https://storage.googleapis.com/{}/speech/brooklyn.flac".format(BUCKET) |
| 84 | + streaming_requests = [ |
| 85 | + speech_v1.types.StreamingRecognizeRequest( |
| 86 | + audio_content=requests.get(uri).content |
| 87 | + ) |
| 88 | + ] |
| 89 | + |
| 90 | + responses = client.streaming_recognize(streamingConfig, streaming_requests) |
| 91 | + |
| 92 | + for response in responses: |
| 93 | + for result in response.results: |
| 94 | + assert result.alternatives[0].transcript is not None |
0 commit comments