一.图灵对话
jiehe.py
from aip import AipSpeech,AipNlp import os, time, requests """ 你的 APPID AK SK """ APP_ID = '15420917' API_KEY = 'eP25XkG4vhdRQk7A6ZW3Zf2C' SECRET_KEY = 'RxwrVkWUodowcBWcyLwS7DWI8B9XE4cH ' nlp = AipNlp(APP_ID, API_KEY, SECRET_KEY) #nlp 自然语言进程,用作语言比对相似度 client = AipSpeech(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): res = client.asr(get_file_content(filePath), 'pcm', 16000, { 'dev_pid': 1536, }) print(res) return res.get("result")[0] def text2audio(text): filename=f"{time.time()}.mp3" result = client.synthesis(text, 'zh', 1, { 'vol': 5, }) if not isinstance(result,dict): with open(filename,"wb") as f: f.write(result) return filename def tul(text): args = { "reqType":0, "perception": { "inputText": { "text": text } }, "userInfo": { "apiKey": "ebadba9c8a18497e90483f0a1ef5cf29", "userId": "1111" } } url = "http://openapi.tuling123.com/openapi/api/v2" res = requests.post(url,json=args) result = res.json().get("results")[0].get("values").get("text") return result text = audio2text("bjtq.m4a") if nlp.simnet("你叫什么名字",text).get("score"): #进行比对 test="我的名字叫银角大王8" else: test=tul(text) filename=text2audio(test) os.system(filename)
二.在线对话
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <audio controls autoplay id="player"></audio> <p> <button onclick="start_reco()" style="background-color: yellow">录制语音指令</button> </p> <p> <button onclick="stop_reco_audio()" style="background-color: blue">发送语音指令</button> </p> </body> <!--<script type="application/javascript" src="/static/Recorder.js"></script>--> <script type="application/javascript" src="https://cdn.bootcss.com/recorderjs/0.1.0/recorder.js"></script> <script type="text/javascript" src="/static/jQuery3.1.1.js"></script> <script type="text/javascript"> 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_audio() { reco.stop(); send_audio(); reco.clear(); } function send_audio() { reco.exportWAV(function (wav_file) { var formdata = new FormData(); formdata.append("record", wav_file); console.log(formdata); $.ajax({ url: "http://192.168.13.42:9527/ai", type: 'post', processData: false, contentType: false, data: formdata, dataType: 'json', success: function (data) { document.getElementById("player").src ="http://192.168.13.42:9527/get_audio/" + data.filename } }); }) } </script> </html>
from flask import Flask,render_template,request,jsonify,send_file from uuid import uuid4 import jiehe app=Flask(__name__) @app.route('/') def index(): return render_template("index1.html") @app.route("/ai",methods=["POST"]) def ai(): audio = request.files.get("record") print("audio",audio) filename=f'{uuid4()}.wav' audio.save(filename) q_text =jiehe.audio2text(filename) a_text =jiehe.tul(q_text) a_file=jiehe.text2audio(a_text) return jsonify({'filename':a_file}) @app.route('/get_audio/<filename>') def get_audio(filename): return send_file(filename) if __name__ == '__main__': app.run('0.0.0.0',9527,debug=True)