flask ai 语音识别以及模拟人类说话

flask_语音识别以及合,模拟人类说话

调用百度语音识别以及合成在页面上返回

flask_app.py

from aip import AipSpeech
import os
""" 你的 APPID AK SK """
APP_ID = '156743074'
API_KEY = '7qgG 9Lcx8mvmj'
SECRET_KEY = 'ky q0zNDnRzG9rtn6upz0xip'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

def text2audio(answer):
    # 语音合成
    result = client.synthesis(answer, 'zh', 1, {
        'vol': 5,
        "spd": 4,
        "pit": 9,
        "per": 4
    })

    # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
    from uuid import uuid4
    res_file_name = f"{uuid4()}.mp3"
    res_file_name = os.path.join(os.path.abspath('chat'), res_file_name)
    if not isinstance(result, dict):
        with open(res_file_name, 'wb') as f:
            f.write(result)
        return res_file_name


def audio2text(filePath):
    # 开始语音识别
    # 读取文件
    filePath = os.path.join(os.path.abspath('chat'), 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 fp:
        res = client.asr(fp.read(), 'pcm', 16000, {
            'dev_pid': 1537,
        })

    return res

my_nlp.py

from aip import AipNlp
import os
""" 你的 APPID AK SK """
APP_ID = '156740374'
API_KEY = '7qgG cVss7fc9Lx8mvmj'
SECRET_KEY = 'kyvKcM1 n6upz0xip'

nlp_client = AipNlp(APP_ID, API_KEY, SECRET_KEY)

#图灵配置
import requests

dic = {
	"reqType":0,
    "perception": {
        "inputText": {
            "text": "你叫什么名字"
        }
    },
    "userInfo": {
        "apiKey": "e237357df4dd405f9b2dddd22320837a",
        "userId": "123123"
    }
}

def go_tuling(Q):
    dic["perception"]["inputText"]["text"] = Q
    res = requests.post("http://openapi.tuling123.com/openapi/api/v2",json=dic)
    res_dic = res.json()
    print(res_dic.get("results")[0]["values"]["text"])

    return res_dic.get("results")[0]["values"]["text"]



def my_nlp_func(text):
    # if "你叫什么名字" in text : # 你的名字是什么? 你丫谁呀? 您贵姓? what's your name?
    #     # NLP 自然语言处理的问题
    #     return "我的名字叫小石榴"

    if nlp_client.simnet("你的名字是什么",text).get("score") >= 0.58:
        return "我的名字叫小石榴"

    if nlp_client.simnet("你今年多大了",text).get("score") >= 0.58:
        return "我今年10岁了"


    return go_tuling(text)

baidu_aip.py

from aip import AipSpeech
import os
""" 你的 APPID AK SK """
APP_ID = '15674374'
API_KEY = '7qgGbZ Lx8mvmj'
SECRET_KEY = 'kyvKM1 tn6upz0xip'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

def text2audio(answer):
    # 语音合成
    result = client.synthesis(answer, 'zh', 1, {
        'vol': 5,
        "spd": 4,
        "pit": 9,
        "per": 4
    })

    # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
    from uuid import uuid4
    res_file_name = f"{uuid4()}.mp3"
    res_file_name = os.path.join(os.path.abspath('chat'), res_file_name)
    if not isinstance(result, dict):
        with open(res_file_name, 'wb') as f:
            f.write(result)
        return res_file_name


def audio2text(filePath):
    # 开始语音识别
    # 读取文件
    filePath = os.path.join(os.path.abspath('chat'), 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 fp:
        res = client.asr(fp.read(), 'pcm', 16000, {
            'dev_pid': 1537,
        })

    return res

前端




    
    Title










你可能感兴趣的:(flask ai 语音识别以及模拟人类说话)