wed版语音机器人:
GitHub项目地址:https://github.com/Yang915/WebToy
特别说明:该项目在本机测试,通过浏览器调用系统麦克风(https请求),实际环境在Firefox进行127.0.0.1本机回环测试,其他浏览器不确保正常运行!!!
项目目录说明:
项目主程序:main.py
1 import os 2 from flask import Flask, render_template, request 3 4 from BP_get_answer import app_get_answer # 返回应答语音的蓝图模块 5 6 app = Flask(__name__) 7 app.debug = True 8 9 app.register_blueprint(app_get_answer) # 注册蓝图 10 11 12 @app.route('/ai_uploader', methods=['GET', 'POST']) 13 def answer(): 14 # 接收前端发来的语音消息,并指定路径保存 15 reco_file = request.files.get('reco') 16 # 调用uuid三方模块生成唯一文件名 17 from uuid import uuid4 18 filename = f'{uuid4()}.wav' 19 20 filepath = os.path.join(os.path.dirname(__file__), 'audio', 'questions', filename) 21 reco_file.save(filepath) 22 # print('语音问题保存路径:',filepath) 23 24 # 调用语音识别模块,对语音信息进行格式转换保存在指定目录,然后进行识别,得到文字信息 25 from ASR import asr 26 text_question = asr(filepath) 27 # print('语音问题文本',text_question) 28 29 # 调用自然语言处理模块,对文字信息进行处理,得到回复文字信息 30 from NLP import nlp 31 text_answer = nlp(text_question) 32 # print('语音回答文本',text_answer) 33 34 # 调用语音合成模块,对回复的文字信息进行合成并保存在指定目录下 35 from TTS import tts 36 answer_filepath = tts(text_answer) 37 print('语音回答文件路径:', answer_filepath) 38 39 # 获取语音应答消息文件名并返回 40 answer_filename = os.path.basename(answer_filepath) 41 return {'filename': answer_filename} 42 43 44 # 返回展示页面 45 @app.route('/record') 46 def get_record(): 47 return render_template('WebToy.html') 48 49 50 if __name__ == '__main__': 51 app.run()