Web 录音
所需文件下载地址
链接:https://pan.baidu.com/s/1Dzbv8gPUZJ3T8Fe02hOJvg
提取码:zbgt
py文件
from flask import Flask, jsonify,send_file from flask import render_template from flask import request from uuid import uuid4 from other 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_test = my_nlp(text) filename = text2audio(new_test) ret = { "filename": filename, "content":new_test, "code": 0, } return jsonify(ret) @app.route("/get_file/<filename>") def get_file(filename): return send_file(filename) if __name__ == '__main__': app.run('0.0.0.0', 9527, debug=True)
from aip import AipNlp from aip import AipSpeech from uuid import uuid4 import requests import os APP_ID = '15842727' API_KEY = 'gBsfoHWw4pOh9n3sNhwoB853' SECRET_KEY = '4e0WXxlTo5lMgFu45lLnO490SnnpLQLN' 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 fp: return fp.read() def audio2text(filePath): ret = client.asr(get_file_content(filePath), 'pcm', 16000, { 'dev_pid': 1536, }) text = ret.get("result")[0] return text def to_tuling(text, uid): data = { "perception": { "inputText": { "text": "北京" }, }, "userInfo": { "apiKey": "934b6f5ee4c44370bd2daf71ed7b0b77", "userId": "11" } } data["perception"]["inputText"]["text"] = text data["userInfo"]["userId"] = uid res = requests.post("http://openapi.tuling123.com/openapi/api/v2", json=data) text = res.json().get('results')[0].get('values').get('text') return text def my_nlp(text): if nlp_client.simnet(text, "你叫什么名字").get('score') >= 0.58: a = '我叫人工智障' return a if nlp_client.simnet(text, "你今年几岁了").get('score') >= 0.75: a = '永远18岁' return a a = to_tuling(text, 'open123') return a def text2audio(text): result = client.synthesis(text, 'zh', 1, { 'vol': 5, 'spd': 4, 'pit': 7, 'per': 4, }) filename = f"{uuid4()}.mp3" # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 if not isinstance(result, dict): with open(filename, 'wb') as f: f.write(result) return filename