Add CardioAI project with three modules
- Module 1: Dashboard for cardiovascular disease data visualization - Module 2: Machine learning predictor with Flask API - Module 3: Voice assistant with DeepSeek and CosyVoice integration - Add .gitignore for proper file exclusion - Update requirements and documentation Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
886
aicodes/module3_voice_assistant/templates/voice_index.html
Normal file
886
aicodes/module3_voice_assistant/templates/voice_index.html
Normal file
@@ -0,0 +1,886 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>CardioAI - 心血管健康语音助手</title>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
||||
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
|
||||
<style>
|
||||
:root {
|
||||
--primary-color: #2c3e50;
|
||||
--secondary-color: #3498db;
|
||||
--accent-color: #9b59b6;
|
||||
--success-color: #27ae60;
|
||||
--warning-color: #f39c12;
|
||||
--danger-color: #e74c3c;
|
||||
--light-color: #ecf0f1;
|
||||
--dark-color: #2c3e50;
|
||||
--border-radius: 12px;
|
||||
--box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
|
||||
--transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: 'Roboto', sans-serif;
|
||||
line-height: 1.6;
|
||||
color: #333;
|
||||
background: linear-gradient(135deg, #f5f7fa 0%, #e4e8f0 100%);
|
||||
min-height: 100vh;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.header {
|
||||
text-align: center;
|
||||
margin-bottom: 40px;
|
||||
padding: 30px;
|
||||
background: white;
|
||||
border-radius: var(--border-radius);
|
||||
box-shadow: var(--box-shadow);
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.header::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
height: 5px;
|
||||
background: linear-gradient(90deg, var(--primary-color), var(--accent-color), var(--secondary-color));
|
||||
}
|
||||
|
||||
.logo {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.logo i {
|
||||
font-size: 48px;
|
||||
color: var(--danger-color);
|
||||
margin-right: 15px;
|
||||
}
|
||||
|
||||
.logo h1 {
|
||||
font-family: 'Poppins', sans-serif;
|
||||
font-size: 36px;
|
||||
color: var(--primary-color);
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
font-size: 18px;
|
||||
color: #666;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.description {
|
||||
max-width: 800px;
|
||||
margin: 20px auto 0;
|
||||
padding: 15px;
|
||||
background-color: #f8f9fa;
|
||||
border-radius: 8px;
|
||||
font-size: 15px;
|
||||
color: #555;
|
||||
line-height: 1.8;
|
||||
}
|
||||
|
||||
.content {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: 30px;
|
||||
margin-bottom: 40px;
|
||||
}
|
||||
|
||||
@media (max-width: 992px) {
|
||||
.content {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
.card {
|
||||
background: white;
|
||||
border-radius: var(--border-radius);
|
||||
box-shadow: var(--box-shadow);
|
||||
padding: 30px;
|
||||
transition: var(--transition);
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.card::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
height: 4px;
|
||||
background: linear-gradient(90deg, var(--secondary-color), var(--accent-color));
|
||||
}
|
||||
|
||||
.card:hover {
|
||||
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
|
||||
transform: translateY(-5px);
|
||||
}
|
||||
|
||||
.card h2 {
|
||||
font-family: 'Poppins', sans-serif;
|
||||
font-size: 24px;
|
||||
color: var(--primary-color);
|
||||
margin-bottom: 20px;
|
||||
padding-bottom: 15px;
|
||||
border-bottom: 2px solid var(--light-color);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.card h2 i {
|
||||
margin-right: 10px;
|
||||
color: var(--secondary-color);
|
||||
}
|
||||
|
||||
.form-group {
|
||||
margin-bottom: 25px;
|
||||
}
|
||||
|
||||
.form-group label {
|
||||
display: block;
|
||||
margin-bottom: 8px;
|
||||
font-weight: 500;
|
||||
color: var(--dark-color);
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
.form-group textarea {
|
||||
width: 100%;
|
||||
min-height: 120px;
|
||||
padding: 15px;
|
||||
border: 2px solid #ddd;
|
||||
border-radius: 8px;
|
||||
font-size: 16px;
|
||||
font-family: 'Roboto', sans-serif;
|
||||
resize: vertical;
|
||||
transition: var(--transition);
|
||||
background-color: #f8f9fa;
|
||||
}
|
||||
|
||||
.form-group textarea:focus {
|
||||
border-color: var(--secondary-color);
|
||||
outline: none;
|
||||
background-color: white;
|
||||
box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
|
||||
}
|
||||
|
||||
.examples {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
|
||||
gap: 10px;
|
||||
margin-top: 15px;
|
||||
}
|
||||
|
||||
.example-btn {
|
||||
padding: 10px 15px;
|
||||
background-color: #f8f9fa;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 6px;
|
||||
font-size: 14px;
|
||||
cursor: pointer;
|
||||
transition: var(--transition);
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.example-btn:hover {
|
||||
background-color: var(--secondary-color);
|
||||
color: white;
|
||||
border-color: var(--secondary-color);
|
||||
}
|
||||
|
||||
.btn {
|
||||
display: inline-block;
|
||||
padding: 14px 30px;
|
||||
background: var(--secondary-color);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: var(--transition);
|
||||
width: 100%;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
background: #2980b9;
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 5px 15px rgba(41, 128, 185, 0.3);
|
||||
}
|
||||
|
||||
.btn:active {
|
||||
transform: translateY(0);
|
||||
}
|
||||
|
||||
.btn:disabled {
|
||||
background-color: #95a5a6;
|
||||
cursor: not-allowed;
|
||||
transform: none;
|
||||
box-shadow: none;
|
||||
}
|
||||
|
||||
.btn i {
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
.result-container {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.answer-text {
|
||||
background-color: #f8f9fa;
|
||||
padding: 20px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 25px;
|
||||
line-height: 1.8;
|
||||
font-size: 16px;
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.answer-text h3 {
|
||||
color: var(--primary-color);
|
||||
margin-bottom: 10px;
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
.audio-controls {
|
||||
background-color: #f0f8ff;
|
||||
padding: 20px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 25px;
|
||||
border-left: 4px solid var(--secondary-color);
|
||||
}
|
||||
|
||||
.audio-controls h3 {
|
||||
color: var(--primary-color);
|
||||
margin-bottom: 15px;
|
||||
font-size: 18px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.audio-controls h3 i {
|
||||
margin-right: 10px;
|
||||
color: var(--accent-color);
|
||||
}
|
||||
|
||||
.audio-player {
|
||||
width: 100%;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.audio-player audio {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.audio-info {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
margin-top: 10px;
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.loading {
|
||||
display: none;
|
||||
text-align: center;
|
||||
padding: 40px;
|
||||
}
|
||||
|
||||
.loading-spinner {
|
||||
width: 60px;
|
||||
height: 60px;
|
||||
border: 5px solid #f3f3f3;
|
||||
border-top: 5px solid var(--secondary-color);
|
||||
border-radius: 50%;
|
||||
animation: spin 1s linear infinite;
|
||||
margin: 0 auto 20px;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
0% { transform: rotate(0deg); }
|
||||
100% { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
.loading p {
|
||||
font-size: 16px;
|
||||
color: #666;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.error {
|
||||
display: none;
|
||||
background-color: rgba(231, 76, 60, 0.1);
|
||||
border: 1px solid var(--danger-color);
|
||||
color: var(--danger-color);
|
||||
padding: 15px;
|
||||
border-radius: 8px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.status-bar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 15px;
|
||||
background-color: #f8f9fa;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 25px;
|
||||
}
|
||||
|
||||
.status-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.status-indicator {
|
||||
width: 12px;
|
||||
height: 12px;
|
||||
border-radius: 50%;
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
.status-connected {
|
||||
background-color: var(--success-color);
|
||||
animation: pulse 2s infinite;
|
||||
}
|
||||
|
||||
.status-disconnected {
|
||||
background-color: var(--danger-color);
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0% { opacity: 1; }
|
||||
50% { opacity: 0.5; }
|
||||
100% { opacity: 1; }
|
||||
}
|
||||
|
||||
.status-text {
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.footer {
|
||||
text-align: center;
|
||||
padding: 20px;
|
||||
color: #666;
|
||||
font-size: 14px;
|
||||
margin-top: 40px;
|
||||
border-top: 1px solid #eee;
|
||||
}
|
||||
|
||||
.system-info {
|
||||
background-color: #f8f9fa;
|
||||
padding: 15px;
|
||||
border-radius: 8px;
|
||||
margin-top: 20px;
|
||||
font-size: 13px;
|
||||
color: #777;
|
||||
}
|
||||
|
||||
.system-info p {
|
||||
margin: 5px 0;
|
||||
}
|
||||
|
||||
.feature-list {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
|
||||
gap: 15px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.feature-item {
|
||||
background-color: #f8f9fa;
|
||||
padding: 15px;
|
||||
border-radius: 8px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.feature-item i {
|
||||
color: var(--secondary-color);
|
||||
margin-right: 10px;
|
||||
font-size: 18px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<header class="header">
|
||||
<div class="logo">
|
||||
<i class="fas fa-microphone-alt"></i>
|
||||
<h1>CardioAI 语音助手</h1>
|
||||
</div>
|
||||
<p class="subtitle">心血管健康智能问答与语音交互系统</p>
|
||||
<div class="description">
|
||||
<p>这是一个基于DeepSeek大模型和CosyVoice语音合成技术的心血管健康语音助手。您可以提问任何关于心血管健康的问题,系统将提供专业的文字回答并转换为语音播放。</p>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="status-bar" id="statusBar">
|
||||
<div class="status-item">
|
||||
<div class="status-indicator status-connected" id="llmStatus"></div>
|
||||
<span class="status-text" id="llmStatusText">DeepSeek LLM: 连接中...</span>
|
||||
</div>
|
||||
<div class="status-item">
|
||||
<div class="status-indicator status-connected" id="ttsStatus"></div>
|
||||
<span class="status-text" id="ttsStatusText">CosyVoice TTS: 连接中...</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<div class="card">
|
||||
<h2><i class="fas fa-question-circle"></i> 提问心血管健康问题</h2>
|
||||
|
||||
<form id="questionForm">
|
||||
<div class="form-group">
|
||||
<label for="question"><i class="fas fa-comment-medical"></i> 请输入您的问题:</label>
|
||||
<textarea id="question" name="question" placeholder="例如:如何预防高血压?心脏病早期有哪些症状?哪些食物对心脏健康有益?..." required></textarea>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label><i class="fas fa-lightbulb"></i> 示例问题:</label>
|
||||
<div class="examples">
|
||||
<button type="button" class="example-btn" onclick="setExample(0)">如何预防高血压?</button>
|
||||
<button type="button" class="example-btn" onclick="setExample(1)">心脏病早期有哪些症状?</button>
|
||||
<button type="button" class="example-btn" onclick="setExample(2)">哪些食物对心脏健康有益?</button>
|
||||
<button type="button" class="example-btn" onclick="setExample(3)">如何控制胆固醇水平?</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn" id="submitBtn">
|
||||
<i class="fas fa-brain"></i> 获取专业回答
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div class="loading" id="loading">
|
||||
<div class="loading-spinner"></div>
|
||||
<p>正在生成专业回答并合成语音,请稍候...</p>
|
||||
</div>
|
||||
|
||||
<div class="error" id="errorMessage"></div>
|
||||
|
||||
<div class="system-info">
|
||||
<p><i class="fas fa-info-circle"></i> 系统提示:回答由AI生成,仅供参考。如有医疗问题,请咨询专业医生。</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card result-container" id="resultContainer">
|
||||
<h2><i class="fas fa-comment-medical"></i> 回答结果</h2>
|
||||
|
||||
<div class="answer-text" id="answerText">
|
||||
<!-- 文字回答将显示在这里 -->
|
||||
</div>
|
||||
|
||||
<div class="audio-controls">
|
||||
<h3><i class="fas fa-volume-up"></i> 语音播报</h3>
|
||||
<div class="audio-player">
|
||||
<audio controls id="audioPlayer" autoplay>
|
||||
<source id="audioSource" type="audio/mp3">
|
||||
您的浏览器不支持音频播放。
|
||||
</audio>
|
||||
</div>
|
||||
<div class="audio-info">
|
||||
<span id="audioStatus">准备播放...</span>
|
||||
<span id="audioTime">00:00</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="feature-list">
|
||||
<div class="feature-item">
|
||||
<i class="fas fa-user-md"></i>
|
||||
<span>专业心血管健康顾问</span>
|
||||
</div>
|
||||
<div class="feature-item">
|
||||
<i class="fas fa-robot"></i>
|
||||
<span>基于DeepSeek大模型</span>
|
||||
</div>
|
||||
<div class="feature-item">
|
||||
<i class="fas fa-microphone-alt"></i>
|
||||
<span>CosyVoice语音合成</span>
|
||||
</div>
|
||||
<div class="feature-item">
|
||||
<i class="fas fa-headphones-alt"></i>
|
||||
<span>实时音频播放</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button class="btn" id="newQuestionBtn" style="margin-top: 30px; background-color: var(--primary-color);">
|
||||
<i class="fas fa-redo"></i> 提出新问题
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="footer">
|
||||
<p><i class="fas fa-shield-alt"></i> CardioAI - 心血管疾病智能辅助系统</p>
|
||||
<p>本工具提供基于AI的心血管健康咨询服务,仅供参考,不能替代专业医疗建议。</p>
|
||||
<p>© 2026 CardioAI. 所有权利保留。</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// DOM元素
|
||||
const questionForm = document.getElementById('questionForm');
|
||||
const questionInput = document.getElementById('question');
|
||||
const submitBtn = document.getElementById('submitBtn');
|
||||
const loadingElement = document.getElementById('loading');
|
||||
const errorElement = document.getElementById('errorMessage');
|
||||
const resultContainer = document.getElementById('resultContainer');
|
||||
const answerText = document.getElementById('answerText');
|
||||
const audioPlayer = document.getElementById('audioPlayer');
|
||||
const audioSource = document.getElementById('audioSource');
|
||||
const audioStatus = document.getElementById('audioStatus');
|
||||
const audioTime = document.getElementById('audioTime');
|
||||
const newQuestionBtn = document.getElementById('newQuestionBtn');
|
||||
const llmStatus = document.getElementById('llmStatus');
|
||||
const llmStatusText = document.getElementById('llmStatusText');
|
||||
const ttsStatus = document.getElementById('ttsStatus');
|
||||
const ttsStatusText = document.getElementById('ttsStatusText');
|
||||
|
||||
// 示例问题
|
||||
const exampleQuestions = [
|
||||
"如何预防高血压?请提供具体的饮食和生活方式建议。",
|
||||
"心脏病早期有哪些症状?哪些人群需要特别注意?",
|
||||
"哪些食物对心脏健康有益?请推荐一些心脏友好的食谱。",
|
||||
"如何控制胆固醇水平?除了药物,还有哪些自然方法?",
|
||||
"心血管疾病患者适合进行哪些运动?运动时需要注意什么?",
|
||||
"什么是冠心病?它的主要危险因素有哪些?",
|
||||
"如何区分心绞痛和心肌梗死?出现相关症状应该怎么办?",
|
||||
"糖尿病患者如何预防心血管并发症?",
|
||||
"血压多少算正常?不同年龄段的血压标准有差异吗?",
|
||||
"长期压力对心血管健康有什么影响?如何有效管理压力?"
|
||||
];
|
||||
|
||||
// 设置示例问题
|
||||
function setExample(index) {
|
||||
if (index >= 0 && index < exampleQuestions.length) {
|
||||
questionInput.value = exampleQuestions[index];
|
||||
questionInput.focus();
|
||||
}
|
||||
}
|
||||
|
||||
// 检查系统状态
|
||||
async function checkSystemStatus() {
|
||||
try {
|
||||
const response = await fetch('/api/health');
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
|
||||
// 更新LLM状态
|
||||
if (data.llm_initialized) {
|
||||
llmStatus.className = 'status-indicator status-connected';
|
||||
llmStatusText.textContent = 'DeepSeek LLM: 已连接';
|
||||
} else {
|
||||
llmStatus.className = 'status-indicator status-disconnected';
|
||||
if (data.missing_config && data.missing_config.deepseek) {
|
||||
llmStatusText.textContent = 'DeepSeek LLM: 需要API密钥';
|
||||
llmStatusText.title = '请配置.env文件中的DEEPSEEK_API_KEY1';
|
||||
} else {
|
||||
llmStatusText.textContent = 'DeepSeek LLM: 未连接';
|
||||
}
|
||||
}
|
||||
|
||||
// 更新TTS状态
|
||||
if (data.dashscope_initialized) {
|
||||
ttsStatus.className = 'status-indicator status-connected';
|
||||
ttsStatusText.textContent = 'CosyVoice TTS: 已连接';
|
||||
} else {
|
||||
ttsStatus.className = 'status-indicator status-disconnected';
|
||||
if (data.missing_config && data.missing_config.dashscope) {
|
||||
ttsStatusText.textContent = 'CosyVoice TTS: 需要API密钥';
|
||||
ttsStatusText.title = '请配置.env文件中的DASHSCOPE_API_KEY';
|
||||
} else {
|
||||
ttsStatusText.textContent = 'CosyVoice TTS: 未连接';
|
||||
}
|
||||
}
|
||||
|
||||
// 显示配置警告(如果需要)
|
||||
if (data.setup_required) {
|
||||
showConfigWarning(data.setup_instructions);
|
||||
} else {
|
||||
hideConfigWarning();
|
||||
}
|
||||
|
||||
return data;
|
||||
} else {
|
||||
throw new Error('健康检查失败');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('检查系统状态时出错:', error);
|
||||
llmStatus.className = 'status-indicator status-disconnected';
|
||||
llmStatusText.textContent = 'DeepSeek LLM: 检查失败';
|
||||
ttsStatus.className = 'status-indicator status-disconnected';
|
||||
ttsStatusText.textContent = 'CosyVoice TTS: 检查失败';
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// 显示加载状态
|
||||
function showLoading() {
|
||||
loadingElement.style.display = 'block';
|
||||
submitBtn.disabled = true;
|
||||
submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 处理中...';
|
||||
errorElement.style.display = 'none';
|
||||
}
|
||||
|
||||
// 隐藏加载状态
|
||||
function hideLoading() {
|
||||
loadingElement.style.display = 'none';
|
||||
submitBtn.disabled = false;
|
||||
submitBtn.innerHTML = '<i class="fas fa-brain"></i> 获取专业回答';
|
||||
}
|
||||
|
||||
// 显示错误信息
|
||||
function showError(message) {
|
||||
errorElement.textContent = message;
|
||||
errorElement.style.display = 'block';
|
||||
hideLoading();
|
||||
}
|
||||
|
||||
// 显示配置警告
|
||||
function showConfigWarning(message) {
|
||||
// 创建或显示配置警告元素
|
||||
let configWarning = document.getElementById('configWarning');
|
||||
if (!configWarning) {
|
||||
configWarning = document.createElement('div');
|
||||
configWarning.id = 'configWarning';
|
||||
configWarning.className = 'config-warning';
|
||||
configWarning.style.cssText = `
|
||||
background-color: #fff3cd;
|
||||
border: 1px solid #ffeaa7;
|
||||
color: #856404;
|
||||
padding: 15px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 20px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
`;
|
||||
|
||||
const warningContent = document.createElement('div');
|
||||
warningContent.style.flex = '1';
|
||||
warningContent.innerHTML = `
|
||||
<strong><i class="fas fa-exclamation-triangle"></i> 配置提示</strong>
|
||||
<p style="margin: 8px 0 0 0; font-size: 14px;">${message}</p>
|
||||
<p style="margin: 5px 0 0 0; font-size: 13px;">
|
||||
请复制 <code>.env.example</code> 为 <code>.env</code> 并填入您的API密钥。
|
||||
</p>
|
||||
`;
|
||||
|
||||
const closeBtn = document.createElement('button');
|
||||
closeBtn.innerHTML = '<i class="fas fa-times"></i>';
|
||||
closeBtn.style.cssText = `
|
||||
background: none;
|
||||
border: none;
|
||||
color: #856404;
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
padding: 5px 10px;
|
||||
`;
|
||||
closeBtn.onclick = () => configWarning.style.display = 'none';
|
||||
|
||||
configWarning.appendChild(warningContent);
|
||||
configWarning.appendChild(closeBtn);
|
||||
|
||||
// 插入到状态栏之后
|
||||
const statusBar = document.getElementById('statusBar');
|
||||
statusBar.parentNode.insertBefore(configWarning, statusBar.nextSibling);
|
||||
} else {
|
||||
configWarning.style.display = 'block';
|
||||
const warningContent = configWarning.querySelector('div');
|
||||
warningContent.innerHTML = `
|
||||
<strong><i class="fas fa-exclamation-triangle"></i> 配置提示</strong>
|
||||
<p style="margin: 8px 0 0 0; font-size: 14px;">${message}</p>
|
||||
<p style="margin: 5px 0 0 0; font-size: 13px;">
|
||||
请复制 <code>.env.example</code> 为 <code>.env</code> 并填入您的API密钥。
|
||||
</p>
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
function hideConfigWarning() {
|
||||
const configWarning = document.getElementById('configWarning');
|
||||
if (configWarning) {
|
||||
configWarning.style.display = 'none';
|
||||
}
|
||||
}
|
||||
|
||||
// 更新音频时间显示
|
||||
function updateAudioTime() {
|
||||
const currentTime = audioPlayer.currentTime;
|
||||
const duration = audioPlayer.duration;
|
||||
|
||||
if (!isNaN(duration)) {
|
||||
const currentMinutes = Math.floor(currentTime / 60);
|
||||
const currentSeconds = Math.floor(currentTime % 60);
|
||||
const durationMinutes = Math.floor(duration / 60);
|
||||
const durationSeconds = Math.floor(duration % 60);
|
||||
|
||||
audioTime.textContent =
|
||||
`${currentMinutes.toString().padStart(2, '0')}:${currentSeconds.toString().padStart(2, '0')} / ${durationMinutes.toString().padStart(2, '0')}:${durationSeconds.toString().padStart(2, '0')}`;
|
||||
}
|
||||
}
|
||||
|
||||
// 显示结果
|
||||
function displayResults(data) {
|
||||
// 显示文字回答
|
||||
answerText.innerHTML = `
|
||||
<h3><i class="fas fa-comment-medical"></i> 专业回答:</h3>
|
||||
<div style="white-space: pre-wrap; line-height: 1.8;">${data.text_answer}</div>
|
||||
`;
|
||||
|
||||
// 如果有音频数据,设置音频播放器
|
||||
if (data.audio_base64) {
|
||||
// 创建base64音频URL
|
||||
const audioUrl = `data:audio/mp3;base64,${data.audio_base64}`;
|
||||
audioSource.src = audioUrl;
|
||||
audioPlayer.load();
|
||||
|
||||
// 设置音频事件监听器
|
||||
audioPlayer.oncanplay = function() {
|
||||
audioStatus.textContent = '准备播放...';
|
||||
};
|
||||
|
||||
audioPlayer.onplay = function() {
|
||||
audioStatus.textContent = '正在播放...';
|
||||
};
|
||||
|
||||
audioPlayer.onpause = function() {
|
||||
audioStatus.textContent = '已暂停';
|
||||
};
|
||||
|
||||
audioPlayer.onended = function() {
|
||||
audioStatus.textContent = '播放完成';
|
||||
};
|
||||
|
||||
audioPlayer.ontimeupdate = updateAudioTime;
|
||||
|
||||
// 尝试自动播放
|
||||
audioPlayer.play().catch(function(error) {
|
||||
console.log('自动播放被阻止:', error);
|
||||
audioStatus.textContent = '点击播放按钮开始播放';
|
||||
});
|
||||
} else {
|
||||
audioStatus.textContent = '语音合成失败,仅显示文字回答';
|
||||
}
|
||||
|
||||
// 显示结果容器
|
||||
resultContainer.style.display = 'block';
|
||||
|
||||
// 滚动到结果
|
||||
resultContainer.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
||||
}
|
||||
|
||||
// 表单提交处理
|
||||
questionForm.addEventListener('submit', async function(event) {
|
||||
event.preventDefault();
|
||||
|
||||
// 验证表单
|
||||
if (!questionForm.checkValidity()) {
|
||||
questionForm.reportValidity();
|
||||
return;
|
||||
}
|
||||
|
||||
const question = questionInput.value.trim();
|
||||
|
||||
if (!question) {
|
||||
showError('请输入问题内容');
|
||||
return;
|
||||
}
|
||||
|
||||
showLoading();
|
||||
|
||||
try {
|
||||
// 发送问题到后端
|
||||
const response = await fetch('/api/ask', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ question: question })
|
||||
});
|
||||
|
||||
// 检查响应状态
|
||||
if (!response.ok) {
|
||||
// 尝试获取错误信息
|
||||
const contentType = response.headers.get('content-type');
|
||||
if (contentType && contentType.includes('application/json')) {
|
||||
const errorData = await response.json();
|
||||
throw new Error(errorData.message || `服务器错误 (${response.status})`);
|
||||
} else {
|
||||
const errorText = await response.text();
|
||||
// 如果返回的是HTML,提取有用的错误信息
|
||||
if (errorText.includes('<html') || errorText.includes('<!DOCTYPE')) {
|
||||
throw new Error(`服务器返回了HTML错误页面 (${response.status}),请检查后端配置`);
|
||||
} else {
|
||||
throw new Error(`服务器错误: ${errorText.substring(0, 100)}... (${response.status})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 解析JSON响应
|
||||
const result = await response.json();
|
||||
|
||||
if (result.status === 'success') {
|
||||
displayResults(result);
|
||||
} else {
|
||||
showError(result.message || '获取回答时出错');
|
||||
}
|
||||
} catch (error) {
|
||||
showError(`请求失败: ${error.message}。请检查服务器是否运行正常或API密钥是否正确配置。`);
|
||||
console.error('详细错误:', error);
|
||||
} finally {
|
||||
hideLoading();
|
||||
}
|
||||
});
|
||||
|
||||
// 新问题按钮
|
||||
newQuestionBtn.addEventListener('click', function() {
|
||||
resultContainer.style.display = 'none';
|
||||
questionInput.value = '';
|
||||
questionInput.focus();
|
||||
|
||||
// 滚动到表单
|
||||
questionForm.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
||||
});
|
||||
|
||||
// 页面加载时初始化
|
||||
document.addEventListener('DOMContentLoaded', async function() {
|
||||
console.log('🎤 CardioAI语音助手界面已初始化');
|
||||
|
||||
// 检查系统状态
|
||||
await checkSystemStatus();
|
||||
|
||||
// 设置初始焦点
|
||||
questionInput.focus();
|
||||
|
||||
// 每30秒检查一次系统状态
|
||||
setInterval(checkSystemStatus, 30000);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
333
aicodes/module3_voice_assistant/voice_assistant_app.py
Normal file
333
aicodes/module3_voice_assistant/voice_assistant_app.py
Normal file
@@ -0,0 +1,333 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
CardioAI - 语音助手模块
|
||||
基于Deepseek和CosyVoice的心血管健康问答语音助手
|
||||
"""
|
||||
|
||||
import os
|
||||
import base64
|
||||
from flask import Flask, request, jsonify, render_template
|
||||
from langchain_openai import ChatOpenAI
|
||||
from dotenv import load_dotenv
|
||||
import dashscope
|
||||
from dashscope.audio.tts_v2 import SpeechSynthesizer, AudioFormat, ResultCallback
|
||||
import json
|
||||
import traceback
|
||||
|
||||
# 初始化Flask应用
|
||||
app = Flask(__name__, template_folder='templates')
|
||||
|
||||
# 环境变量路径 - 从ENV_PATH环境变量读取,默认为项目根目录下的.env文件
|
||||
ENV_PATH = os.getenv('ENV_PATH', '/Users/anthony/PycharmProjects/ sad_test01/.env')
|
||||
|
||||
def load_environment_variables():
|
||||
"""加载环境变量"""
|
||||
try:
|
||||
if os.path.exists(ENV_PATH):
|
||||
print(f"📋 从 {ENV_PATH} 加载环境变量")
|
||||
load_dotenv(dotenv_path=ENV_PATH)
|
||||
else:
|
||||
print(f"⚠️ 环境变量文件不存在: {ENV_PATH},尝试从默认位置加载")
|
||||
load_dotenv() # 尝试从默认位置加载
|
||||
|
||||
# 检查必要的环境变量
|
||||
required_vars = ['DEEPSEEK_API_KEY1', 'DASHSCOPE_API_KEY']
|
||||
missing_vars = [var for var in required_vars if not os.getenv(var)]
|
||||
|
||||
if missing_vars:
|
||||
print(f"❌ 缺少必要的环境变量: {missing_vars}")
|
||||
print("⚠️ 请在环境变量文件中设置以下变量:")
|
||||
print(" - DEEPSEEK_API_KEY1: DeepSeek API密钥")
|
||||
print(" - DASHSCOPE_API_KEY: DashScope (阿里云) API密钥")
|
||||
print(" - base_url1: DeepSeek API基础URL (可选,默认: https://api.deepseek.com/v1)")
|
||||
return False
|
||||
else:
|
||||
print("✅ 环境变量加载成功")
|
||||
print(f" DeepSeek API密钥: {'已设置' if os.getenv('DEEPSEEK_API_KEY1') else '未设置'}")
|
||||
print(f" DashScope API密钥: {'已设置' if os.getenv('DASHSCOPE_API_KEY') else '未设置'}")
|
||||
print(f" DeepSeek基础URL: {os.getenv('base_url1', '默认: https://api.deepseek.com/v1')}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 加载环境变量时出错: {e}")
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
def initialize_llm():
|
||||
"""初始化DeepSeek LLM"""
|
||||
try:
|
||||
# 设置DeepSeek API配置 (使用与llm_streaming.py一致的变量名)
|
||||
deepseek_api_key = os.getenv('DEEPSEEK_API_KEY1')
|
||||
deepseek_base_url = os.getenv('base_url1', 'https://api.deepseek.com/v1')
|
||||
|
||||
if not deepseek_api_key:
|
||||
raise ValueError("DEEPSEEK_API_KEY1环境变量未设置")
|
||||
|
||||
# 初始化ChatOpenAI实例(兼容OpenAI接口)
|
||||
llm = ChatOpenAI(
|
||||
base_url=deepseek_base_url,
|
||||
api_key=deepseek_api_key,
|
||||
model="deepseek-chat",
|
||||
temperature=0.7,
|
||||
max_tokens=1000
|
||||
)
|
||||
|
||||
print("✅ DeepSeek LLM初始化成功")
|
||||
return llm
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 初始化DeepSeek LLM时出错: {e}")
|
||||
traceback.print_exc()
|
||||
return None
|
||||
|
||||
def initialize_tts():
|
||||
"""初始化语音合成"""
|
||||
try:
|
||||
# 设置DashScope API密钥
|
||||
dashscope_api_key = os.getenv('DASHSCOPE_API_KEY')
|
||||
|
||||
if not dashscope_api_key:
|
||||
raise ValueError("DASHSCOPE_API_KEY环境变量未设置")
|
||||
|
||||
dashscope.api_key = dashscope_api_key
|
||||
print("✅ CosyVoice语音合成初始化成功")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 初始化语音合成时出错: {e}")
|
||||
traceback.print_exc()
|
||||
|
||||
def get_config_status():
|
||||
"""获取配置状态"""
|
||||
config_status = {
|
||||
'deepseek': {
|
||||
'api_key_set': bool(os.getenv('DEEPSEEK_API_KEY1')),
|
||||
'base_url_set': bool(os.getenv('base_url1')),
|
||||
'status': 'configured' if os.getenv('DEEPSEEK_API_KEY1') else 'missing_api_key'
|
||||
},
|
||||
'dashscope': {
|
||||
'api_key_set': bool(os.getenv('DASHSCOPE_API_KEY')),
|
||||
'status': 'configured' if os.getenv('DASHSCOPE_API_KEY') else 'missing_api_key'
|
||||
},
|
||||
'env_file_exists': os.path.exists(ENV_PATH)
|
||||
}
|
||||
return config_status
|
||||
|
||||
def get_system_prompt():
|
||||
"""获取系统提示词"""
|
||||
return """你是一名专业的心血管健康顾问,拥有丰富的医学知识和临床经验。你的任务是:
|
||||
|
||||
1. **专业准确**:基于最新的医学研究和临床指南提供准确信息
|
||||
2. **通俗易懂**:用通俗易懂的语言解释医学术语和概念
|
||||
3. **个性化建议**:根据用户的具体情况提供个性化建议
|
||||
4. **预防为主**:强调心血管疾病的预防和早期干预
|
||||
5. **安全提醒**:明确指出哪些情况需要立即就医
|
||||
|
||||
请保持回答的专业性、准确性和实用性,同时要富有同理心和耐心。"""
|
||||
|
||||
def synthesize_speech(text):
|
||||
"""将文本合成为语音并返回base64编码的音频"""
|
||||
try:
|
||||
if not text or len(text.strip()) == 0:
|
||||
raise ValueError("文本内容为空")
|
||||
|
||||
print(f"🔊 开始语音合成,文本长度: {len(text)} 字符")
|
||||
|
||||
# 创建语音合成器实例
|
||||
# 使用cosyvoice-v2模型,longxiaochun_v2音色,MP3格式
|
||||
synthesizer = SpeechSynthesizer(
|
||||
model="cosyvoice-v2",
|
||||
voice="longxiaochun_v2",
|
||||
format=AudioFormat.MP3_22050HZ_MONO_256KBPS,
|
||||
speech_rate=1.0,
|
||||
pitch_rate=1.0,
|
||||
volume=50
|
||||
)
|
||||
|
||||
# 同步调用语音合成
|
||||
# 注意:文本长度可能有限制,如果太长需要分段处理
|
||||
max_text_length = 2000 # CosyVoice单次调用的文本长度限制
|
||||
if len(text) > max_text_length:
|
||||
print(f"⚠️ 文本长度超过{max_text_length}字符,将进行分段处理")
|
||||
# 简单分段:按句号、问号、感叹号分段
|
||||
segments = []
|
||||
current_segment = ""
|
||||
|
||||
for char in text:
|
||||
current_segment += char
|
||||
if char in ['。', '!', '?', '.', '!', '?'] and len(current_segment) > 100:
|
||||
segments.append(current_segment)
|
||||
current_segment = ""
|
||||
|
||||
if current_segment:
|
||||
segments.append(current_segment)
|
||||
|
||||
# 合并音频数据
|
||||
audio_data = b""
|
||||
for i, segment in enumerate(segments):
|
||||
print(f" 合成第 {i+1}/{len(segments)} 段,长度: {len(segment)} 字符")
|
||||
segment_audio = synthesizer.call(segment.strip())
|
||||
audio_data += segment_audio
|
||||
else:
|
||||
# 直接合成
|
||||
audio_data = synthesizer.call(text.strip())
|
||||
|
||||
print(f"✅ 语音合成完成,音频大小: {len(audio_data)} 字节")
|
||||
|
||||
# 将音频数据编码为base64
|
||||
audio_base64 = base64.b64encode(audio_data).decode('utf-8')
|
||||
|
||||
return audio_base64
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 语音合成失败: {e}")
|
||||
traceback.print_exc()
|
||||
return None
|
||||
|
||||
# 全局变量
|
||||
llm = None
|
||||
|
||||
@app.route('/')
|
||||
def home():
|
||||
"""主页面 - 语音助手界面"""
|
||||
return render_template('voice_index.html')
|
||||
|
||||
@app.route('/api/health', methods=['GET'])
|
||||
def health_check():
|
||||
"""健康检查端点"""
|
||||
config_status = get_config_status()
|
||||
|
||||
# 检查整体健康状态
|
||||
llm_ready = llm is not None
|
||||
tts_ready = dashscope.api_key is not None
|
||||
overall_healthy = llm_ready and tts_ready
|
||||
|
||||
return jsonify({
|
||||
'status': 'healthy' if overall_healthy else 'degraded',
|
||||
'service': 'CardioAI Voice Assistant',
|
||||
'llm_initialized': llm_ready,
|
||||
'dashscope_initialized': tts_ready,
|
||||
'config_status': config_status,
|
||||
'missing_config': {
|
||||
'deepseek': not config_status['deepseek']['api_key_set'],
|
||||
'dashscope': not config_status['dashscope']['api_key_set']
|
||||
},
|
||||
'setup_required': not config_status['deepseek']['api_key_set'] or not config_status['dashscope']['api_key_set'],
|
||||
'setup_instructions': '请配置.env文件中的API密钥' if not config_status['deepseek']['api_key_set'] or not config_status['dashscope']['api_key_set'] else '配置完成'
|
||||
})
|
||||
|
||||
@app.route('/api/ask', methods=['POST'])
|
||||
def ask_question():
|
||||
"""问答端点 - 处理用户问题并返回文本和语音回答"""
|
||||
global llm
|
||||
|
||||
try:
|
||||
# 获取用户问题
|
||||
if request.is_json:
|
||||
data = request.get_json()
|
||||
question = data.get('question', '').strip()
|
||||
else:
|
||||
question = request.form.get('question', '').strip()
|
||||
|
||||
if not question:
|
||||
return jsonify({
|
||||
'status': 'error',
|
||||
'message': '请提供问题内容'
|
||||
}), 400
|
||||
|
||||
print(f"🤔 用户提问: {question[:100]}...")
|
||||
|
||||
# 确保LLM已初始化
|
||||
if llm is None:
|
||||
print("⚠️ LLM未初始化,尝试重新初始化")
|
||||
llm = initialize_llm()
|
||||
if llm is None:
|
||||
return jsonify({
|
||||
'status': 'error',
|
||||
'message': '语言模型未初始化,请检查配置'
|
||||
}), 503
|
||||
|
||||
# 构建完整的消息
|
||||
system_prompt = get_system_prompt()
|
||||
messages = [
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": question}
|
||||
]
|
||||
|
||||
# 调用DeepSeek API获取回答
|
||||
print("🧠 正在生成回答...")
|
||||
response = llm.invoke(messages)
|
||||
text_answer = response.content if hasattr(response, 'content') else str(response)
|
||||
|
||||
print(f"✅ 回答生成完成,长度: {len(text_answer)} 字符")
|
||||
|
||||
# 语音合成
|
||||
audio_base64 = synthesize_speech(text_answer)
|
||||
|
||||
if audio_base64 is None:
|
||||
print("⚠️ 语音合成失败,仅返回文本回答")
|
||||
return jsonify({
|
||||
'status': 'success',
|
||||
'text_answer': text_answer,
|
||||
'audio_base64': None,
|
||||
'message': '语音合成失败,仅返回文本回答'
|
||||
})
|
||||
|
||||
# 返回结果
|
||||
return jsonify({
|
||||
'status': 'success',
|
||||
'text_answer': text_answer,
|
||||
'audio_base64': audio_base64,
|
||||
'audio_format': 'mp3',
|
||||
'audio_sample_rate': '22050Hz'
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 处理问题时出错: {e}")
|
||||
traceback.print_exc()
|
||||
return jsonify({
|
||||
'status': 'error',
|
||||
'message': f'处理问题时出错: {str(e)}'
|
||||
}), 500
|
||||
|
||||
def init_app():
|
||||
"""初始化应用"""
|
||||
print("=" * 60)
|
||||
print("🎤 CardioAI - 心血管健康语音助手")
|
||||
print("=" * 60)
|
||||
|
||||
# 加载环境变量
|
||||
if not load_environment_variables():
|
||||
print("⚠️ 环境变量加载失败,某些功能可能无法使用")
|
||||
|
||||
# 初始化LLM
|
||||
global llm
|
||||
llm = initialize_llm()
|
||||
|
||||
# 初始化语音合成
|
||||
initialize_tts()
|
||||
|
||||
print("\n📡 API端点:")
|
||||
print(" GET / - 语音助手界面")
|
||||
print(" GET /api/health - 健康检查")
|
||||
print(" POST /api/ask - 提问并获取语音回答")
|
||||
|
||||
print(f"\n🧠 LLM状态: {'已初始化' if llm is not None else '未初始化'}")
|
||||
print(f"🔊 语音合成: {'已初始化' if dashscope.api_key else '未初始化'}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 初始化应用
|
||||
init_app()
|
||||
|
||||
# 运行Flask应用
|
||||
print(f"\n🌍 启动服务器: http://127.0.0.1:5002")
|
||||
print(" 按 Ctrl+C 停止\n")
|
||||
|
||||
app.run(
|
||||
host='0.0.0.0',
|
||||
port=5002,
|
||||
debug=True,
|
||||
threaded=True
|
||||
)
|
||||
else:
|
||||
# 用于WSGI部署
|
||||
init_app()
|
||||
Reference in New Issue
Block a user