Files
voice-chat-web/static/index.html
2026-04-21 18:51:08 +08:00

760 lines
24 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>语音对话</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
max-width: 600px;
width: 100%;
background: white;
border-radius: 20px;
padding: 30px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.header {
text-align: center;
margin-bottom: 30px;
}
.header h1 {
color: #333;
font-size: 28px;
margin-bottom: 10px;
}
.status-indicator {
display: inline-flex;
align-items: center;
gap: 8px;
font-size: 14px;
color: #666;
}
.status-dot {
width: 10px;
height: 10px;
border-radius: 50%;
background: #ccc;
}
.status-dot.online {
background: #4CAF50;
animation: pulse 2s infinite;
}
.status-dot.offline {
background: #f44336;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.record-section {
text-align: center;
margin-bottom: 30px;
}
.record-btn {
width: 120px;
height: 120px;
border-radius: 50%;
border: none;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
font-size: 24px;
cursor: pointer;
transition: all 0.3s ease;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 5px;
margin: 0 auto;
}
.record-btn:hover {
transform: scale(1.05);
box-shadow: 0 10px 30px rgba(102,126,234,0.4);
}
.record-btn.recording {
background: linear-gradient(135deg, #f44336 0%, #e53935 100%);
animation: recording-pulse 1s infinite;
}
@keyframes recording-pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.1); }
}
.record-btn .icon {
font-size: 36px;
}
.record-btn .text {
font-size: 14px;
}
.record-status {
margin-top: 15px;
font-size: 14px;
color: #666;
}
.record-status.recording {
color: #f44336;
font-weight: bold;
}
.text-section {
margin: 20px 0;
}
.text-input-wrapper {
display: flex;
gap: 10px;
}
.text-input {
flex: 1;
padding: 12px 15px;
border: 2px solid #eee;
border-radius: 10px;
font-size: 15px;
outline: none;
transition: border-color 0.2s;
}
.text-input:focus {
border-color: #667eea;
}
.send-text-btn {
padding: 12px 20px;
border: none;
border-radius: 10px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
font-size: 15px;
cursor: pointer;
transition: all 0.2s;
}
.send-text-btn:hover {
transform: scale(1.05);
}
.send-text-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
transform: none;
}
.waveform {
display: flex;
justify-content: center;
gap: 3px;
margin-top: 15px;
height: 30px;
}
.wave-bar {
width: 4px;
height: 10px;
background: #667eea;
border-radius: 2px;
animation: wave 0.5s infinite ease-in-out;
}
.wave-bar:nth-child(odd) {
animation-delay: 0.1s;
}
.wave-bar:nth-child(3n) {
animation-delay: 0.2s;
}
@keyframes wave {
0%, 100% { height: 10px; }
50% { height: 25px; }
}
.chat-section {
max-height: 400px;
overflow-y: auto;
border: 1px solid #eee;
border-radius: 10px;
padding: 15px;
background: #f9f9f9;
}
.message {
margin-bottom: 15px;
padding: 12px 15px;
border-radius: 10px;
max-width: 80%;
}
.message.user {
background: #667eea;
color: white;
margin-left: auto;
}
.message.assistant {
background: white;
color: #333;
border: 1px solid #eee;
}
.message .role {
font-size: 12px;
font-weight: bold;
margin-bottom: 5px;
opacity: 0.8;
}
.message .content {
font-size: 15px;
line-height: 1.5;
}
.audio-content {
display: flex;
align-items: center;
}
.play-btn {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 8px 15px;
border-radius: 20px;
border: none;
background: rgba(255,255,255,0.2);
cursor: pointer;
transition: all 0.2s;
font-size: 14px;
}
.play-btn:hover {
background: rgba(255,255,255,0.3);
}
.play-btn.playing {
background: rgba(255,255,255,0.4);
}
.play-icon {
font-size: 16px;
}
.duration {
color: rgba(255,255,255,0.8);
}
.loading {
display: flex;
justify-content: center;
gap: 8px;
padding: 20px;
}
.loading-dot {
width: 10px;
height: 10px;
border-radius: 50%;
background: #667eea;
animation: loading-bounce 0.6s infinite ease-in-out;
}
.loading-dot:nth-child(1) { animation-delay: 0s; }
.loading-dot:nth-child(2) { animation-delay: 0.1s; }
.loading-dot:nth-child(3) { animation-delay: 0.2s; }
@keyframes loading-bounce {
0%, 80%, 100% { transform: scale(0); }
40% { transform: scale(1); }
}
.actions {
margin-top: 20px;
text-align: center;
}
.clear-btn {
padding: 10px 20px;
border: 1px solid #ddd;
border-radius: 8px;
background: white;
color: #666;
font-size: 14px;
cursor: pointer;
transition: all 0.2s;
}
.clear-btn:hover {
background: #f5f5f5;
}
.error-message {
background: #ffebee;
color: #c62828;
padding: 15px;
border-radius: 10px;
margin-bottom: 15px;
text-align: center;
}
.hint {
margin-top: 15px;
font-size: 13px;
color: #999;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🎤 语音对话</h1>
<div class="status-indicator">
<span class="status-dot" id="statusDot"></span>
<span id="statusText">检测连接...</span>
</div>
</div>
<div class="record-section">
<button class="record-btn" id="recordBtn">
<span class="icon">🎤</span>
<span class="text">点击录音</span>
</button>
<div class="record-status" id="recordStatus">点击按钮开始录音</div>
<div class="waveform" id="waveform" style="display: none;">
<div class="wave-bar"></div>
<div class="wave-bar"></div>
<div class="wave-bar"></div>
<div class="wave-bar"></div>
<div class="wave-bar"></div>
<div class="wave-bar"></div>
<div class="wave-bar"></div>
<div class="wave-bar"></div>
</div>
</div>
<div class="text-section">
<div class="text-input-wrapper">
<input type="text" id="textInput" placeholder="输入文字消息..." class="text-input">
<button id="sendTextBtn" class="send-text-btn">发送</button>
</div>
</div>
<div class="chat-section" id="chatSection">
<div class="hint">开始你的第一次语音对话吧!</div>
</div>
<div class="actions">
<button class="clear-btn" id="clearBtn">清除对话</button>
</div>
</div>
<script>
// 配置
const API_URL = '/api';
// 状态
let isRecording = false;
let mediaRecorder = null;
let audioChunks = [];
let conversationId = null;
let audioContext = null;
let audioStream = null;
let scriptProcessor = null;
let recordedBuffers = [];
// 元素
const recordBtn = document.getElementById('recordBtn');
const recordStatus = document.getElementById('recordStatus');
const waveform = document.getElementById('waveform');
const chatSection = document.getElementById('chatSection');
const clearBtn = document.getElementById('clearBtn');
const statusDot = document.getElementById('statusDot');
const statusText = document.getElementById('statusText');
const textInput = document.getElementById('textInput');
const sendTextBtn = document.getElementById('sendTextBtn');
// 发送文字消息
async function sendText(text) {
if (!text.trim()) return;
try {
showLoading();
const formData = new FormData();
formData.append('text', text);
if (conversationId) {
formData.append('conversation_id', conversationId);
}
const resp = await fetch(`${API_URL}/voice/text`, {
method: 'POST',
body: formData
});
if (!resp.ok) {
const error = await resp.text();
throw new Error(error);
}
const data = await resp.json();
conversationId = data.conversation_id;
// 显示消息
addMessage('user', text);
addMessage('assistant', data.reply);
textInput.value = '';
} catch (e) {
console.error('发送失败:', e);
showError('发送失败: ' + e.message);
}
}
// 检查服务状态
async function checkStatus() {
try {
const resp = await fetch(`${API_URL}/status`);
const data = await resp.json();
if (data.model_online) {
statusDot.className = 'status-dot online';
statusText.textContent = '模型服务已连接';
} else {
statusDot.className = 'status-dot offline';
statusText.textContent = '模型服务离线';
}
} catch (e) {
statusDot.className = 'status-dot offline';
statusText.textContent = '服务连接失败';
}
}
// 创建 WAV 文件
function createWavFile(audioBuffer, sampleRate = 16000) {
const numChannels = 1;
const bitsPerSample = 16;
const bytesPerSample = bitsPerSample / 8;
const blockAlign = numChannels * bytesPerSample;
const byteRate = sampleRate * blockAlign;
const dataSize = audioBuffer.length * bytesPerSample;
const headerSize = 44;
const totalSize = headerSize + dataSize;
const buffer = new ArrayBuffer(totalSize);
const view = new DataView(buffer);
// WAV header
writeString(view, 0, 'RIFF');
view.setUint32(4, totalSize - 8, true);
writeString(view, 8, 'WAVE');
writeString(view, 12, 'fmt ');
view.setUint32(16, 16, true); // fmt chunk size
view.setUint16(20, 1, true); // audio format (PCM)
view.setUint16(22, numChannels, true);
view.setUint32(24, sampleRate, true);
view.setUint32(28, byteRate, true);
view.setUint16(32, blockAlign, true);
view.setUint16(34, bitsPerSample, true);
writeString(view, 36, 'data');
view.setUint32(40, dataSize, true);
// 写入音频数据
floatTo16BitPCM(view, 44, audioBuffer);
return new Blob([buffer], { type: 'audio/wav' });
}
function writeString(view, offset, string) {
for (let i = 0; i < string.length; i++) {
view.setUint8(offset + i, string.charCodeAt(i));
}
}
function floatTo16BitPCM(view, offset, input) {
for (let i = 0; i < input.length; i++, offset += 2) {
const s = Math.max(-1, Math.min(1, input[i]));
view.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true);
}
}
// 初始化录音
async function initAudio() {
try {
audioStream = await navigator.mediaDevices.getUserMedia({
audio: {
echoCancellation: true,
noiseSuppression: true,
sampleRate: 16000
}
});
audioContext = new (window.AudioContext || window.webkitAudioContext)({
sampleRate: 16000
});
const source = audioContext.createMediaStreamSource(audioStream);
scriptProcessor = audioContext.createScriptProcessor(4096, 1, 1);
scriptProcessor.onaudioprocess = (e) => {
if (isRecording) {
recordedBuffers.push(e.inputBuffer.getChannelData(0).slice());
}
};
source.connect(scriptProcessor);
scriptProcessor.connect(audioContext.destination);
return true;
} catch (e) {
console.error('获取麦克风失败:', e);
showError('无法访问麦克风,请检查权限设置');
return false;
}
}
// 开始录音
async function startRecording() {
if (!audioContext) {
const success = await initAudio();
if (!success) return;
}
recordedBuffers = [];
isRecording = true;
recordBtn.classList.add('recording');
recordBtn.querySelector('.icon').textContent = '⏹️';
recordBtn.querySelector('.text').textContent = '停止录音';
recordStatus.textContent = '正在录音...';
recordStatus.classList.add('recording');
waveform.style.display = 'flex';
}
// 停止录音
function stopRecording() {
if (isRecording) {
isRecording = false;
// 合并所有缓冲区
const totalLength = recordedBuffers.reduce((acc, buf) => acc + buf.length, 0);
const mergedBuffer = new Float32Array(totalLength);
let offset = 0;
for (const buf of recordedBuffers) {
mergedBuffer.set(buf, offset);
offset += buf.length;
}
// 创建 WAV 文件
const wavBlob = createWavFile(mergedBuffer, 16000);
recordBtn.classList.remove('recording');
recordBtn.querySelector('.icon').textContent = '🎤';
recordBtn.querySelector('.text').textContent = '点击录音';
recordStatus.textContent = '处理中...';
recordStatus.classList.remove('recording');
waveform.style.display = 'none';
sendAudio(wavBlob);
}
}
// 发送音频
async function sendAudio(audioBlob) {
try {
showLoading();
// 计算音频时长
const duration = Math.round(recordedBuffers.reduce((acc, buf) => acc + buf.length, 0) / 16000);
const formData = new FormData();
formData.append('audio', audioBlob, 'recording.wav');
if (conversationId) {
formData.append('conversation_id', conversationId);
}
const resp = await fetch(`${API_URL}/voice/chat`, {
method: 'POST',
body: formData
});
if (!resp.ok) {
const error = await resp.text();
throw new Error(error);
}
const data = await resp.json();
conversationId = data.conversation_id;
// 显示消息(带音频播放)
addMessage('user', audioBlob, duration);
addMessage('assistant', data.reply);
recordStatus.textContent = '点击按钮开始录音';
} catch (e) {
console.error('发送失败:', e);
showError('发送失败: ' + e.message);
recordStatus.textContent = '发送失败,点击重试';
}
}
// 添加消息
function addMessage(role, content, audioDuration = null) {
// 移除提示
const hint = chatSection.querySelector('.hint');
if (hint) hint.remove();
// 移除加载
const loading = chatSection.querySelector('.loading');
if (loading) loading.remove();
const msg = document.createElement('div');
msg.className = `message ${role}`;
// 用户消息可能是音频
if (role === 'user' && content instanceof Blob) {
const audioUrl = URL.createObjectURL(content);
const durationText = audioDuration ? `${audioDuration}s` : '';
msg.innerHTML = `
<div class="role">我</div>
<div class="content audio-content">
<button class="play-btn" onclick="playAudio('${audioUrl}', this)">
<span class="play-icon">▶️</span>
<span class="duration">${durationText}</span>
</button>
</div>
`;
} else {
msg.innerHTML = `
<div class="role">${role === 'user' ? '我' : 'AI'}</div>
<div class="content">${content}</div>
`;
}
chatSection.appendChild(msg);
// 滚动到底部
chatSection.scrollTop = chatSection.scrollHeight;
}
// 播放音频
function playAudio(audioUrl, btn) {
const audio = new Audio(audioUrl);
const icon = btn.querySelector('.play-icon');
audio.onplay = () => {
icon.textContent = '🔊';
btn.classList.add('playing');
};
audio.onended = () => {
icon.textContent = '▶️';
btn.classList.remove('playing');
};
audio.play();
}
// 显示加载
function showLoading() {
const hint = chatSection.querySelector('.hint');
if (hint) hint.remove();
const loading = document.createElement('div');
loading.className = 'loading';
loading.innerHTML = `
<div class="loading-dot"></div>
<div class="loading-dot"></div>
<div class="loading-dot"></div>
`;
chatSection.appendChild(loading);
chatSection.scrollTop = chatSection.scrollHeight;
}
// 显示错误
function showError(message) {
const loading = chatSection.querySelector('.loading');
if (loading) loading.remove();
const error = document.createElement('div');
error.className = 'error-message';
error.textContent = message;
chatSection.appendChild(error);
// 3秒后移除
setTimeout(() => error.remove(), 3000);
}
// 清除对话
async function clearChat() {
if (conversationId) {
try {
await fetch(`${API_URL}/conversation/${conversationId}`, {
method: 'DELETE'
});
} catch (e) {}
}
conversationId = null;
chatSection.innerHTML = '<div class="hint">开始你的第一次语音对话吧!</div>';
}
// 事件绑定
recordBtn.addEventListener('click', () => {
if (isRecording) {
stopRecording();
} else {
startRecording();
}
});
clearBtn.addEventListener('click', clearChat);
// 文字输入事件
sendTextBtn.addEventListener('click', () => {
sendText(textInput.value);
});
textInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendText(textInput.value);
}
});
// 初始化
checkStatus();
setInterval(checkStatus, 10000); // 每10秒检查状态
</script>
</body>
</html>