'DialogFlow streaming audio from microphone - realtime

I've been triying to use this dialogflow code from this git https://gist.github.com/dalequark/aa0385de55b97fe1e2950b6e1ead063c in order to test audio streaming from my microphone. I made a couple of changes to the original code, related to the import dialogflow part. This is the change: import google.cloud.dialogflow as dialogflow

I'm using VS code and install the pyaudio, simpleaudio and the other libraries. However, when trying to test the program, it throws the following error. It seems related to the line 83, exactly the request treaming_detect_intent(requests).

    print('=' * 20)
    requests = __request_generator()
    responses = session_client.streaming_detect_intent(requests)

I'm not python expert. Any thought? what could be the reason for the error? Thanks. Output:

   c:/Users/Administrador/git/4648c110b02963a049da2bfa637493fb/mic.py --project-id agent1-tdge --session-id 123 --language-code es

====================
Traceback (most recent call last):
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\grpc_helpers.py", line 156, in error_remapped_callable
    return _StreamingResponseIterator(
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\grpc_helpers.py", line 82, in __init__
    self._stored_first_result = next(self._wrapped)
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python310\lib\site-packages\grpc\_channel.py", line 426, in __next__
    return self._next()
  File "C:\Users\Administrador\AppData\Local\Programs\Python\Python310\lib\site-packages\grpc\_channel.py", line 826, in _next
    raise self
grpc._channel._MultiThreadedRendezvous: <_MultiThreadedRendezvous of RPC that terminated with:
        status = StatusCode.UNKNOWN
        details = "Exception iterating requests!"
        debug_error_string = "None"
>

The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
      File "c:\Users\Administrador\git\4648c110b02963a049da2bfa637493fb\mic.py", line 124, in <module>
        main()
      File "c:\Users\Administrador\git\4648c110b02963a049da2bfa637493fb\mic.py", line 102, in main
        response = grab_intent(args.project_id, args.session_id, args.language_code)
      File "c:\Users\Administrador\git\4648c110b02963a049da2bfa637493fb\mic.py", line 83, in grab_intent
        responses = session_client.streaming_detect_intent(requests)
      File "C:\Users\Administrador\AppData\Local\Programs\Python\Python310\lib\site-packages\google\cloud\dialogflow_v2\services\sessions\client.py", line 623, in streaming_detect_intent
        response = rpc(requests, retry=retry, timeout=timeout, metadata=metadata,)
      File "C:\Users\Administrador\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\gapic_v1\method.py", line 154, in __call__
        return wrapped_func(*args, **kwargs)
      File "C:\Users\Administrador\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\grpc_helpers.py", line 160, in error_remapped_callable
        raise exceptions.from_grpc_error(exc) from exc
    google.api_core.exceptions.Unknown: None Exception iterating requests!
    PS C:\Users\Administrador\git\4648c110b02963a049da2bfa637493fb> 


    Python code in this github: 

        
    import google.cloud.dialogflow  as dialogflow
    import pyaudio
    import simpleaudio as sa
    import argparse
    import uuid
    import os
    
    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'xxxxxe-f758e0aeef9f.json'
    # Audio recording parameters
    SAMPLE_RATE = 16000
    CHUNK_SIZE = int(SAMPLE_RATE / 10)
    
    
    def grab_intent(projectId, sessionId, languageCode):
        """Start stream from microphone input to dialogflow API"""
       
        session_client = dialogflow.SessionsClient()
        # Audio output stream
        
        final_request_received = False
    
        def __request_generator():
            input_stream = pyaudio.PyAudio().open(channels=1,
                    rate=SAMPLE_RATE, format=pyaudio.paInt16, input=True)
            audio_encoding = dialogflow.enums.AudioEncoding.AUDIO_ENCODING_LINEAR_16
            session_path = session_client.session_path(projectId, sessionId)
            print('Session path: {}\n'.format(session_path))
            
            input_audio_config = dialogflow.types.InputAudioConfig(audio_encoding=audio_encoding, 
                language_code=languageCode,
                sample_rate_hertz=SAMPLE_RATE)
            speech_config = dialogflow.types.SynthesizeSpeechConfig(
                voice=dialogflow.types.VoiceSelectionParams(ssml_gender=dialogflow.enums.SsmlVoiceGender.SSML_VOICE_GENDER_FEMALE))
            output_audio_config = dialogflow.types.OutputAudioConfig(
                audio_encoding=dialogflow.enums.OutputAudioEncoding.OUTPUT_AUDIO_ENCODING_LINEAR_16,
                sample_rate_hertz=SAMPLE_RATE,
                synthesize_speech_config=speech_config)
            query_input = dialogflow.types.QueryInput(audio_config=input_audio_config)
    
            # The first request contains the configuration.
            yield dialogflow.types.StreamingDetectIntentRequest(
                session=session_path, query_input=query_input, output_audio_config=output_audio_config)
    
            while True:
                if final_request_received:
                    print("received final request")
                    input_stream.close()
                    print("closed stream")
                    return
                if input_stream.is_active():
                    content = input_stream.read(CHUNK_SIZE, exception_on_overflow = False)
                    yield dialogflow.types.StreamingDetectIntentRequest(input_audio=content)
            
    
    
    
        while True:
            print('=' * 20)
            requests = __request_generator()
            responses = session_client.streaming_detect_intent(requests)
    
            for response in responses:
                print(f'Intermediate transcription result: {response.recognition_result.transcript}')
                if response.recognition_result.is_final:
                    final_request_received = True
                if response.query_result.query_text:
                    print(f'Fullfilment Text: {response.query_result.fulfillment_text}')
                    print(f'Intent: {response.query_result.intent}')
                if response.output_audio:
                    return response
    
    
    def play_audio(audio):
        audio_obj = sa.play_buffer(audio, 1, 2, SAMPLE_RATE)
        audio_obj.wait_done()
    
    def main():
        while(True):
            response = grab_intent(args.project_id, args.session_id, args.language_code)
            play_audio(response.output_audio)
    
    if __name__ == "__main__":
        parser = argparse.ArgumentParser(
            description=__doc__,
            formatter_class=argparse.RawDescriptionHelpFormatter)
        parser.add_argument(
            '--project-id',
            help='Project/agent id.  Required.',
            required=True)
        parser.add_argument(
            '--session-id',
            help='Identifier of the DetectIntent session. '
                 'Defaults to a random UUID.',
            default=str(uuid.uuid4()))
        parser.add_argument(
            '--language-code',
            help='Language code of the query. Defaults to "en-US".',
            default='en-US')
    
        args = parser.parse_args()
        main() 
 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source