Download index.html from fdbw/grok_2mkf: direct link, hf CLI and curl.
- Browser
- Download file 14.9 kB
-
https://huggingface.co/spaces/fdbw/grok_2mkf/resolve/main/index.html
- Command line
-
hf download hf://spaces/fdbw/grok_2mkf/index.html
-
curl -L -o index.html https://huggingface.co/spaces/fdbw/grok_2mkf/resolve/main/index.html
14.9 kB
| <html lang="zh"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> | |
| <title>实时互译</title> | |
| <style> | |
| * { box-sizing: border-box; margin: 0; padding: 0; } | |
| html, body { | |
| height: 100%; background: #0d0d0d; color: #eee; | |
| font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", sans-serif; | |
| overflow: hidden; | |
| } | |
| .screen { display: none; height: 100%; flex-direction: column; } | |
| .screen.active { display: flex; } | |
| #startScreen { | |
| justify-content: center; align-items: center; gap: 20px; | |
| padding: 24px 16px; text-align: center; | |
| } | |
| #startScreen h2 { font-size: 24px; color: #8ecfff; } | |
| .subtitle { font-size: 13px; color: #888; line-height: 1.6; } | |
| .lang-row { display: flex; gap: 16px; justify-content: center; width: 100%; max-width: 360px; } | |
| .lang-box { display: flex; flex-direction: column; gap: 6px; flex: 1; } | |
| .lang-box label { font-size: 13px; } | |
| .lang-label-a { color: #4fc3f7; } | |
| .lang-label-b { color: #ffb74d; } | |
| .lang-box select { | |
| width: 100%; font-size: 15px; padding: 10px; | |
| background: #1a1a1a; color: #eee; border: 1px solid #333; border-radius: 10px; | |
| text-align: center; | |
| } | |
| .bigBtn { | |
| width: 200px; padding: 14px; font-size: 16px; border: none; border-radius: 12px; | |
| color: #fff; background: linear-gradient(135deg, #1b5e20, #2e7d32); cursor: pointer; | |
| } | |
| #callScreen { height: 100%; } | |
| #topBar { | |
| display: flex; justify-content: space-between; align-items: center; | |
| padding: 10px 14px; background: #111; border-bottom: 1px solid #222; | |
| } | |
| #topBar .title { font-size: 15px; font-weight: bold; color: #8ecfff; } | |
| .top-btn { | |
| background: rgba(255,255,255,0.06); color: #aaa; border: 1px solid #333; | |
| border-radius: 8px; padding: 5px 12px; font-size: 12px; margin-left: 6px; cursor: pointer; | |
| } | |
| #statusBar { | |
| padding: 8px 14px; text-align: center; font-size: 13px; color: #ccc; | |
| background: #111; border-bottom: 1px solid #1a1a1a; min-height: 40px; | |
| } | |
| #messageArea { | |
| flex: 1; overflow-y: auto; padding: 12px 14px; display: flex; flex-direction: column; gap: 10px; | |
| } | |
| #messageArea:empty::after { | |
| content: "点击麦克风开始说话,说完自动结束"; | |
| display: block; margin: auto; color: #444; font-size: 14px; text-align: center; | |
| } | |
| .msg-item { | |
| background: #141414; border-radius: 10px; padding: 10px 14px; | |
| border-left: 4px solid #333; | |
| } | |
| .msg-item.role-a { border-left-color: #4fc3f7; } | |
| .msg-item.role-b { border-left-color: #ffb74d; } | |
| .msg-header { font-size: 11px; color: #888; margin-bottom: 4px; } | |
| .msg-original { font-size: 16px; color: #ddd; margin-bottom: 4px; } | |
| .msg-translation { font-size: 15px; color: #ffe58e; border-top: 1px solid #2a2a2a; padding-top: 4px; } | |
| .msg-translation.pending { color: #666; font-style: italic; } | |
| #debugBar { | |
| font-size: 12px; color: #888; padding: 5px 10px; background: #1a1a1a; | |
| border-top: 1px solid #333; text-align: center; | |
| } | |
| #micArea { display: flex; border-top: 1px solid #1a1a1a; background: #0a0a0a; } | |
| .mic-btn-wrap { | |
| flex: 1; display: flex; flex-direction: column; align-items: center; | |
| padding: 14px 8px 16px; gap: 6px; border-right: 1px solid #1a1a1a; | |
| } | |
| .mic-btn-wrap:last-child { border-right: none; } | |
| .mic-label { font-size: 12px; font-weight: 600; } | |
| .mic-label-a { color: #4fc3f7; } | |
| .mic-label-b { color: #ffb74d; } | |
| .mic-btn { | |
| width: 70px; height: 70px; border-radius: 50%; border: none; font-size: 26px; | |
| display: flex; align-items: center; justify-content: center; cursor: pointer; | |
| } | |
| #micA { background: #1a2a3a; color: #4fc3f7; box-shadow: 0 0 0 3px rgba(79,195,247,0.25); } | |
| #micB { background: #2a1e0a; color: #ffb74d; box-shadow: 0 0 0 3px rgba(255,183,77,0.25); } | |
| .mic-btn.recording { animation: pulse 1.1s infinite; } | |
| #micA.recording { background: #0a4a7a; } | |
| #micB.recording { background: #5a2e00; } | |
| .mic-btn.locked { opacity: 0.3; cursor: not-allowed; } | |
| @keyframes pulse { | |
| 0%,100% { transform: scale(1); } | |
| 50% { transform: scale(1.07); } | |
| } | |
| .mic-status { font-size: 11px; color: #666; height: 16px; } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="startScreen" class="screen active"> | |
| <h2>🗣️ 实时互译</h2> | |
| <div class="lang-row"> | |
| <div class="lang-box"> | |
| <label class="lang-label-a">语言 A</label> | |
| <select id="langA"></select> | |
| </div> | |
| <div class="lang-box"> | |
| <label class="lang-label-b">语言 B</label> | |
| <select id="langB"></select> | |
| </div> | |
| </div> | |
| <button class="bigBtn" id="startBtn">开始通话</button> | |
| <div class="subtitle"> | |
| 点击一次麦克风开始说话<br> | |
| 说完后自动翻译并播放 | |
| </div> | |
| </div> | |
| <div id="callScreen" class="screen"> | |
| <div id="topBar"> | |
| <span class="title">互译中</span> | |
| <div> | |
| <button class="top-btn" id="clearBtn">清空</button> | |
| <button class="top-btn" id="exitBtn">退出</button> | |
| </div> | |
| </div> | |
| <div id="statusBar">准备就绪</div> | |
| <div id="messageArea"></div> | |
| <div id="debugBar">等待操作</div> | |
| <div id="micArea"> | |
| <div class="mic-btn-wrap"> | |
| <div class="mic-label mic-label-a">A 说话</div> | |
| <button class="mic-btn" id="micA">🎤</button> | |
| <div class="mic-status" id="statusA">点击开始</div> | |
| </div> | |
| <div class="mic-btn-wrap"> | |
| <div class="mic-label mic-label-b">B 说话</div> | |
| <button class="mic-btn" id="micB">🎤</button> | |
| <div class="mic-status" id="statusB">点击开始</div> | |
| </div> | |
| </div> | |
| </div> | |
| <script> | |
| const LANGS = [ | |
| { code: 'zh-CN', name: '中文' }, | |
| { code: 'en-US', name: 'English' }, | |
| { code: 'fr-FR', name: 'Français' }, | |
| { code: 'ja-JP', name: '日本語' }, | |
| ]; | |
| const langASel = document.getElementById('langA'); | |
| const langBSel = document.getElementById('langB'); | |
| LANGS.forEach(l => { | |
| [langASel, langBSel].forEach(sel => { | |
| const o = document.createElement('option'); | |
| o.value = l.code; o.textContent = l.name; | |
| sel.appendChild(o); | |
| }); | |
| }); | |
| langASel.value = 'zh-CN'; | |
| langBSel.value = 'en-US'; | |
| let langA = 'zh-CN', langB = 'en-US'; | |
| let recordingRole = null; | |
| let isPlaying = false; | |
| let recognition = null; | |
| let currentItem = null; | |
| let collectedText = ''; | |
| let silenceTimer = null; | |
| let maxTimer = null; | |
| const statusBar = document.getElementById('statusBar'); | |
| const debugBar = document.getElementById('debugBar'); | |
| const messageArea = document.getElementById('messageArea'); | |
| const micA = document.getElementById('micA'); | |
| const micB = document.getElementById('micB'); | |
| const statusA = document.getElementById('statusA'); | |
| const statusB = document.getElementById('statusB'); | |
| function log(msg) { | |
| console.log(msg); | |
| debugBar.textContent = msg; | |
| } | |
| // 强制加载语音列表 | |
| function loadVoices() { | |
| return new Promise(resolve => { | |
| let voices = speechSynthesis.getVoices(); | |
| if (voices.length > 0) { | |
| resolve(voices); | |
| return; | |
| } | |
| speechSynthesis.onvoiceschanged = () => { | |
| voices = speechSynthesis.getVoices(); | |
| resolve(voices); | |
| }; | |
| // 超时保护 | |
| setTimeout(() => resolve(speechSynthesis.getVoices()), 1000); | |
| }); | |
| } | |
| // 页面加载时就预加载 | |
| loadVoices().then(v => log('语音预加载完成: ' + v.length + ' 个')); | |
| function setUI(role, state) { | |
| const btn = role === 'a' ? micA : micB; | |
| const sts = role === 'a' ? statusA : statusB; | |
| btn.classList.remove('recording', 'locked'); | |
| if (state === 'recording') { | |
| btn.classList.add('recording'); | |
| sts.textContent = '说话中...'; | |
| } else if (state === 'locked') { | |
| btn.classList.add('locked'); | |
| sts.textContent = isPlaying ? '播放中' : '锁定'; | |
| } else { | |
| sts.textContent = '点击开始'; | |
| } | |
| } | |
| function refreshUI() { | |
| if (isPlaying) { | |
| setUI('a', 'locked'); setUI('b', 'locked'); | |
| } else if (recordingRole === 'a') { | |
| setUI('a', 'recording'); setUI('b', 'idle'); | |
| } else if (recordingRole === 'b') { | |
| setUI('a', 'idle'); setUI('b', 'recording'); | |
| } else { | |
| setUI('a', 'idle'); setUI('b', 'idle'); | |
| } | |
| } | |
| function clearTimers() { | |
| if (silenceTimer) { clearTimeout(silenceTimer); silenceTimer = null; } | |
| if (maxTimer) { clearTimeout(maxTimer); maxTimer = null; } | |
| } | |
| function forceStop() { | |
| clearTimers(); | |
| if (recognition) { | |
| try { | |
| recognition.onresult = null; | |
| recognition.onerror = null; | |
| recognition.onend = null; | |
| recognition.stop(); | |
| } catch(e){} | |
| recognition = null; | |
| } | |
| } | |
| function finishListening(role) { | |
| forceStop(); | |
| const text = collectedText.trim(); | |
| recordingRole = null; | |
| refreshUI(); | |
| if (text) { | |
| processFinal(role, text); | |
| } else { | |
| statusBar.textContent = '没有识别到内容,请重试'; | |
| log('结果为空'); | |
| } | |
| } | |
| function startListening(role) { | |
| if (isPlaying) { | |
| statusBar.textContent = '请等待播放结束'; | |
| return; | |
| } | |
| if (recordingRole === role) { | |
| finishListening(role); | |
| return; | |
| } | |
| if (recordingRole) { | |
| forceStop(); | |
| recordingRole = null; | |
| } | |
| collectedText = ''; | |
| currentItem = null; | |
| forceStop(); | |
| const SR = window.SpeechRecognition || window.webkitSpeechRecognition; | |
| if (!SR) { | |
| alert('当前浏览器不支持语音识别,请使用 Chrome'); | |
| return; | |
| } | |
| recognition = new SR(); | |
| recognition.lang = (role === 'a' ? langA : langB); | |
| recognition.continuous = false; | |
| recognition.interimResults = true; | |
| recognition.maxAlternatives = 1; | |
| recordingRole = role; | |
| refreshUI(); | |
| statusBar.textContent = role.toUpperCase() + ' 请说话,说完自动结束'; | |
| log('开始识别'); | |
| recognition.onresult = (event) => { | |
| let interim = ''; | |
| let final = ''; | |
| for (let i = event.resultIndex; i < event.results.length; i++) { | |
| const t = event.results[i][0].transcript; | |
| if (event.results[i].isFinal) final += t; | |
| else interim += t; | |
| } | |
| if (final) { | |
| collectedText = (collectedText + ' ' + final).trim(); | |
| log('最终: ' + final); | |
| } | |
| const show = (collectedText + (interim ? ' ' + interim : '')).trim(); | |
| if (show) { | |
| if (!currentItem) currentItem = createItem(role); | |
| currentItem.querySelector('.msg-original').textContent = show + (interim ? '…' : ''); | |
| currentItem.querySelector('.msg-translation').textContent = '识别中...'; | |
| currentItem.querySelector('.msg-translation').classList.add('pending'); | |
| scrollBottom(); | |
| } | |
| if (final) { | |
| clearTimeout(silenceTimer); | |
| silenceTimer = setTimeout(() => { | |
| if (recordingRole === role) finishListening(role); | |
| }, 1000); | |
| } | |
| }; | |
| recognition.onerror = (e) => { | |
| if (e.error === 'no-speech') { | |
| statusBar.textContent = '没检测到语音'; | |
| recordingRole = null; | |
| refreshUI(); | |
| } | |
| }; | |
| recognition.onend = () => { | |
| if (recordingRole === role && collectedText.trim()) { | |
| finishListening(role); | |
| } else if (recordingRole === role) { | |
| recordingRole = null; | |
| refreshUI(); | |
| statusBar.textContent = '没有识别到内容'; | |
| } | |
| }; | |
| try { | |
| recognition.start(); | |
| maxTimer = setTimeout(() => { | |
| if (recordingRole === role) finishListening(role); | |
| }, 12000); | |
| } catch (e) { | |
| recordingRole = null; | |
| refreshUI(); | |
| } | |
| } | |
| async function processFinal(role, text) { | |
| const sourceLang = role === 'a' ? langA : langB; | |
| const targetLang = role === 'a' ? langB : langA; | |
| if (!currentItem) currentItem = createItem(role); | |
| currentItem.querySelector('.msg-original').textContent = text; | |
| currentItem.querySelector('.msg-translation').textContent = '翻译中...'; | |
| currentItem.querySelector('.msg-translation').classList.add('pending'); | |
| scrollBottom(); | |
| statusBar.textContent = '正在翻译...'; | |
| let translated = text; | |
| try { | |
| const from = sourceLang.split('-')[0]; | |
| const to = targetLang.split('-')[0]; | |
| const url = `https://api.mymemory.translated.net/get?q=${encodeURIComponent(text)}&langpair=${from}|${to}`; | |
| const res = await fetch(url); | |
| const data = await res.json(); | |
| if (data.responseData?.translatedText) { | |
| translated = data.responseData.translatedText; | |
| } | |
| } catch (e) {} | |
| currentItem.querySelector('.msg-translation').textContent = translated; | |
| currentItem.querySelector('.msg-translation').classList.remove('pending'); | |
| scrollBottom(); | |
| // 立即播放 | |
| speak(translated, targetLang); | |
| } | |
| async function speak(text, langCode) { | |
| if (!window.speechSynthesis) { | |
| statusBar.textContent = '不支持语音播放'; | |
| return; | |
| } | |
| isPlaying = true; | |
| refreshUI(); | |
| statusBar.textContent = '正在播放...'; | |
| const voices = await loadVoices(); | |
| const langPrefix = langCode.split('-')[0]; | |
| // 优先选本地语音,减少延迟 | |
| let selected = null; | |
| const candidates = voices.filter(v => v.lang.toLowerCase().startsWith(langPrefix)); | |
| // 优先级:本地 > Google > Microsoft > 任意 | |
| selected = candidates.find(v => v.localService) | |
| || candidates.find(v => v.name.includes('Google')) | |
| || candidates.find(v => v.name.includes('Microsoft')) | |
| || candidates[0]; | |
| const utter = new SpeechSynthesisUtterance(text); | |
| utter.lang = langCode; | |
| utter.rate = 1.08; | |
| utter.volume = 1; | |
| if (selected) { | |
| utter.voice = selected; | |
| log('播放声音: ' + selected.name + (selected.localService ? ' (本地)' : '')); | |
| } | |
| utter.onend = () => { | |
| isPlaying = false; | |
| refreshUI(); | |
| statusBar.textContent = '播放结束,可继续说话'; | |
| }; | |
| utter.onerror = () => { | |
| isPlaying = false; | |
| refreshUI(); | |
| statusBar.textContent = '播放出错'; | |
| }; | |
| speechSynthesis.cancel(); | |
| speechSynthesis.speak(utter); | |
| } | |
| function createItem(role) { | |
| const item = document.createElement('div'); | |
| item.className = 'msg-item role-' + role; | |
| item.innerHTML = ` | |
| <div class="msg-header">${role.toUpperCase()}</div> | |
| <div class="msg-original">…</div> | |
| <div class="msg-translation pending">…</div> | |
| `; | |
| messageArea.appendChild(item); | |
| return item; | |
| } | |
| function scrollBottom() { | |
| messageArea.scrollTop = messageArea.scrollHeight; | |
| } | |
| micA.onclick = () => startListening('a'); | |
| micB.onclick = () => startListening('b'); | |
| document.getElementById('startBtn').onclick = async () => { | |
| langA = langASel.value; | |
| langB = langBSel.value; | |
| if (langA === langB) return alert('请选择不同语言'); | |
| document.getElementById('startScreen').classList.remove('active'); | |
| document.getElementById('callScreen').classList.add('active'); | |
| messageArea.innerHTML = ''; | |
| collectedText = ''; | |
| recordingRole = null; | |
| isPlaying = false; | |
| statusBar.textContent = '点击麦克风开始说话'; | |
| refreshUI(); | |
| await loadVoices(); // 进入时再加载一次 | |
| }; | |
| document.getElementById('clearBtn').onclick = () => { | |
| messageArea.innerHTML = ''; | |
| currentItem = null; | |
| }; | |
| document.getElementById('exitBtn').onclick = () => { | |
| forceStop(); | |
| speechSynthesis.cancel(); | |
| document.getElementById('callScreen').classList.remove('active'); | |
| document.getElementById('startScreen').classList.add('active'); | |
| }; | |
| log('电脑优化版已加载'); | |
| </script> | |
| </body> | |
| </html> |