【问题标题】:How to send text input to google assistant sdk in python如何在python中将文本输入发送到谷歌助手sdk
【发布时间】:2020-08-30 18:19:15
【问题描述】:

在将文本转换为音频然后发送到谷歌助手 pushtalk 时不会给出准确的结果。所以我需要建议,我如何将文本数据发送到谷歌助手 textinput 以获得更准确的结果。或任何其他可以提高我在 python 中的准确性的建议

这是我目前安装的

"pip install --upgrade setuptools wheel \ 谷歌助理图书馆\ google-assistant-sdk [示例] \ google-auth-oauthlib[工具] "

这是我目前用来将文本转换为音频然后发送到谷歌助手的工具

    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})

【问题讨论】:

    标签: python-3.x google-assistant-sdk


    【解决方案1】:

    您可以直接使用 API 发送文本查询,而不是尝试转换音频。

    如果您下载示例项目,您应该可以访问a sample for text input

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

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

    【讨论】:

    • 以上链接为示例。
    • 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) 。我如何以上述方式调用文本输入
    • 您必须直接运行 python 脚本,因为没有导出名称
    • 您可以发送text_query 而不是AudioInConfig
    猜你喜欢
    • 2021-06-19
    • 2021-10-04
    • 2021-11-06
    • 2017-06-28
    • 2018-01-02
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多