<s> import contextlib <EOL> import threading <EOL> from gcloud . credentials import get_credentials <EOL> from google . cloud . speech . v1 . cloud_speech_pb2 import * <EOL> from google . rpc import code_pb2 <EOL> from grpc . beta import implementations <EOL> import pyaudio <EOL> RATE = <NUM_LIT> <EOL> CHANNELS = <NUM_LIT:1> <EOL> CHUNK = RATE // <NUM_LIT:10> <EOL> DEADLINE_SECS = <NUM_LIT:8> * <NUM_LIT> * <NUM_LIT> <EOL> SPEECH_SCOPE = '<STR_LIT>' <EOL> def make_channel ( host , port ) : <EOL> """<STR_LIT>""" <EOL> ssl_channel = implementations . ssl_channel_credentials ( None , None , None ) <EOL> creds = get_credentials ( ) . create_scoped ( [ SPEECH_SCOPE ] ) <EOL> auth_header = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' + creds . get_access_token ( ) . access_token ) <EOL> auth_plugin = implementations . metadata_call_credentials ( <EOL> lambda _ , cb : cb ( [ auth_header ] , None ) , <EOL> name = '<STR_LIT>' ) <EOL> composite_channel = implementations . composite_channel_credentials ( <EOL> ssl_channel , auth_plugin ) <EOL> return implementations . secure_channel ( host , port , composite_channel ) <EOL> @ contextlib . contextmanager <EOL> def record_audio ( channels , rate , chunk ) : <EOL> """<STR_LIT>""" <EOL> audio_interface = pyaudio . PyAudio ( ) <EOL> audio_stream = audio_interface . open ( <EOL> format = pyaudio . paInt16 , channels = channels , rate = rate , <EOL> input = True , frames_per_buffer = chunk , <EOL> ) <EOL> yield audio_stream <EOL> audio_stream . stop_stream ( ) <EOL> audio_stream . close ( ) <EOL> audio_interface . terminate ( ) <EOL> def request_stream ( stop_audio , channels = CHANNELS , rate = RATE , chunk = CHUNK ) : <EOL> """<STR_LIT>""" <EOL> with record_audio ( channels , rate , chunk ) as audio_stream : <EOL> metadata = InitialRecognizeRequest ( <EOL> encoding = '<STR_LIT>' , sample_rate = rate ) <EOL> audio_request = AudioRequest ( content = audio_stream . read ( chunk ) ) <EOL> yield RecognizeRequest ( <EOL> initial_request = metadata , <EOL> audio_request = audio_request ) <EOL> while not stop_audio . is_set ( ) : <EOL> audio_request = AudioRequest ( content = audio_stream . read ( chunk ) ) <EOL> yield RecognizeRequest ( audio_request = audio_request ) <EOL> def listen_print_loop ( recognize_stream ) : <EOL> for resp in recognize_stream : <EOL> if resp . error . code != code_pb2 . OK : <EOL> raise RuntimeError ( '<STR_LIT>' + resp . error . message ) <EOL> for result in resp . results : <EOL> print ( result . alternatives ) <EOL> if any ( alt . confidence > <NUM_LIT> and <EOL> ( alt . transcript . strip ( ) in ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> for result in resp . results <EOL> for alt in result . alternatives ) : <EOL> print ( '<STR_LIT>' ) <EOL> return <EOL> def main ( ) : <EOL> stop_audio = threading . Event ( ) <EOL> with beta_create_Speech_stub ( <EOL> make_channel ( '<STR_LIT>' , <NUM_LIT> ) ) as service : <EOL> try : <EOL> listen_print_loop ( <EOL> service . Recognize ( request_stream ( stop_audio ) , DEADLINE_SECS ) ) <EOL> finally : <EOL> stop_audio . set ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
