puja garg
puja garg

Reputation: 393

How to send text input to google assistant sdk in python

while converting text to audio and then sending to google assistant pushtalk is not giving accurate result. So i need suggestion, how i can send text data to google assistant textinput to get more accurate result. Or any other suggestion which can increase my accuracy in python

this is what i am currently installed

"pip install --upgrade setuptools wheel \ google-assistant-library \ google-assistant-sdk[samples] \ google-auth-oauthlib[tool] "

this what i am current using to convert text to audio and then sending to google-assistant

    def post(self):
        args = request.get_json()
        text_received = str(args['assitant_text'])
        file_name = str(args['google_file_name'])
        project_id = str(args['project_id'])
        device_model_id = str(args['device_model_id'])
        refresh_token = str(args['refresh_token'])
        client_id = str(args['client_id'])
        client_secret = str(args['client_secret'])
        voice = str(args['voice'])
        text_received = "WHat is the capital of karnataka"
        logger.emit("Received request parameters from api",
                        {
                            "api": "/google",
                            "args": args
                        }
                    )
        token = {"refresh_token": refresh_token, "token_uri": "https://accounts.google.com/o/oauth2/token", "client_id": client_id, "client_secret": client_secret, "scopes": ["https://www.googleapis.com/auth/assistant-sdk-prototype"]}
        with open('credentials.json', 'w') as outfile:
            json.dump(token, outfile)
        status = subprocess.call('aws polly synthesize-speech --output-format mp3 --voice-id %s --text "%s"  %s.mp3' %(voice, text_received, file_name), shell=True, stdout=subprocess.PIPE)
        logger.emit("Calling aws polly",
                        {
                            "api": "/google",
                            "status_code": status
                        }
                    )
        tf = open("%s.mp3" % file_name, 'r')
        _input = AudioSegment.from_file(tf.name)
        tf = tempfile.NamedTemporaryFile(suffix=".wav", delete=True)
        output = _input.set_channels(1).set_frame_rate(16000)
        f = output.export(tf.name, format="wav")
        status = subprocess.call('googlesamples-assistant-pushtotalk --credentials credentials.json --device-config device_config.json --project-id %s --device-model-id %s -i %s -o %s_output.wav' %(project_id, device_model_id, f.name, file_name), shell=True, stdout=subprocess.PIPE)
        logger.emit("Calling google samples assistant pushtotalk",
                        {
                            "api": "/google",
                            "status": status
                        }
                    )
        as_output = AudioSegment.from_file("%s_output.wav" %file_name)

        kf = tempfile.NamedTemporaryFile(suffix=".wav")
        output = as_output.set_channels(2)
        f = output.export(kf.name, format="wav")
        r = sr.Recognizer()
        with sr.AudioFile(f.name) as source:
          output_audio = r.record(source)
        text = r.recognize_google(output_audio)
        f.close()
        kf.close()
        os.remove("%s_output.wav" %file_name)
        os.remove("%s.mp3" %file_name)
        logger.emit("api completed successfully",
                        {
                            "api": "/google",
                            "status": "completed"
                        }
                    )
        return jsonify({"response": text})

Upvotes: 1

Views: 3738

Answers (1)

Nick Felker
Nick Felker

Reputation: 11978

You can send text queries using the API directly instead of trying to convert the audio.

There's also a sample for text input that you should be able to access if you download the sample project:

cd google-assistant-sdk/googlesamples/assistant/grpc

python -m textinput --device-id 'my-device-identifier' --device-model-id 'my-model-identifier'

Upvotes: 1

Related Questions