|
30 | 30 | def transcribe_file(speech_file): |
31 | 31 | """Transcribe the given audio file asynchronously.""" |
32 | 32 | from google.cloud import speech |
33 | | - speech_client = speech.Client() |
| 33 | + from google.cloud.speech import enums |
| 34 | + from google.cloud.speech import types |
| 35 | + client = speech.SpeechClient() |
34 | 36 |
|
| 37 | + # [START migration_async_request] |
35 | 38 | with io.open(speech_file, 'rb') as audio_file: |
36 | 39 | content = audio_file.read() |
37 | | - audio_sample = speech_client.sample( |
38 | | - content, |
39 | | - source_uri=None, |
40 | | - encoding='LINEAR16', |
41 | | - sample_rate_hertz=16000) |
42 | 40 |
|
43 | | - operation = audio_sample.long_running_recognize('en-US') |
| 41 | + audio = types.RecognitionAudio(content=content) |
| 42 | + config = types.RecognitionConfig( |
| 43 | + encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16, |
| 44 | + sample_rate_hertz=16000, |
| 45 | + language_code='en-US') |
44 | 46 |
|
| 47 | + # [START migration_async_response] |
| 48 | + operation = client.long_running_recognize(config, audio) |
| 49 | + # [END migration_async_request] |
| 50 | + |
| 51 | + # Sleep and poll operation.done() |
45 | 52 | retry_count = 100 |
46 | | - while retry_count > 0 and not operation.complete: |
| 53 | + while retry_count > 0 and not operation.done(): |
47 | 54 | retry_count -= 1 |
48 | 55 | time.sleep(2) |
49 | | - operation.poll() |
50 | 56 |
|
51 | | - if not operation.complete: |
| 57 | + if not operation.done(): |
52 | 58 | print('Operation not complete and retry limit reached.') |
53 | 59 | return |
54 | 60 |
|
55 | | - alternatives = operation.results |
| 61 | + alternatives = operation.result().results[0].alternatives |
56 | 62 | for alternative in alternatives: |
57 | 63 | print('Transcript: {}'.format(alternative.transcript)) |
58 | 64 | print('Confidence: {}'.format(alternative.confidence)) |
59 | | - # [END send_request] |
| 65 | + # [END migration_async_response] |
60 | 66 |
|
61 | 67 |
|
62 | 68 | def transcribe_gcs(gcs_uri): |
63 | 69 | """Asynchronously transcribes the audio file specified by the gcs_uri.""" |
64 | 70 | from google.cloud import speech |
65 | | - speech_client = speech.Client() |
| 71 | + from google.cloud.speech import enums |
| 72 | + from google.cloud.speech import types |
| 73 | + client = speech.SpeechClient() |
66 | 74 |
|
67 | | - audio_sample = speech_client.sample( |
68 | | - content=None, |
69 | | - source_uri=gcs_uri, |
70 | | - encoding='FLAC', |
71 | | - sample_rate_hertz=16000) |
| 75 | + audio = types.RecognitionAudio(uri=gcs_uri) |
| 76 | + config = types.RecognitionConfig( |
| 77 | + encoding=enums.RecognitionConfig.AudioEncoding.FLAC, |
| 78 | + sample_rate_hertz=16000, |
| 79 | + language_code='en-US') |
72 | 80 |
|
73 | | - operation = audio_sample.long_running_recognize('en-US') |
| 81 | + operation = client.long_running_recognize(config, audio) |
74 | 82 |
|
75 | 83 | retry_count = 100 |
76 | | - while retry_count > 0 and not operation.complete: |
| 84 | + while retry_count > 0 and not operation.done(): |
77 | 85 | retry_count -= 1 |
78 | 86 | time.sleep(2) |
79 | | - operation.poll() |
80 | 87 |
|
81 | | - if not operation.complete: |
| 88 | + if not operation.done(): |
82 | 89 | print('Operation not complete and retry limit reached.') |
83 | 90 | return |
84 | 91 |
|
85 | | - alternatives = operation.results |
| 92 | + alternatives = operation.result().results[0].alternatives |
86 | 93 | for alternative in alternatives: |
87 | 94 | print('Transcript: {}'.format(alternative.transcript)) |
88 | 95 | print('Confidence: {}'.format(alternative.confidence)) |
89 | | - # [END send_request_gcs] |
90 | 96 |
|
91 | 97 |
|
92 | 98 | if __name__ == '__main__': |
|
0 commit comments