zoukankan      html  css  js  c++  java
  • 简单人工智能---基于百度接口和图灵机器人

    基础准备工作

    百度api接口准备:

    接下来就是创建应用

     选择需要的,然后生成api接口

    图灵机器人准备:

    创建一个机器人

    进行机器人的设置

     写后台代码

    from flask import  Flask,render_template,jsonify,request,send_file
    from uuid   import uuid4
    from others import audio2text ,text2audio,my_nlp
    app = Flask(__name__)
    
    @app.route("/")
    def index():
        return render_template("WebToy.html")
    
    
    @app.route("/upload",methods=["post"])
    def upload():
        fi = request.files.get("reco")
        fi_name = f"{uuid4()}.wav"
        fi.save(fi_name)
    
        text = audio2text(fi_name)
        new_text = my_nlp(text)
        filename = text2audio(new_text)
    
        ret = {
            "filename":filename,
            "content":new_text,
            "code":0
        }
        return jsonify(ret)
    
    @app.route("/get_file/<name>")
    def get_file(name):
        return send_file(name)
    
    
    
    
    
    
    
    
    if __name__ == '__main__':
    
    
        app.run("0.0.0.0",9527)
    
    import os
    
    import requests
    from aip import AipSpeech
    from aip import AipNlp
    from uuid import uuid4
    
    """ 你的 APPID AK SK """
    APP_ID = '15842542'
    API_KEY = 'AMphU5SfVThjqH6Ii7BFnacm'
    SECRET_KEY = 'aHiCNZ3psw1cOiW62p0nQMvaG4DhLrjS'
    
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
    nlp_client = AipNlp(APP_ID, API_KEY, SECRET_KEY)
    
    
    # 读取文件
    def get_file_content(filepath):
        os.system(f"ffmpeg -y -i {filepath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filepath}.pcm")
        with open(f"{filepath}.pcm", "rb") as f:
            return f.read()
    
    
    # 识别本地文件
    
    def audio2text(filepath):
        res = client.asr(get_file_content(filepath), 'pcm', 16000, {
            "dev_pid": 1536,
        })
        text = res.get("result")[0]
        return text
    
    
    
    
    # 智能问答
    def to_ling(text, uid):
        data = {
            "perception": {
                "inputText": {
                    "text": "附近的酒店"
                }
            },
            "userInfo": {
                "apiKey": "6d84dea150a34a3db748a9728fa93fec",
                "userId": "1"
            }
        }
        data["perception"]["inputText"]["text"] = text
        data["userInfo"]["userId"] = uid
        res = requests.post("http://openapi.tuling123.com/openapi/api/v2",json=data)
        res_json = res.json()
        text = res_json.get("results")[0].get('values').get("text")
        return text
    
    
    def my_nlp(text):
        if nlp_client.simnet(text, "你叫什么名字").get("score") >= 0.75:
            A = "我叫车干儿"
            return A
        if nlp_client.simnet(text, "你吃饭了没有").get("score") >= 0.75:
            A = "现在才几点,我还没有吃饭了"
            return A
        A = to_ling(text,2)
        return A
    
    
    
    # 语音合成
    
    def text2audio(text):
        result = client.synthesis(text, "zh", 1, {
            "vol": 6,
            "per": 4,
            "spd": 4,
            "pit": 7,
        })
        filename = f"{uuid4()}.mp3"
        if not isinstance(result, dict):
            with open(filename, "wb") as f1:
                f1.write(result)
        return filename
    
    #前端代码
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>我是玩具</title>
    </head>
    <body>
    
    <p>
        <audio id="player" controls autoplay></audio>
    </p>
    <button onclick="start_reco()">录音</button>
    <button onclick="stop_reco()">发送录音</button>
    <div id="mes">
    </div>
    </body>
    <script type="text/javascript" src="/static/Recorder.js"></script>
    <script type="text/javascript" src="/static/jquery-3.3.1.min.js"></script>
    <script type="text/javascript">
        var serv = "http://192.168.11.54:9527";
    
        var reco = null;
        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();
            reco.exportWAV(function (wav_file) {
                console.log(wav_file);
                var formdata = new FormData(); // form 表单 {key:value}
                formdata.append("reco", wav_file); // form input type="file"
                $.ajax({
                    url: serv + "/upload",
                    type: 'post',
                    processData: false,
                    contentType: false,
                    data: formdata,
                    dataType: 'json',
                    success: function (data) {
                        if(data.code == 0) {
                            document.getElementById("player").src="http://192.168.11.54:9527/get_file/"+ data.filename;
                            document.getElementById("mes").innerText=data.content;
                        }
                    }
                })
            });
    
            reco.clear();
        }
    
    
    </script>
    </html>
    

     注意事项:

     录音的时候,文件需要进行一个转码,需要安装ffmpeg

      安装:pip install ffmpeg

      添加环境变量:

        

        

        

        添加环境变量即可!!!

  • 相关阅读:
    jquery swipper插件的一些弊端
    linux ffmpeg 源码安装教程
    二叉树遍历(宽度优先)入门
    node安装教程
    Check the string CodeForces
    Minimize the error CodeForces
    sourcestress 问题解决方案
    C++ substr
    Java 读取Excel文件
    Stall Reservations
  • 原文地址:https://www.cnblogs.com/xinjie123/p/10604013.html
Copyright © 2011-2022 走看看