Initial commit: CardioAI - 心血管疾病智能辅助系统
This commit is contained in:
84
module3_voice_assistant/voice_assistant_app.py
Normal file
84
module3_voice_assistant/voice_assistant_app.py
Normal file
@@ -0,0 +1,84 @@
|
||||
from flask import Flask, request, jsonify, render_template
|
||||
import os
|
||||
import base64
|
||||
from dotenv import load_dotenv
|
||||
from langchain_openai import ChatOpenAI
|
||||
from dashscope.audio.tts_v2 import SpeechSynthesizer, AudioFormat
|
||||
import dashscope
|
||||
|
||||
# 加载环境变量
|
||||
ENV_PATH = "D:\\AI_Coding\\aicodes\\.env"
|
||||
load_dotenv(ENV_PATH)
|
||||
|
||||
# 初始化DashScope API Key
|
||||
dashscope.api_key = os.getenv("DASHSCOPE_API_KEY")
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# 初始化DeepSeek LLM
|
||||
def get_llm():
|
||||
llm = ChatOpenAI(
|
||||
base_url=os.getenv("base_url1"),
|
||||
api_key=os.getenv("DEEPSEEK_API_KEY1"),
|
||||
model="deepseek-chat",
|
||||
temperature=0,
|
||||
)
|
||||
return llm
|
||||
|
||||
# 系统提示词
|
||||
system_prompt = "你是一位专业的心血管健康顾问,专注于提供科学、准确的心血管疾病预防和健康管理建议。请基于医学知识回答用户的问题,保持专业、友好的态度。"
|
||||
|
||||
@app.route('/')
|
||||
def home():
|
||||
return render_template('voice_index.html')
|
||||
|
||||
@app.route('/ask_cardio', methods=['POST'])
|
||||
def ask_cardio():
|
||||
try:
|
||||
# 接收用户问题
|
||||
data = request.json
|
||||
user_question = data.get('question', '')
|
||||
|
||||
if not user_question:
|
||||
return jsonify({'error': '请输入问题'}), 400
|
||||
|
||||
# 初始化LLM
|
||||
llm = get_llm()
|
||||
|
||||
# 构建完整的prompt
|
||||
full_prompt = f"{system_prompt}\n\n用户问题:{user_question}"
|
||||
|
||||
# 获取LLM回答
|
||||
response = llm.invoke(full_prompt)
|
||||
text_answer = response.content
|
||||
|
||||
# 使用CosyVoice合成语音
|
||||
model = "cosyvoice-v2"
|
||||
voice = "longxiaochun_v2"
|
||||
|
||||
# 初始化SpeechSynthesizer
|
||||
synthesizer = SpeechSynthesizer(
|
||||
model=model,
|
||||
voice=voice,
|
||||
format=AudioFormat.MP3_22050HZ_MONO_256KBPS
|
||||
)
|
||||
|
||||
# 同步调用合成语音
|
||||
audio_data = synthesizer.call(text_answer)
|
||||
|
||||
# 将音频数据转换为Base64编码
|
||||
audio_base64 = base64.b64encode(audio_data).decode('utf-8')
|
||||
|
||||
# 返回结果
|
||||
response_data = {
|
||||
'text_answer': text_answer,
|
||||
'audio_base64': audio_base64
|
||||
}
|
||||
|
||||
return jsonify(response_data)
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True, host='0.0.0.0', port=5001)
|
||||
Reference in New Issue
Block a user