簡單人工智能---基於百度接口和圖靈機器人


基礎准備工作

百度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

  添加環境變量:

    

    

    

    添加環境變量即可!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM