fdbw commited on
Commit
76e1193
·
verified ·
1 Parent(s): 42fa36c

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +448 -19
index.html CHANGED
@@ -1,19 +1,448 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="zh">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
6
+ <title>实时互译 - 诊断版</title>
7
+ <style>
8
+ * { box-sizing: border-box; margin: 0; padding: 0; }
9
+ html, body {
10
+ height: 100%; background: #0d0d0d; color: #eee;
11
+ font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", sans-serif;
12
+ touch-action: manipulation; overflow: hidden;
13
+ }
14
+ .screen { display: none; height: 100%; flex-direction: column; }
15
+ .screen.active { display: flex; }
16
+
17
+ #startScreen {
18
+ justify-content: center; align-items: center; gap: 20px;
19
+ padding: 24px 16px; text-align: center;
20
+ }
21
+ #startScreen h2 { font-size: 22px; color: #8ecfff; }
22
+ .subtitle { font-size: 13px; color: #888; line-height: 1.6; }
23
+ .lang-row { display: flex; gap: 14px; justify-content: center; width: 100%; max-width: 340px; }
24
+ .lang-box { display: flex; flex-direction: column; gap: 6px; flex: 1; }
25
+ .lang-box label { font-size: 13px; }
26
+ .lang-label-a { color: #4fc3f7; }
27
+ .lang-label-b { color: #ffb74d; }
28
+ .lang-box select {
29
+ width: 100%; font-size: 15px; padding: 10px;
30
+ background: #1a1a1a; color: #eee; border: 1px solid #333; border-radius: 10px;
31
+ text-align: center;
32
+ }
33
+ .bigBtn {
34
+ width: 200px; padding: 14px; font-size: 16px; border: none; border-radius: 12px;
35
+ color: #fff; background: linear-gradient(135deg, #1b5e20, #2e7d32);
36
+ }
37
+ .warning {
38
+ background: #3e2723; color: #ffcc80; padding: 10px 14px; border-radius: 10px;
39
+ font-size: 13px; line-height: 1.5; max-width: 320px;
40
+ }
41
+
42
+ #callScreen { height: 100%; }
43
+ #topBar {
44
+ display: flex; justify-content: space-between; align-items: center;
45
+ padding: 10px 12px; background: #111; border-bottom: 1px solid #222;
46
+ }
47
+ #topBar .title { font-size: 15px; font-weight: bold; color: #8ecfff; }
48
+ .top-btn {
49
+ background: rgba(255,255,255,0.06); color: #aaa; border: 1px solid #333;
50
+ border-radius: 8px; padding: 5px 10px; font-size: 12px; margin-left: 6px;
51
+ }
52
+ #statusBar {
53
+ padding: 8px 12px; text-align: center; font-size: 13px; color: #ccc;
54
+ background: #111; border-bottom: 1px solid #1a1a1a; min-height: 40px;
55
+ }
56
+ #messageArea {
57
+ flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px;
58
+ }
59
+ #messageArea:empty::after {
60
+ content: "先点麦克风开始 → 说话 → 再点同一个麦克风结束";
61
+ display: block; margin: auto; color: #444; font-size: 14px; text-align: center; padding: 20px;
62
+ }
63
+ .msg-item {
64
+ background: #141414; border-radius: 10px; padding: 10px 12px;
65
+ border-left: 4px solid #333;
66
+ }
67
+ .msg-item.role-a { border-left-color: #4fc3f7; }
68
+ .msg-item.role-b { border-left-color: #ffb74d; }
69
+ .msg-header { font-size: 11px; color: #888; margin-bottom: 4px; }
70
+ .msg-original { font-size: 16px; color: #ddd; margin-bottom: 4px; }
71
+ .msg-translation { font-size: 15px; color: #ffe58e; border-top: 1px solid #2a2a2a; padding-top: 4px; }
72
+ .msg-translation.pending { color: #666; font-style: italic; }
73
+
74
+ #debugBar {
75
+ font-size: 12px; color: #aaa; padding: 6px 10px; background: #1a1a1a;
76
+ border-top: 1px solid #333; text-align: center; min-height: 32px;
77
+ }
78
+
79
+ #micArea { display: flex; border-top: 1px solid #1a1a1a; background: #0a0a0a; }
80
+ .mic-btn-wrap {
81
+ flex: 1; display: flex; flex-direction: column; align-items: center;
82
+ padding: 14px 8px 16px; gap: 6px; border-right: 1px solid #1a1a1a;
83
+ }
84
+ .mic-btn-wrap:last-child { border-right: none; }
85
+ .mic-label { font-size: 12px; font-weight: 600; }
86
+ .mic-label-a { color: #4fc3f7; }
87
+ .mic-label-b { color: #ffb74d; }
88
+ .mic-btn {
89
+ width: 70px; height: 70px; border-radius: 50%; border: none; font-size: 26px;
90
+ display: flex; align-items: center; justify-content: center;
91
+ }
92
+ #micA { background: #1a2a3a; color: #4fc3f7; box-shadow: 0 0 0 3px rgba(79,195,247,0.25); }
93
+ #micB { background: #2a1e0a; color: #ffb74d; box-shadow: 0 0 0 3px rgba(255,183,77,0.25); }
94
+ .mic-btn.recording { animation: pulse 1.1s infinite; }
95
+ #micA.recording { background: #0a4a7a; }
96
+ #micB.recording { background: #5a2e00; }
97
+ .mic-btn.locked { opacity: 0.3; }
98
+ @keyframes pulse {
99
+ 0%,100% { transform: scale(1); }
100
+ 50% { transform: scale(1.07); }
101
+ }
102
+ .mic-status { font-size: 11px; color: #666; height: 16px; }
103
+ </style>
104
+ </head>
105
+ <body>
106
+
107
+ <div id="startScreen" class="screen active">
108
+ <h2>🗣️ 实时互译 · 诊断版</h2>
109
+ <div class="lang-row">
110
+ <div class="lang-box">
111
+ <label class="lang-label-a">语言 A</label>
112
+ <select id="langA"></select>
113
+ </div>
114
+ <div class="lang-box">
115
+ <label class="lang-label-b">语言 B</label>
116
+ <select id="langB"></select>
117
+ </div>
118
+ </div>
119
+ <button class="bigBtn" id="startBtn">开始通话</button>
120
+ <div class="warning">
121
+ <b>重要:</b>请务必用「新标签页打开」本页面!<br>
122
+ 在 Hugging Face 嵌入窗口里识别经常失败。
123
+ </div>
124
+ <div class="subtitle">
125
+ 操作方法:点击麦克风 → 说话 → <b>再点一次同一个麦克风结束</b>
126
+ </div>
127
+ </div>
128
+
129
+ <div id="callScreen" class="screen">
130
+ <div id="topBar">
131
+ <span class="title">互译中 · 诊断</span>
132
+ <div>
133
+ <button class="top-btn" id="clearBtn">清空</button>
134
+ <button class="top-btn" id="exitBtn">退出</button>
135
+ </div>
136
+ </div>
137
+ <div id="statusBar">准备就绪</div>
138
+ <div id="messageArea"></div>
139
+ <div id="debugBar">等待操作</div>
140
+ <div id="micArea">
141
+ <div class="mic-btn-wrap">
142
+ <div class="mic-label mic-label-a">A 说话</div>
143
+ <button class="mic-btn" id="micA">🎤</button>
144
+ <div class="mic-status" id="statusA">点击开始</div>
145
+ </div>
146
+ <div class="mic-btn-wrap">
147
+ <div class="mic-label mic-label-b">B 说话</div>
148
+ <button class="mic-btn" id="micB">🎤</button>
149
+ <div class="mic-status" id="statusB">点击开始</div>
150
+ </div>
151
+ </div>
152
+ </div>
153
+
154
+ <script>
155
+ const LANGS = [
156
+ { code: 'zh-CN', name: '中文' },
157
+ { code: 'en-US', name: 'English' },
158
+ { code: 'fr-FR', name: 'Français' },
159
+ { code: 'ja-JP', name: '日本語' },
160
+ ];
161
+
162
+ const langASel = document.getElementById('langA');
163
+ const langBSel = document.getElementById('langB');
164
+ LANGS.forEach(l => {
165
+ [langASel, langBSel].forEach(sel => {
166
+ const o = document.createElement('option');
167
+ o.value = l.code; o.textContent = l.name;
168
+ sel.appendChild(o);
169
+ });
170
+ });
171
+ langASel.value = 'zh-CN';
172
+ langBSel.value = 'en-US';
173
+
174
+ let langA = 'zh-CN', langB = 'en-US';
175
+ let recordingRole = null;
176
+ let isPlaying = false;
177
+ let recognition = null;
178
+ let currentItem = null;
179
+ let collectedText = ''; // 收集到的所有文字
180
+ let maxTimer = null;
181
+
182
+ const statusBar = document.getElementById('statusBar');
183
+ const debugBar = document.getElementById('debugBar');
184
+ const messageArea = document.getElementById('messageArea');
185
+ const micA = document.getElementById('micA');
186
+ const micB = document.getElementById('micB');
187
+ const statusA = document.getElementById('statusA');
188
+ const statusB = document.getElementById('statusB');
189
+
190
+ function log(msg) {
191
+ console.log(msg);
192
+ debugBar.textContent = msg;
193
+ }
194
+
195
+ function setUI(role, state) {
196
+ const btn = role === 'a' ? micA : micB;
197
+ const sts = role === 'a' ? statusA : statusB;
198
+ btn.classList.remove('recording', 'locked');
199
+ if (state === 'recording') {
200
+ btn.classList.add('recording');
201
+ sts.textContent = '再说完点一次结束';
202
+ } else if (state === 'locked') {
203
+ btn.classList.add('locked');
204
+ sts.textContent = isPlaying ? '播放中' : '锁定';
205
+ } else {
206
+ sts.textContent = '点击开始';
207
+ }
208
+ }
209
+
210
+ function refreshUI() {
211
+ if (isPlaying) {
212
+ setUI('a', 'locked'); setUI('b', 'locked');
213
+ } else if (recordingRole === 'a') {
214
+ setUI('a', 'recording'); setUI('b', 'idle');
215
+ } else if (recordingRole === 'b') {
216
+ setUI('a', 'idle'); setUI('b', 'recording');
217
+ } else {
218
+ setUI('a', 'idle'); setUI('b', 'idle');
219
+ }
220
+ }
221
+
222
+ function forceStop() {
223
+ if (maxTimer) { clearTimeout(maxTimer); maxTimer = null; }
224
+ if (recognition) {
225
+ try {
226
+ recognition.onresult = null;
227
+ recognition.onerror = null;
228
+ recognition.onend = null;
229
+ recognition.stop();
230
+ } catch(e){}
231
+ recognition = null;
232
+ }
233
+ }
234
+
235
+ function startListening(role) {
236
+ if (isPlaying) {
237
+ statusBar.textContent = '请等待播放结束';
238
+ return;
239
+ }
240
+
241
+ // 再次点击同一个麦克风 = 结束
242
+ if (recordingRole === role) {
243
+ log('手动结束,当前收集文字: ' + (collectedText || '(空)'));
244
+ statusBar.textContent = '正在处理...';
245
+ forceStop();
246
+
247
+ setTimeout(() => {
248
+ const text = collectedText.trim();
249
+ recordingRole = null;
250
+ refreshUI();
251
+ if (text) {
252
+ processFinal(role, text);
253
+ } else {
254
+ statusBar.textContent = '没有识别到任何文字,请重试(建议用新标签页打开)';
255
+ log('最终结果为空');
256
+ }
257
+ }, 500);
258
+ return;
259
+ }
260
+
261
+ // 切换角色时先停掉上一个
262
+ if (recordingRole) {
263
+ forceStop();
264
+ recordingRole = null;
265
+ }
266
+
267
+ collectedText = '';
268
+ currentItem = null;
269
+ forceStop();
270
+
271
+ const SR = window.SpeechRecognition || window.webkitSpeechRecognition;
272
+ if (!SR) {
273
+ alert('当前浏览器不支持语音识别');
274
+ return;
275
+ }
276
+
277
+ recognition = new SR();
278
+ recognition.lang = (role === 'a' ? langA : langB);
279
+ recognition.continuous = true;
280
+ recognition.interimResults = true;
281
+ recognition.maxAlternatives = 1;
282
+
283
+ recordingRole = role;
284
+ refreshUI();
285
+ statusBar.textContent = role.toUpperCase() + ' 请说话,说完后再点一次麦克风';
286
+ log('开始识别 lang=' + recognition.lang);
287
+
288
+ recognition.onstart = () => log('onstart 触发');
289
+
290
+ recognition.onresult = (event) => {
291
+ let interim = '';
292
+ let final = '';
293
+ for (let i = event.resultIndex; i < event.results.length; i++) {
294
+ const t = event.results[i][0].transcript;
295
+ if (event.results[i].isFinal) final += t;
296
+ else interim += t;
297
+ }
298
+
299
+ if (final) {
300
+ collectedText = (collectedText + ' ' + final).trim();
301
+ log('收到最终结果: ' + final);
302
+ }
303
+ if (interim) log('中间结果: ' + interim);
304
+
305
+ const show = (collectedText + (interim ? ' ' + interim : '')).trim();
306
+ if (show) {
307
+ if (!currentItem) currentItem = createItem(role);
308
+ currentItem.querySelector('.msg-original').textContent = show + (interim ? '…' : '');
309
+ currentItem.querySelector('.msg-translation').textContent = '识别中,说完请再点麦克风';
310
+ currentItem.querySelector('.msg-translation').classList.add('pending');
311
+ scrollBottom();
312
+ }
313
+ };
314
+
315
+ recognition.onerror = (e) => {
316
+ log('onerror: ' + e.error);
317
+ if (e.error !== 'aborted') {
318
+ statusBar.textContent = '错误: ' + e.error;
319
+ }
320
+ };
321
+
322
+ recognition.onend = () => log('onend 触发');
323
+
324
+ try {
325
+ recognition.start();
326
+ // 20秒保护
327
+ maxTimer = setTimeout(() => {
328
+ if (recordingRole === role) {
329
+ log('超时自动结束');
330
+ statusBar.textContent = '超时,正在处理...';
331
+ forceStop();
332
+ setTimeout(() => {
333
+ const text = collectedText.trim();
334
+ recordingRole = null;
335
+ refreshUI();
336
+ if (text) processFinal(role, text);
337
+ else statusBar.textContent = '超时且无结果';
338
+ }, 400);
339
+ }
340
+ }, 20000);
341
+ } catch (e) {
342
+ log('start 异常: ' + e.message);
343
+ alert('启动失败: ' + e.message);
344
+ recordingRole = null;
345
+ refreshUI();
346
+ }
347
+ }
348
+
349
+ async function processFinal(role, text) {
350
+ const sourceLang = role === 'a' ? langA : langB;
351
+ const targetLang = role === 'a' ? langB : langA;
352
+
353
+ if (!currentItem) currentItem = createItem(role);
354
+ currentItem.querySelector('.msg-original').textContent = text;
355
+ currentItem.querySelector('.msg-translation').textContent = '翻译中...';
356
+ currentItem.querySelector('.msg-translation').classList.add('pending');
357
+ scrollBottom();
358
+ statusBar.textContent = '正在翻译...';
359
+ log('翻译原文: ' + text);
360
+
361
+ let translated = text;
362
+ try {
363
+ // 简单 fallback
364
+ const from = sourceLang.split('-')[0];
365
+ const to = targetLang.split('-')[0];
366
+ const url = `https://api.mymemory.translated.net/get?q=${encodeURIComponent(text)}&langpair=${from}|${to}`;
367
+ const res = await fetch(url);
368
+ const data = await res.json();
369
+ if (data.responseData?.translatedText) {
370
+ translated = data.responseData.translatedText;
371
+ }
372
+ } catch (e) {
373
+ log('翻译失败');
374
+ }
375
+
376
+ currentItem.querySelector('.msg-translation').textContent = translated;
377
+ currentItem.querySelector('.msg-translation').classList.remove('pending');
378
+ scrollBottom();
379
+
380
+ // 播放
381
+ if (window.speechSynthesis) {
382
+ isPlaying = true;
383
+ refreshUI();
384
+ statusBar.textContent = '播放译音中...';
385
+ const u = new SpeechSynthesisUtterance(translated);
386
+ u.lang = targetLang;
387
+ u.onend = () => {
388
+ isPlaying = false;
389
+ refreshUI();
390
+ statusBar.textContent = '播放结束,可继续';
391
+ };
392
+ speechSynthesis.cancel();
393
+ speechSynthesis.speak(u);
394
+ } else {
395
+ statusBar.textContent = '翻译完成(不支持播放)';
396
+ }
397
+ }
398
+
399
+ function createItem(role) {
400
+ const item = document.createElement('div');
401
+ item.className = 'msg-item role-' + role;
402
+ item.innerHTML = `
403
+ <div class="msg-header">${role.toUpperCase()}</div>
404
+ <div class="msg-original">…</div>
405
+ <div class="msg-translation pending">…</div>
406
+ `;
407
+ messageArea.appendChild(item);
408
+ return item;
409
+ }
410
+
411
+ function scrollBottom() {
412
+ messageArea.scrollTop = messageArea.scrollHeight;
413
+ }
414
+
415
+ micA.onclick = () => startListening('a');
416
+ micB.onclick = () => startListening('b');
417
+
418
+ document.getElementById('startBtn').onclick = () => {
419
+ langA = langASel.value;
420
+ langB = langBSel.value;
421
+ if (langA === langB) return alert('请选择不同语言');
422
+ document.getElementById('startScreen').classList.remove('active');
423
+ document.getElementById('callScreen').classList.add('active');
424
+ messageArea.innerHTML = '';
425
+ collectedText = '';
426
+ recordingRole = null;
427
+ isPlaying = false;
428
+ statusBar.textContent = '点击麦克风开始,说完再点一次结束';
429
+ log('进入通话界面');
430
+ refreshUI();
431
+ };
432
+
433
+ document.getElementById('clearBtn').onclick = () => {
434
+ messageArea.innerHTML = '';
435
+ currentItem = null;
436
+ };
437
+
438
+ document.getElementById('exitBtn').onclick = () => {
439
+ forceStop();
440
+ speechSynthesis.cancel();
441
+ document.getElementById('callScreen').classList.remove('active');
442
+ document.getElementById('startScreen').classList.add('active');
443
+ };
444
+
445
+ log('页面加载完成 - 请用新标签页打开');
446
+ </script>
447
+ </body>
448
+ </html>