zoukankan      html  css  js  c++  java
  • 使用百度ai接口加图灵机器人完成简单web版语音对话

    app文件

    from flask import Flask, request, render_template, jsonify, send_file
    from uuid import uuid4
    import os
    import asr_test
    
    app = Flask(__name__)
    app.debug = True
    
    
    @app.route('/')
    def index():
        return render_template('index.html')
    
    
    @app.route('/uploader', methods=['POST'])
    def uploader():
        file = request.files.get('reco')
        file_name = os.path.join('audio', f'{uuid4()}.wav')
        file.save(file_name)
        ret_filename = asr_test.my_ai(file_name)
        print(ret_filename)
        return jsonify({'filename': ret_filename})
    
    
    @app.route('/get_audio/<filename>')
    def get_audio(filename):
        file = os.path.join('audio', filename)
        return send_file(file)
    
    
    if __name__ == '__main__':
        app.run('0.0.0.0', 5000)

    调用百度语音识别与语音合成接口,把传来的语言识别成文字,并调用下面的相似度接口,返回回答的文字,然后利用语音合成返回回答

    from aip import AipSpeech
    import os
    from my_npl import get_score
    from uuid import uuid4
    
    """ 你的 APPID AK SK """
    APP_ID = '******'
    API_KEY = '******'
    SECRET_KEY = '******'
    
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
    
    
    # 读取文件
    def get_file_content(filePath):
        any2pcm_str = f"ffmpeg -y  -i {filePath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm"
        os.system(any2pcm_str)
        with open(f"{filePath}.pcm", 'rb') as fp:
            return fp.read()
    
    
    # 识别本地文件
    def my_ai(file):
        res = client.asr(get_file_content(file), 'pcm', 16000, {
            'dev_pid': 1536,
        })
    
        print(res.get('result'))
        print(res)
        question = res.get('result')[0]
        req = get_score(question)
    
        req = client.synthesis(req, 'zh', 1, {
            'vol': 5,
            'pit': 5,
            'spd': 4,
            "per": 4
        })
    
        # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
        if not isinstance(req, dict):
            ret_filename = f'{uuid4()}.mp3'
            new_filename = os.path.join("audio", ret_filename)
            with open(new_filename, 'wb') as f:
                f.write(req)
            return ret_filename

    调用百度ai自然语言中的短文本相似度接口,使相似的问题得到相同的答案

    from aip import AipNlp
    from mytuling import to_tuling
    
    """ 你的 APPID AK SK """
    APP_ID = '***'
    API_KEY = '***'
    SECRET_KEY = '***'
    
    client = AipNlp(APP_ID, API_KEY, SECRET_KEY)
    
    
    def get_score(Q):
        if client.simnet(Q, '你叫什么名字').get('score') > 0.7:
            return '我是大名鼎鼎的小王吧'
        elif client.simnet(Q, '你今年几岁呀').get('score') > 0.7:
            return '我今年已经1112岁啦'
        else:
            return to_tuling(Q)

    调用图灵接口完成未设定问答的

    import requests
    
    tuling_url = 'http://openapi.tuling123.com/openapi/api/v2'
    
    data = {"reqType": 0,
            "perception": {
                "inputText": {
                    "text": ""
                }
            }
        ,
            "userInfo": {
                "apiKey": "***",
                "userId": "***"
            }
            }
    
    
    def to_tuling(Q):
        data["perception"]["inputText"]['text'] = Q
        a = requests.post(url=tuling_url, json=data)
        res = a.json()
        print(res)
        return res.get("results")[0].get("values").get("text")

    简单前端页面

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <audio src="" autoplay controls id="player"></audio>
    <p>
        <button onclick="start_reco()">开始录音</button>
    </p>
    <p>
        <button onclick="stop_reco()">停止录音</button>
    </p>
    
    </body>
    <script src="/static/Recorder.js"></script>
    <script src="/static/jQuery3.1.1.js"></script>
    <script type="text/javascript">
        var serv = "http://127.0.0.1:5000";
        var audio_serv = serv + "/get_audio/";
        var audio_context = new AudioContext();
        navigator.getUserMedia = (navigator.getUserMedia ||
            navigator.webkitGetUserMedia ||
            navigator.mozGetUserMedia ||
            navigator.msGetUserMedia);
    
        navigator.getUserMedia({audio: true}, create_stream, function (err) {
            console.log(err)
        });
    
        function create_stream(user_media) {
            var stream_input = audio_context.createMediaStreamSource(user_media);
            reco = new Recorder(stream_input);
        }
    
        function start_reco() {
            reco.record();
        }
    
        function stop_reco() {
            reco.stop();
            get_audio();
            reco.clear();
        }
    
        function get_audio() {
            reco.exportWAV(function (wav_file) {
                // wav_file 音频文件 Blob("wav","context")
                console.log(wav_file);
                var formdata = new FormData();
                formdata.append("reco", wav_file);
                $.ajax({
                        url: serv + "/uploader",
                        type: 'post',
                        processData: false,
                        contentType: false,
                        data: formdata,
                        dataType: 'json',
                        success: function (data) {
                            document.getElementById("player").src = audio_serv + data.filename;
                        }
                    }
                );
            })
        }
    </script>
    </html>
    前端页面
  • 相关阅读:
    servlet遇到的问题
    servlel出现404问题★ 出现不自动映射 设置XML的问题时候
    c3p0的错误mchange.v2.ser.Indirector
    【转】JSON和JSONP
    性能测试理论
    如何解决包冲突问题
    一方包、二方包、三方包是什么?
    利用selenium webdriver点击alert提示框
    采用Spring管理Bean和依赖注入
    Java加载jar文件并调用jar文件当中有参数和返回值的方法
  • 原文地址:https://www.cnblogs.com/luck-L/p/9891406.html
Copyright © 2011-2022 走看看