简单人工智能---基于百度接口和图灵机器人

基础准备工作

百度api接口准备:

简单人工智能---基于百度接口和图灵机器人_第1张图片

接下来就是创建应用

简单人工智能---基于百度接口和图灵机器人_第2张图片

 

 

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

图灵机器人准备:

创建一个机器人

简单人工智能---基于百度接口和图灵机器人_第3张图片

进行机器人的设置

简单人工智能---基于百度接口和图灵机器人_第4张图片

 

 写后台代码

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/")
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

 

#前端代码



    
    我是玩具



 注意事项:

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

  安装:pip install ffmpeg

  添加环境变量:

    简单人工智能---基于百度接口和图灵机器人_第5张图片

    简单人工智能---基于百度接口和图灵机器人_第6张图片

    简单人工智能---基于百度接口和图灵机器人_第7张图片

    添加环境变量即可!!!

转载于:https://www.cnblogs.com/xinjie123/p/10604013.html

你可能感兴趣的:(人工智能,ffmpeg,python)