- 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>
886 lines
31 KiB
HTML
886 lines
31 KiB
HTML
<!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> |