Taher Poonawala commited on
Commit
93e6599
Β·
1 Parent(s): c09a4e0

Fix CORS: serve live GUI from Space at /live route, auto-detect API URL

Browse files
Files changed (3) hide show
  1. Dockerfile +1 -1
  2. api_backend.py +10 -0
  3. live_translation_gui.html +1271 -0
Dockerfile CHANGED
@@ -22,7 +22,7 @@ RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \
22
  # api_backend.py downloads indic-seamless on first startup using the token.
23
  # With HF Persistent Storage mounted at /app/model_cache, this only happens once.
24
 
25
- COPY api_backend.py translation_module.py index.html ./
26
 
27
  RUN mkdir -p uploads outputs model_cache logs
28
 
 
22
  # api_backend.py downloads indic-seamless on first startup using the token.
23
  # With HF Persistent Storage mounted at /app/model_cache, this only happens once.
24
 
25
+ COPY api_backend.py translation_module.py index.html live_translation_gui.html ./
26
 
27
  RUN mkdir -p uploads outputs model_cache logs
28
 
api_backend.py CHANGED
@@ -391,6 +391,16 @@ async def root():
391
  )
392
 
393
 
 
 
 
 
 
 
 
 
 
 
394
  @app.get("/health")
395
  async def health():
396
  return {
 
391
  )
392
 
393
 
394
+ @app.get("/live", response_class=HTMLResponse)
395
+ async def live_ui():
396
+ """Serve the live translation GUI"""
397
+ path = Path("live_translation_gui.html")
398
+ if path.exists():
399
+ with open(path, "r", encoding="utf-8") as f:
400
+ return f.read()
401
+ return HTMLResponse("<h1>Live UI not found</h1><p>Add live_translation_gui.html to the app directory.</p>")
402
+
403
+
404
  @app.get("/health")
405
  async def health():
406
  return {
live_translation_gui.html ADDED
@@ -0,0 +1,1271 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Vāk β€” Live Speech Translation</title>
7
+ <link href="https://fonts.googleapis.com/css2?family=Syne:wght@400;500;600;700;800&family=JetBrains+Mono:wght@300;400;500&display=swap" rel="stylesheet">
8
+ <style>
9
+ :root {
10
+ --bg: #0a0a0f;
11
+ --surface: #111118;
12
+ --surface2: #1a1a24;
13
+ --border: #2a2a3a;
14
+ --accent: #7c6aff;
15
+ --accent2: #ff6a9e;
16
+ --accent3: #6affd4;
17
+ --text: #e8e8f0;
18
+ --text-dim: #6b6b8a;
19
+ --text-dimmer: #3a3a55;
20
+ --recording: #ff4466;
21
+ --success: #44ffaa;
22
+ --warn: #ffaa44;
23
+ --radius: 16px;
24
+ --radius-sm: 8px;
25
+ }
26
+
27
+ * { margin: 0; padding: 0; box-sizing: border-box; }
28
+
29
+ body {
30
+ font-family: 'Syne', sans-serif;
31
+ background: var(--bg);
32
+ color: var(--text);
33
+ min-height: 100vh;
34
+ overflow-x: hidden;
35
+ }
36
+
37
+ /* Background grid */
38
+ body::before {
39
+ content: '';
40
+ position: fixed;
41
+ inset: 0;
42
+ background-image:
43
+ linear-gradient(rgba(124,106,255,0.03) 1px, transparent 1px),
44
+ linear-gradient(90deg, rgba(124,106,255,0.03) 1px, transparent 1px);
45
+ background-size: 40px 40px;
46
+ pointer-events: none;
47
+ z-index: 0;
48
+ }
49
+
50
+ .app {
51
+ position: relative;
52
+ z-index: 1;
53
+ max-width: 960px;
54
+ margin: 0 auto;
55
+ padding: 32px 24px;
56
+ }
57
+
58
+ /* Header */
59
+ header {
60
+ display: flex;
61
+ align-items: center;
62
+ justify-content: space-between;
63
+ margin-bottom: 48px;
64
+ }
65
+
66
+ .logo {
67
+ display: flex;
68
+ align-items: baseline;
69
+ gap: 10px;
70
+ }
71
+
72
+ .logo-text {
73
+ font-size: 28px;
74
+ font-weight: 800;
75
+ letter-spacing: -1px;
76
+ background: linear-gradient(135deg, var(--accent), var(--accent2));
77
+ -webkit-background-clip: text;
78
+ -webkit-text-fill-color: transparent;
79
+ }
80
+
81
+ .logo-sub {
82
+ font-family: 'JetBrains Mono', monospace;
83
+ font-size: 11px;
84
+ color: var(--text-dim);
85
+ letter-spacing: 2px;
86
+ text-transform: uppercase;
87
+ }
88
+
89
+ .api-config {
90
+ display: flex;
91
+ align-items: center;
92
+ gap: 8px;
93
+ background: var(--surface);
94
+ border: 1px solid var(--border);
95
+ border-radius: 100px;
96
+ padding: 6px 6px 6px 16px;
97
+ }
98
+
99
+ .api-config input {
100
+ background: none;
101
+ border: none;
102
+ outline: none;
103
+ font-family: 'JetBrains Mono', monospace;
104
+ font-size: 12px;
105
+ color: var(--text-dim);
106
+ width: 200px;
107
+ }
108
+
109
+ .api-config input::placeholder { color: var(--text-dimmer); }
110
+
111
+ .connect-btn {
112
+ background: var(--surface2);
113
+ border: 1px solid var(--border);
114
+ border-radius: 100px;
115
+ padding: 6px 14px;
116
+ color: var(--text);
117
+ font-family: 'Syne', sans-serif;
118
+ font-size: 12px;
119
+ font-weight: 600;
120
+ cursor: pointer;
121
+ transition: all 0.2s;
122
+ white-space: nowrap;
123
+ }
124
+
125
+ .connect-btn:hover { border-color: var(--accent); color: var(--accent); }
126
+
127
+ .status-dot {
128
+ width: 8px;
129
+ height: 8px;
130
+ border-radius: 50%;
131
+ background: var(--text-dimmer);
132
+ transition: background 0.3s;
133
+ flex-shrink: 0;
134
+ }
135
+
136
+ .status-dot.connected { background: var(--success); box-shadow: 0 0 8px var(--success); }
137
+ .status-dot.error { background: var(--recording); }
138
+
139
+ /* Main grid */
140
+ .main-grid {
141
+ display: grid;
142
+ grid-template-columns: 280px 1fr;
143
+ gap: 16px;
144
+ align-items: start;
145
+ }
146
+
147
+ /* Controls panel */
148
+ .controls-panel {
149
+ display: flex;
150
+ flex-direction: column;
151
+ gap: 12px;
152
+ }
153
+
154
+ .panel {
155
+ background: var(--surface);
156
+ border: 1px solid var(--border);
157
+ border-radius: var(--radius);
158
+ padding: 20px;
159
+ }
160
+
161
+ .panel-label {
162
+ font-family: 'JetBrains Mono', monospace;
163
+ font-size: 10px;
164
+ letter-spacing: 2px;
165
+ text-transform: uppercase;
166
+ color: var(--text-dim);
167
+ margin-bottom: 14px;
168
+ }
169
+
170
+ /* Language selector */
171
+ .lang-grid {
172
+ display: grid;
173
+ grid-template-columns: 1fr 1fr;
174
+ gap: 6px;
175
+ max-height: 240px;
176
+ overflow-y: auto;
177
+ scrollbar-width: thin;
178
+ scrollbar-color: var(--border) transparent;
179
+ }
180
+
181
+ .lang-btn {
182
+ background: var(--surface2);
183
+ border: 1px solid var(--border);
184
+ border-radius: var(--radius-sm);
185
+ padding: 8px 10px;
186
+ color: var(--text-dim);
187
+ font-family: 'Syne', sans-serif;
188
+ font-size: 12px;
189
+ font-weight: 500;
190
+ cursor: pointer;
191
+ transition: all 0.15s;
192
+ text-align: left;
193
+ white-space: nowrap;
194
+ overflow: hidden;
195
+ text-overflow: ellipsis;
196
+ }
197
+
198
+ .lang-btn:hover { border-color: var(--accent); color: var(--text); }
199
+ .lang-btn.active {
200
+ background: rgba(124,106,255,0.15);
201
+ border-color: var(--accent);
202
+ color: var(--accent);
203
+ }
204
+
205
+ /* Settings */
206
+ .setting-row {
207
+ display: flex;
208
+ align-items: center;
209
+ justify-content: space-between;
210
+ margin-bottom: 14px;
211
+ }
212
+
213
+ .setting-row:last-child { margin-bottom: 0; }
214
+
215
+ .setting-label {
216
+ font-size: 13px;
217
+ color: var(--text-dim);
218
+ }
219
+
220
+ .setting-value {
221
+ font-family: 'JetBrains Mono', monospace;
222
+ font-size: 13px;
223
+ color: var(--text);
224
+ font-weight: 500;
225
+ }
226
+
227
+ input[type="range"] {
228
+ -webkit-appearance: none;
229
+ width: 90px;
230
+ height: 3px;
231
+ background: var(--border);
232
+ border-radius: 2px;
233
+ outline: none;
234
+ }
235
+
236
+ input[type="range"]::-webkit-slider-thumb {
237
+ -webkit-appearance: none;
238
+ width: 14px;
239
+ height: 14px;
240
+ border-radius: 50%;
241
+ background: var(--accent);
242
+ cursor: pointer;
243
+ box-shadow: 0 0 8px rgba(124,106,255,0.5);
244
+ }
245
+
246
+ /* Model selector */
247
+ .model-select {
248
+ width: 100%;
249
+ background: var(--surface2);
250
+ border: 1px solid var(--border);
251
+ border-radius: var(--radius-sm);
252
+ padding: 8px 12px;
253
+ color: var(--text);
254
+ font-family: 'Syne', sans-serif;
255
+ font-size: 13px;
256
+ outline: none;
257
+ cursor: pointer;
258
+ appearance: none;
259
+ background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%236b6b8a' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
260
+ background-repeat: no-repeat;
261
+ background-position: right 12px center;
262
+ }
263
+
264
+ /* Main record button */
265
+ .record-section {
266
+ display: flex;
267
+ flex-direction: column;
268
+ align-items: center;
269
+ gap: 12px;
270
+ padding: 24px 20px;
271
+ }
272
+
273
+ .record-btn {
274
+ width: 80px;
275
+ height: 80px;
276
+ border-radius: 50%;
277
+ border: none;
278
+ cursor: pointer;
279
+ position: relative;
280
+ background: linear-gradient(135deg, var(--accent), var(--accent2));
281
+ box-shadow: 0 0 0 0 rgba(124,106,255,0.4);
282
+ transition: transform 0.2s, box-shadow 0.2s;
283
+ display: flex;
284
+ align-items: center;
285
+ justify-content: center;
286
+ }
287
+
288
+ .record-btn:hover { transform: scale(1.05); }
289
+ .record-btn:active { transform: scale(0.97); }
290
+
291
+ .record-btn.recording {
292
+ background: linear-gradient(135deg, var(--recording), #ff2255);
293
+ animation: pulse-ring 1.5s ease-out infinite;
294
+ }
295
+
296
+ @keyframes pulse-ring {
297
+ 0% { box-shadow: 0 0 0 0 rgba(255,68,102,0.5); }
298
+ 70% { box-shadow: 0 0 0 20px rgba(255,68,102,0); }
299
+ 100% { box-shadow: 0 0 0 0 rgba(255,68,102,0); }
300
+ }
301
+
302
+ .record-icon {
303
+ width: 24px;
304
+ height: 24px;
305
+ border-radius: 50%;
306
+ background: white;
307
+ transition: all 0.2s;
308
+ }
309
+
310
+ .record-btn.recording .record-icon {
311
+ width: 20px;
312
+ height: 20px;
313
+ border-radius: 4px;
314
+ }
315
+
316
+ .record-status {
317
+ font-family: 'JetBrains Mono', monospace;
318
+ font-size: 12px;
319
+ color: var(--text-dim);
320
+ letter-spacing: 1px;
321
+ text-transform: uppercase;
322
+ }
323
+
324
+ .record-status.recording { color: var(--recording); }
325
+
326
+ /* Cost tracker */
327
+ .cost-bar {
328
+ width: 100%;
329
+ display: flex;
330
+ align-items: center;
331
+ justify-content: space-between;
332
+ gap: 8px;
333
+ }
334
+
335
+ .cost-track {
336
+ flex: 1;
337
+ height: 3px;
338
+ background: var(--border);
339
+ border-radius: 2px;
340
+ overflow: hidden;
341
+ }
342
+
343
+ .cost-fill {
344
+ height: 100%;
345
+ background: linear-gradient(90deg, var(--success), var(--warn), var(--recording));
346
+ border-radius: 2px;
347
+ width: 0%;
348
+ transition: width 1s linear;
349
+ }
350
+
351
+ .cost-label {
352
+ font-family: 'JetBrains Mono', monospace;
353
+ font-size: 11px;
354
+ color: var(--text-dim);
355
+ white-space: nowrap;
356
+ }
357
+
358
+ /* Output panel */
359
+ .output-panel {
360
+ display: flex;
361
+ flex-direction: column;
362
+ gap: 16px;
363
+ }
364
+
365
+ /* Waveform */
366
+ .waveform-container {
367
+ background: var(--surface);
368
+ border: 1px solid var(--border);
369
+ border-radius: var(--radius);
370
+ padding: 16px 20px;
371
+ position: relative;
372
+ overflow: hidden;
373
+ }
374
+
375
+ .waveform-label {
376
+ font-family: 'JetBrains Mono', monospace;
377
+ font-size: 10px;
378
+ letter-spacing: 2px;
379
+ text-transform: uppercase;
380
+ color: var(--text-dim);
381
+ margin-bottom: 10px;
382
+ display: flex;
383
+ align-items: center;
384
+ gap: 8px;
385
+ }
386
+
387
+ .waveform-live {
388
+ width: 6px;
389
+ height: 6px;
390
+ border-radius: 50%;
391
+ background: var(--recording);
392
+ opacity: 0;
393
+ transition: opacity 0.3s;
394
+ }
395
+
396
+ .waveform-live.active { opacity: 1; animation: blink 1s ease-in-out infinite; }
397
+
398
+ @keyframes blink {
399
+ 0%, 100% { opacity: 1; }
400
+ 50% { opacity: 0.3; }
401
+ }
402
+
403
+ canvas#waveform {
404
+ width: 100%;
405
+ height: 48px;
406
+ display: block;
407
+ }
408
+
409
+ /* Translations feed */
410
+ .feed {
411
+ background: var(--surface);
412
+ border: 1px solid var(--border);
413
+ border-radius: var(--radius);
414
+ overflow: hidden;
415
+ display: flex;
416
+ flex-direction: column;
417
+ min-height: 360px;
418
+ }
419
+
420
+ .feed-header {
421
+ display: flex;
422
+ align-items: center;
423
+ justify-content: space-between;
424
+ padding: 16px 20px;
425
+ border-bottom: 1px solid var(--border);
426
+ }
427
+
428
+ .feed-title {
429
+ font-family: 'JetBrains Mono', monospace;
430
+ font-size: 10px;
431
+ letter-spacing: 2px;
432
+ text-transform: uppercase;
433
+ color: var(--text-dim);
434
+ }
435
+
436
+ .clear-btn {
437
+ background: none;
438
+ border: 1px solid var(--border);
439
+ border-radius: 100px;
440
+ padding: 4px 12px;
441
+ color: var(--text-dim);
442
+ font-family: 'Syne', sans-serif;
443
+ font-size: 11px;
444
+ cursor: pointer;
445
+ transition: all 0.2s;
446
+ }
447
+
448
+ .clear-btn:hover { border-color: var(--recording); color: var(--recording); }
449
+
450
+ .feed-body {
451
+ flex: 1;
452
+ overflow-y: auto;
453
+ padding: 16px;
454
+ display: flex;
455
+ flex-direction: column;
456
+ gap: 12px;
457
+ scrollbar-width: thin;
458
+ scrollbar-color: var(--border) transparent;
459
+ }
460
+
461
+ .feed-empty {
462
+ flex: 1;
463
+ display: flex;
464
+ flex-direction: column;
465
+ align-items: center;
466
+ justify-content: center;
467
+ gap: 12px;
468
+ color: var(--text-dimmer);
469
+ text-align: center;
470
+ padding: 40px;
471
+ }
472
+
473
+ .feed-empty-icon {
474
+ font-size: 40px;
475
+ opacity: 0.4;
476
+ }
477
+
478
+ .feed-empty p {
479
+ font-size: 14px;
480
+ line-height: 1.6;
481
+ }
482
+
483
+ /* Translation card */
484
+ .translation-card {
485
+ background: var(--surface2);
486
+ border: 1px solid var(--border);
487
+ border-radius: var(--radius-sm);
488
+ padding: 14px 16px;
489
+ animation: slideIn 0.3s ease-out;
490
+ position: relative;
491
+ overflow: hidden;
492
+ }
493
+
494
+ .translation-card::before {
495
+ content: '';
496
+ position: absolute;
497
+ left: 0;
498
+ top: 0;
499
+ bottom: 0;
500
+ width: 3px;
501
+ background: linear-gradient(180deg, var(--accent), var(--accent2));
502
+ border-radius: 0 2px 2px 0;
503
+ }
504
+
505
+ .translation-card.processing::before {
506
+ background: var(--warn);
507
+ animation: shimmer 1s ease-in-out infinite;
508
+ }
509
+
510
+ @keyframes shimmer {
511
+ 0%, 100% { opacity: 1; }
512
+ 50% { opacity: 0.3; }
513
+ }
514
+
515
+ @keyframes slideIn {
516
+ from { opacity: 0; transform: translateY(8px); }
517
+ to { opacity: 1; transform: translateY(0); }
518
+ }
519
+
520
+ .card-meta {
521
+ display: flex;
522
+ align-items: center;
523
+ justify-content: space-between;
524
+ margin-bottom: 10px;
525
+ }
526
+
527
+ .card-lang {
528
+ display: flex;
529
+ align-items: center;
530
+ gap: 6px;
531
+ }
532
+
533
+ .lang-tag {
534
+ font-family: 'JetBrains Mono', monospace;
535
+ font-size: 10px;
536
+ padding: 2px 8px;
537
+ border-radius: 100px;
538
+ font-weight: 500;
539
+ letter-spacing: 0.5px;
540
+ }
541
+
542
+ .lang-tag.source {
543
+ background: rgba(106,255,212,0.1);
544
+ color: var(--accent3);
545
+ border: 1px solid rgba(106,255,212,0.2);
546
+ }
547
+
548
+ .lang-tag.target {
549
+ background: rgba(124,106,255,0.1);
550
+ color: var(--accent);
551
+ border: 1px solid rgba(124,106,255,0.2);
552
+ }
553
+
554
+ .card-arrow {
555
+ color: var(--text-dimmer);
556
+ font-size: 12px;
557
+ }
558
+
559
+ .card-time {
560
+ font-family: 'JetBrains Mono', monospace;
561
+ font-size: 10px;
562
+ color: var(--text-dimmer);
563
+ }
564
+
565
+ .card-original {
566
+ font-size: 13px;
567
+ color: var(--text-dim);
568
+ margin-bottom: 8px;
569
+ line-height: 1.5;
570
+ font-style: italic;
571
+ }
572
+
573
+ .card-translation {
574
+ font-size: 15px;
575
+ color: var(--text);
576
+ font-weight: 500;
577
+ line-height: 1.5;
578
+ }
579
+
580
+ .card-processing {
581
+ font-size: 13px;
582
+ color: var(--warn);
583
+ font-family: 'JetBrains Mono', monospace;
584
+ }
585
+
586
+ .card-audio {
587
+ margin-top: 10px;
588
+ display: flex;
589
+ align-items: center;
590
+ gap: 8px;
591
+ }
592
+
593
+ .play-btn {
594
+ background: rgba(124,106,255,0.1);
595
+ border: 1px solid rgba(124,106,255,0.3);
596
+ border-radius: 100px;
597
+ padding: 4px 12px;
598
+ color: var(--accent);
599
+ font-family: 'Syne', sans-serif;
600
+ font-size: 11px;
601
+ font-weight: 600;
602
+ cursor: pointer;
603
+ transition: all 0.2s;
604
+ display: flex;
605
+ align-items: center;
606
+ gap: 5px;
607
+ }
608
+
609
+ .play-btn:hover { background: rgba(124,106,255,0.2); }
610
+
611
+ /* Stats bar */
612
+ .stats-bar {
613
+ display: grid;
614
+ grid-template-columns: repeat(4, 1fr);
615
+ gap: 10px;
616
+ }
617
+
618
+ .stat-card {
619
+ background: var(--surface);
620
+ border: 1px solid var(--border);
621
+ border-radius: var(--radius-sm);
622
+ padding: 12px 16px;
623
+ text-align: center;
624
+ }
625
+
626
+ .stat-value {
627
+ font-family: 'JetBrains Mono', monospace;
628
+ font-size: 20px;
629
+ font-weight: 500;
630
+ color: var(--text);
631
+ line-height: 1;
632
+ margin-bottom: 4px;
633
+ }
634
+
635
+ .stat-label {
636
+ font-size: 11px;
637
+ color: var(--text-dim);
638
+ }
639
+
640
+ /* Device selector */
641
+ .device-select {
642
+ width: 100%;
643
+ background: var(--surface2);
644
+ border: 1px solid var(--border);
645
+ border-radius: var(--radius-sm);
646
+ padding: 8px 12px;
647
+ color: var(--text);
648
+ font-family: 'Syne', sans-serif;
649
+ font-size: 12px;
650
+ outline: none;
651
+ cursor: pointer;
652
+ appearance: none;
653
+ background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%236b6b8a' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
654
+ background-repeat: no-repeat;
655
+ background-position: right 12px center;
656
+ margin-bottom: 8px;
657
+ }
658
+
659
+ /* Toast */
660
+ .toast {
661
+ position: fixed;
662
+ bottom: 24px;
663
+ right: 24px;
664
+ background: var(--surface2);
665
+ border: 1px solid var(--border);
666
+ border-radius: var(--radius-sm);
667
+ padding: 12px 18px;
668
+ font-size: 13px;
669
+ color: var(--text);
670
+ z-index: 100;
671
+ opacity: 0;
672
+ transform: translateY(8px);
673
+ transition: all 0.3s;
674
+ max-width: 320px;
675
+ font-family: 'JetBrains Mono', monospace;
676
+ }
677
+
678
+ .toast.show { opacity: 1; transform: translateY(0); }
679
+ .toast.error { border-color: var(--recording); color: var(--recording); }
680
+ .toast.success { border-color: var(--success); color: var(--success); }
681
+
682
+ @media (max-width: 720px) {
683
+ .main-grid { grid-template-columns: 1fr; }
684
+ .stats-bar { grid-template-columns: repeat(2, 1fr); }
685
+ .api-config input { width: 130px; }
686
+ }
687
+ </style>
688
+ </head>
689
+ <body>
690
+ <div class="app">
691
+
692
+ <header>
693
+ <div class="logo">
694
+ <span class="logo-text">Vāk</span>
695
+ <span class="logo-sub">Live Translation</span>
696
+ </div>
697
+ <div class="api-config">
698
+ <div class="status-dot" id="statusDot"></div>
699
+ <input type="text" id="apiUrl" placeholder="API endpoint URL" value="http://localhost:7860">
700
+ <button class="connect-btn" onclick="testConnection()">Connect</button>
701
+ </div>
702
+ </header>
703
+
704
+ <div class="main-grid">
705
+
706
+ <!-- Left: Controls -->
707
+ <div class="controls-panel">
708
+
709
+ <!-- Record -->
710
+ <div class="panel">
711
+ <div class="panel-label">Microphone</div>
712
+ <select class="device-select" id="deviceSelect">
713
+ <option value="">Default microphone</option>
714
+ </select>
715
+ <div class="record-section">
716
+ <button class="record-btn" id="recordBtn" onclick="toggleRecording()">
717
+ <div class="record-icon"></div>
718
+ </button>
719
+ <div class="record-status" id="recordStatus">READY</div>
720
+ </div>
721
+ <div class="cost-bar">
722
+ <span class="cost-label" id="costLabel">$0.000</span>
723
+ <div class="cost-track">
724
+ <div class="cost-fill" id="costFill"></div>
725
+ </div>
726
+ <span class="cost-label" id="sessionTime">0:00</span>
727
+ </div>
728
+ </div>
729
+
730
+ <!-- Target Language -->
731
+ <div class="panel">
732
+ <div class="panel-label">Translate to</div>
733
+ <div class="lang-grid" id="langGrid">
734
+ <!-- populated by JS -->
735
+ </div>
736
+ </div>
737
+
738
+ <!-- Settings -->
739
+ <div class="panel">
740
+ <div class="panel-label">Settings</div>
741
+ <div class="setting-row">
742
+ <span class="setting-label">Chunk size</span>
743
+ <div style="display:flex;align-items:center;gap:8px">
744
+ <input type="range" id="chunkSlider" min="2" max="8" value="3" step="1" oninput="updateChunk(this.value)">
745
+ <span class="setting-value" id="chunkVal">3s</span>
746
+ </div>
747
+ </div>
748
+ <div class="setting-row">
749
+ <span class="setting-label">Auto-play audio</span>
750
+ <label style="cursor:pointer;display:flex;align-items:center;gap:6px">
751
+ <input type="checkbox" id="autoPlay" checked style="accent-color:var(--accent);width:14px;height:14px">
752
+ <span class="setting-value" style="font-size:11px">ON</span>
753
+ </label>
754
+ </div>
755
+ <div class="setting-row">
756
+ <span class="setting-label">Cost limit</span>
757
+ <div style="display:flex;align-items:center;gap:4px">
758
+ <span style="font-family:'JetBrains Mono',monospace;font-size:12px;color:var(--text-dim)">$</span>
759
+ <input type="number" id="costLimit" value="0.50" min="0.10" max="5.00" step="0.10"
760
+ style="width:54px;background:var(--surface2);border:1px solid var(--border);border-radius:6px;padding:4px 8px;color:var(--text);font-family:'JetBrains Mono',monospace;font-size:12px;outline:none;text-align:right">
761
+ </div>
762
+ </div>
763
+ </div>
764
+
765
+ </div>
766
+
767
+ <!-- Right: Output -->
768
+ <div class="output-panel">
769
+
770
+ <!-- Stats -->
771
+ <div class="stats-bar">
772
+ <div class="stat-card">
773
+ <div class="stat-value" id="statChunks">0</div>
774
+ <div class="stat-label">Chunks sent</div>
775
+ </div>
776
+ <div class="stat-card">
777
+ <div class="stat-value" id="statDone">0</div>
778
+ <div class="stat-label">Completed</div>
779
+ </div>
780
+ <div class="stat-card">
781
+ <div class="stat-value" id="statAvgTime">β€”</div>
782
+ <div class="stat-label">Avg latency</div>
783
+ </div>
784
+ <div class="stat-card">
785
+ <div class="stat-value" id="statDetected">β€”</div>
786
+ <div class="stat-label">Detected lang</div>
787
+ </div>
788
+ </div>
789
+
790
+ <!-- Waveform -->
791
+ <div class="waveform-container">
792
+ <div class="waveform-label">
793
+ <div class="waveform-live" id="waveLive"></div>
794
+ Input signal
795
+ </div>
796
+ <canvas id="waveform" height="48"></canvas>
797
+ </div>
798
+
799
+ <!-- Feed -->
800
+ <div class="feed">
801
+ <div class="feed-header">
802
+ <span class="feed-title">Translation feed</span>
803
+ <button class="clear-btn" onclick="clearFeed()">Clear</button>
804
+ </div>
805
+ <div class="feed-body" id="feedBody">
806
+ <div class="feed-empty" id="feedEmpty">
807
+ <div class="feed-empty-icon">πŸŽ™</div>
808
+ <p>Connect to your API endpoint,<br>select a target language,<br>then press record.</p>
809
+ </div>
810
+ </div>
811
+ </div>
812
+
813
+ </div>
814
+ </div>
815
+ </div>
816
+
817
+ <div class="toast" id="toast"></div>
818
+
819
+ <script>
820
+ // ─── State ────────────────────────────────────────────────────────────────────
821
+ const state = {
822
+ connected: false,
823
+ recording: false,
824
+ targetLang: 'English',
825
+ chunkDuration: 3,
826
+ apiUrl: 'http://localhost:7860',
827
+ mediaStream: null,
828
+ mediaRecorder: null,
829
+ recordingInterval: null,
830
+ sessionStart: null,
831
+ sessionTimer: null,
832
+ totalCost: 0,
833
+ chunksCount: 0,
834
+ completedCount: 0,
835
+ totalLatency: 0,
836
+ lastDetectedLang: 'β€”',
837
+ pendingJobs: new Map(),
838
+ audioCtx: null,
839
+ analyser: null,
840
+ animFrame: null,
841
+ };
842
+
843
+ const COST_PER_HOUR = 0.60; // T4 medium
844
+ // Exactly the languages supported by indic-seamless
845
+ // 14 Indian languages + 8 international targets
846
+ const LANGUAGES = [
847
+ // Indian (indic-seamless native)
848
+ 'Assamese','Bengali','Gujarati','Hindi','Kannada',
849
+ 'Malayalam','Marathi','Nepali','Odia','Punjabi',
850
+ 'Sindhi','Tamil','Telugu','Urdu',
851
+ // International targets
852
+ 'English','Arabic','French','German',
853
+ 'Japanese','Korean','Portuguese','Spanish',
854
+ ];
855
+
856
+ const INDIAN_LANGS = new Set([
857
+ 'Assamese','Bengali','Gujarati','Hindi','Kannada',
858
+ 'Malayalam','Marathi','Nepali','Odia','Punjabi',
859
+ 'Sindhi','Tamil','Telugu','Urdu',
860
+ ]);
861
+
862
+ // ─── Init ─────────────────────────────────────────────────────────────────────
863
+ function init() {
864
+ buildLangGrid();
865
+ loadApiUrl();
866
+ populateDevices();
867
+ }
868
+
869
+ function buildLangGrid() {
870
+ const grid = document.getElementById('langGrid');
871
+ grid.innerHTML = '';
872
+ // Add section label for Indian languages
873
+ const indiaLabel = document.createElement('div');
874
+ indiaLabel.style.cssText = 'grid-column:1/-1;font-family:"JetBrains Mono",monospace;font-size:9px;letter-spacing:2px;text-transform:uppercase;color:var(--text-dimmer);padding:4px 0 2px';
875
+ indiaLabel.textContent = 'Indian languages';
876
+ grid.appendChild(indiaLabel);
877
+
878
+ LANGUAGES.forEach(lang => {
879
+ // Add separator before international section
880
+ if (lang === 'English') {
881
+ const intlLabel = document.createElement('div');
882
+ intlLabel.style.cssText = 'grid-column:1/-1;font-family:"JetBrains Mono",monospace;font-size:9px;letter-spacing:2px;text-transform:uppercase;color:var(--text-dimmer);padding:8px 0 2px;border-top:1px solid var(--border);margin-top:4px';
883
+ intlLabel.textContent = 'International';
884
+ grid.appendChild(intlLabel);
885
+ }
886
+ const btn = document.createElement('button');
887
+ btn.className = 'lang-btn' + (lang === state.targetLang ? ' active' : '');
888
+ btn.textContent = lang;
889
+ btn.onclick = () => selectLang(lang);
890
+ grid.appendChild(btn);
891
+ });
892
+ }
893
+
894
+ function selectLang(lang) {
895
+ state.targetLang = lang;
896
+ document.querySelectorAll('.lang-btn').forEach(b => {
897
+ b.classList.toggle('active', b.textContent === lang);
898
+ });
899
+ }
900
+
901
+ function loadApiUrl() {
902
+ document.getElementById('apiUrl').value = state.apiUrl;
903
+ }
904
+
905
+ async function populateDevices() {
906
+ try {
907
+ await navigator.mediaDevices.getUserMedia({ audio: true });
908
+ const devices = await navigator.mediaDevices.enumerateDevices();
909
+ const sel = document.getElementById('deviceSelect');
910
+ devices.filter(d => d.kind === 'audioinput').forEach((d, i) => {
911
+ const opt = document.createElement('option');
912
+ opt.value = d.deviceId;
913
+ opt.textContent = d.label || `Microphone ${i + 1}`;
914
+ sel.appendChild(opt);
915
+ });
916
+ } catch(e) {}
917
+ }
918
+
919
+ // ─── Connection ───────────────────────────────────────────────────────────────
920
+ async function testConnection() {
921
+ const url = document.getElementById('apiUrl').value.trim().replace(/\/$/, '');
922
+ state.apiUrl = url;
923
+ localStorage.setItem('vak_api_url', url);
924
+
925
+ const dot = document.getElementById('statusDot');
926
+ dot.className = 'status-dot';
927
+ showToast('Connecting...', '');
928
+
929
+ try {
930
+ const res = await fetch(`${url}/health`, { signal: AbortSignal.timeout(5000) });
931
+ if (res.ok) {
932
+ const data = await res.json();
933
+ state.connected = true;
934
+ dot.className = 'status-dot connected';
935
+ showToast(`Connected β€” ${data.device || 'ready'}`, 'success');
936
+ // Fetch supported languages from API
937
+ const langRes = await fetch(`${url}/languages`);
938
+ if (langRes.ok) {
939
+ const langData = await langRes.json();
940
+ // Update if API returns different list
941
+ }
942
+ } else {
943
+ throw new Error(`HTTP ${res.status}`);
944
+ }
945
+ } catch(e) {
946
+ state.connected = false;
947
+ dot.className = 'status-dot error';
948
+ showToast(`Connection failed: ${e.message}`, 'error');
949
+ }
950
+ }
951
+
952
+ // ─── Recording ────────────────────────────────────────────────────────────────
953
+ async function toggleRecording() {
954
+ if (state.recording) {
955
+ stopRecording();
956
+ } else {
957
+ await startRecording();
958
+ }
959
+ }
960
+
961
+ async function startRecording() {
962
+ if (!state.connected) {
963
+ showToast('Connect to API first', 'error');
964
+ return;
965
+ }
966
+
967
+ try {
968
+ const deviceId = document.getElementById('deviceSelect').value;
969
+ const constraints = { audio: deviceId ? { deviceId: { exact: deviceId } } : true };
970
+ state.mediaStream = await navigator.mediaDevices.getUserMedia(constraints);
971
+ } catch(e) {
972
+ showToast(`Microphone error: ${e.message}`, 'error');
973
+ return;
974
+ }
975
+
976
+ state.recording = true;
977
+ state.sessionStart = Date.now();
978
+ document.getElementById('recordBtn').classList.add('recording');
979
+ document.getElementById('recordStatus').textContent = 'RECORDING';
980
+ document.getElementById('recordStatus').classList.add('recording');
981
+ document.getElementById('waveLive').classList.add('active');
982
+ document.getElementById('feedEmpty').style.display = 'none';
983
+
984
+ startWaveform();
985
+ startSessionTimer();
986
+ scheduleChunk();
987
+ }
988
+
989
+ function stopRecording() {
990
+ state.recording = false;
991
+ if (state.mediaStream) state.mediaStream.getTracks().forEach(t => t.stop());
992
+ if (state.recordingInterval) clearTimeout(state.recordingInterval);
993
+ if (state.sessionTimer) clearInterval(state.sessionTimer);
994
+ if (state.animFrame) cancelAnimationFrame(state.animFrame);
995
+ if (state.audioCtx) { state.audioCtx.close(); state.audioCtx = null; }
996
+
997
+ document.getElementById('recordBtn').classList.remove('recording');
998
+ document.getElementById('recordStatus').textContent = 'STOPPED';
999
+ document.getElementById('recordStatus').classList.remove('recording');
1000
+ document.getElementById('waveLive').classList.remove('active');
1001
+
1002
+ // Clear waveform
1003
+ const canvas = document.getElementById('waveform');
1004
+ const ctx = canvas.getContext('2d');
1005
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
1006
+ }
1007
+
1008
+ function scheduleChunk() {
1009
+ if (!state.recording) return;
1010
+ recordChunk().then(() => {
1011
+ state.recordingInterval = setTimeout(scheduleChunk, state.chunkDuration * 1000);
1012
+ });
1013
+ }
1014
+
1015
+ async function recordChunk() {
1016
+ if (!state.recording || !state.mediaStream) return;
1017
+
1018
+ return new Promise(resolve => {
1019
+ const chunks = [];
1020
+ const recorder = new MediaRecorder(state.mediaStream, {
1021
+ mimeType: getSupportedMimeType()
1022
+ });
1023
+
1024
+ recorder.ondataavailable = e => { if (e.data.size > 0) chunks.push(e.data); };
1025
+ recorder.onstop = async () => {
1026
+ const blob = new Blob(chunks, { type: recorder.mimeType });
1027
+ if (blob.size < 1000) { resolve(); return; } // Skip tiny/silent chunks
1028
+ await sendChunk(blob);
1029
+ resolve();
1030
+ };
1031
+
1032
+ recorder.start();
1033
+ setTimeout(() => { if (recorder.state !== 'inactive') recorder.stop(); },
1034
+ state.chunkDuration * 1000);
1035
+ });
1036
+ }
1037
+
1038
+ function getSupportedMimeType() {
1039
+ const types = ['audio/webm;codecs=opus', 'audio/webm', 'audio/ogg', 'audio/mp4'];
1040
+ return types.find(t => MediaRecorder.isTypeSupported(t)) || '';
1041
+ }
1042
+
1043
+ async function sendChunk(blob) {
1044
+ const cardId = 'card_' + Date.now();
1045
+ const startTime = Date.now();
1046
+ state.chunksCount++;
1047
+ updateStats();
1048
+ addProcessingCard(cardId);
1049
+
1050
+ const formData = new FormData();
1051
+ formData.append('audio_file', blob, 'chunk.webm');
1052
+ formData.append('target_language', state.targetLang);
1053
+
1054
+ try {
1055
+ const res = await fetch(`${state.apiUrl}/translate`, {
1056
+ method: 'POST',
1057
+ body: formData,
1058
+ });
1059
+
1060
+ if (!res.ok) throw new Error(`API error ${res.status}`);
1061
+ const { job_id } = await res.json();
1062
+
1063
+ // Poll for result
1064
+ let attempts = 0;
1065
+ const poll = async () => {
1066
+ if (attempts++ > 60) {
1067
+ updateCard(cardId, null, 'Timed out');
1068
+ return;
1069
+ }
1070
+ const statusRes = await fetch(`${state.apiUrl}/status/${job_id}`);
1071
+ const data = await statusRes.json();
1072
+
1073
+ if (data.status === 'completed') {
1074
+ const latency = (Date.now() - startTime) / 1000;
1075
+ state.completedCount++;
1076
+ state.totalLatency += latency;
1077
+ state.lastDetectedLang = (data.detected_language || 'β€”').split(' ')[0];
1078
+ updateStats();
1079
+ updateCard(cardId, data, null);
1080
+
1081
+ if (document.getElementById('autoPlay').checked && data.audio_url) {
1082
+ new Audio(`${state.apiUrl}${data.audio_url}`).play().catch(() => {});
1083
+ }
1084
+ } else if (data.status === 'failed') {
1085
+ updateCard(cardId, null, data.error || 'Failed');
1086
+ } else {
1087
+ setTimeout(poll, 800);
1088
+ }
1089
+ };
1090
+ setTimeout(poll, 1000);
1091
+
1092
+ } catch(e) {
1093
+ updateCard(cardId, null, e.message);
1094
+ showToast(`Send error: ${e.message}`, 'error');
1095
+ }
1096
+ }
1097
+
1098
+ // ─── Feed cards ───────────────────────────────────────────────────────────────
1099
+ function addProcessingCard(id) {
1100
+ const feed = document.getElementById('feedBody');
1101
+ const card = document.createElement('div');
1102
+ card.className = 'translation-card processing';
1103
+ card.id = id;
1104
+ card.innerHTML = `
1105
+ <div class="card-meta">
1106
+ <div class="card-lang">
1107
+ <span class="lang-tag source">AUTO</span>
1108
+ <span class="card-arrow">β†’</span>
1109
+ <span class="lang-tag target">${state.targetLang.toUpperCase().slice(0,3)}</span>
1110
+ </div>
1111
+ <span class="card-time">${new Date().toLocaleTimeString()}</span>
1112
+ </div>
1113
+ <div class="card-processing">⟳ Translating...</div>`;
1114
+ feed.insertBefore(card, feed.firstChild);
1115
+ feed.scrollTop = 0;
1116
+ }
1117
+
1118
+ function updateCard(id, data, error) {
1119
+ const card = document.getElementById(id);
1120
+ if (!card) return;
1121
+ card.classList.remove('processing');
1122
+
1123
+ if (error) {
1124
+ card.innerHTML = `
1125
+ <div class="card-meta">
1126
+ <span class="lang-tag source" style="background:rgba(255,68,102,0.1);color:var(--recording);border-color:rgba(255,68,102,0.3)">ERROR</span>
1127
+ <span class="card-time">${new Date().toLocaleTimeString()}</span>
1128
+ </div>
1129
+ <div class="card-processing" style="color:var(--recording)">${error}</div>`;
1130
+ return;
1131
+ }
1132
+
1133
+ const langName = (data.detected_language || 'Unknown').split(' ')[0];
1134
+ const audioBtn = data.audio_url ? `
1135
+ <div class="card-audio">
1136
+ <button class="play-btn" onclick="playAudio('${state.apiUrl}${data.audio_url}')">
1137
+ β–Ά Play audio
1138
+ </button>
1139
+ </div>` : '';
1140
+
1141
+ card.innerHTML = `
1142
+ <div class="card-meta">
1143
+ <div class="card-lang">
1144
+ <span class="lang-tag source">${langName.toUpperCase().slice(0,6)}</span>
1145
+ <span class="card-arrow">β†’</span>
1146
+ <span class="lang-tag target">${state.targetLang.toUpperCase().slice(0,3)}</span>
1147
+ </div>
1148
+ <span class="card-time">${new Date().toLocaleTimeString()}</span>
1149
+ </div>
1150
+ ${data.source_text && !data.source_text.includes('Direct speech') ? `<div class="card-original">${data.source_text}</div>` : ''}
1151
+ <div class="card-translation">${data.translated_text || 'β€”'}</div>
1152
+ ${audioBtn}`;
1153
+ }
1154
+
1155
+ function playAudio(url) {
1156
+ new Audio(url).play().catch(e => showToast('Could not play audio', 'error'));
1157
+ }
1158
+
1159
+ function clearFeed() {
1160
+ const feed = document.getElementById('feedBody');
1161
+ feed.innerHTML = '<div class="feed-empty" id="feedEmpty"><div class="feed-empty-icon">πŸŽ™</div><p>Translation feed cleared.</p></div>';
1162
+ state.chunksCount = 0;
1163
+ state.completedCount = 0;
1164
+ state.totalLatency = 0;
1165
+ updateStats();
1166
+ }
1167
+
1168
+ // ─── Stats & Timers ───────────────────────────────────────────────────────────
1169
+ function updateStats() {
1170
+ document.getElementById('statChunks').textContent = state.chunksCount;
1171
+ document.getElementById('statDone').textContent = state.completedCount;
1172
+ const avg = state.completedCount > 0
1173
+ ? (state.totalLatency / state.completedCount).toFixed(1) + 's'
1174
+ : 'β€”';
1175
+ document.getElementById('statAvgTime').textContent = avg;
1176
+ document.getElementById('statDetected').textContent = state.lastDetectedLang;
1177
+ }
1178
+
1179
+ function startSessionTimer() {
1180
+ state.sessionTimer = setInterval(() => {
1181
+ const elapsed = (Date.now() - state.sessionStart) / 1000;
1182
+ const mins = Math.floor(elapsed / 60);
1183
+ const secs = Math.floor(elapsed % 60);
1184
+ document.getElementById('sessionTime').textContent =
1185
+ `${mins}:${secs.toString().padStart(2,'0')}`;
1186
+
1187
+ // Cost estimate (T4 medium rate)
1188
+ state.totalCost = (elapsed / 3600) * COST_PER_HOUR;
1189
+ document.getElementById('costLabel').textContent = `$${state.totalCost.toFixed(3)}`;
1190
+ const limit = parseFloat(document.getElementById('costLimit').value) || DEFAULT_COST_LIMIT;
1191
+ const pct = Math.min((state.totalCost / limit) * 100, 100);
1192
+ document.getElementById('costFill').style.width = pct + '%';
1193
+
1194
+ // Auto-stop at 90% of limit β€” warn user
1195
+ if (state.totalCost >= limit * 0.9 && state.totalCost < limit) {
1196
+ showToast(`⚠ Approaching cost limit ($${limit.toFixed(2)}) β€” $${(limit - state.totalCost).toFixed(3)} remaining`, 'error');
1197
+ }
1198
+ // Auto-stop at limit
1199
+ if (state.totalCost >= limit && state.recording) {
1200
+ stopRecording();
1201
+ showToast(`πŸ›‘ Cost limit $${limit.toFixed(2)} reached β€” recording stopped automatically`, 'error');
1202
+ }
1203
+ }, 1000);
1204
+ }
1205
+
1206
+ function updateChunk(val) {
1207
+ state.chunkDuration = parseInt(val);
1208
+ document.getElementById('chunkVal').textContent = val + 's';
1209
+ }
1210
+
1211
+ // ─── Waveform ─────────────────────────────────────────────────────────────────
1212
+ function startWaveform() {
1213
+ try {
1214
+ state.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
1215
+ state.analyser = state.audioCtx.createAnalyser();
1216
+ state.analyser.fftSize = 256;
1217
+ const source = state.audioCtx.createMediaStreamSource(state.mediaStream);
1218
+ source.connect(state.analyser);
1219
+ drawWaveform();
1220
+ } catch(e) {}
1221
+ }
1222
+
1223
+ function drawWaveform() {
1224
+ const canvas = document.getElementById('waveform');
1225
+ const ctx = canvas.getContext('2d');
1226
+ canvas.width = canvas.offsetWidth * window.devicePixelRatio;
1227
+ canvas.height = 48 * window.devicePixelRatio;
1228
+ ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
1229
+
1230
+ const W = canvas.offsetWidth, H = 48;
1231
+ const bufLen = state.analyser.frequencyBinCount;
1232
+ const dataArr = new Uint8Array(bufLen);
1233
+
1234
+ const draw = () => {
1235
+ state.animFrame = requestAnimationFrame(draw);
1236
+ state.analyser.getByteTimeDomainData(dataArr);
1237
+
1238
+ ctx.clearRect(0, 0, W, H);
1239
+
1240
+ ctx.beginPath();
1241
+ ctx.strokeStyle = 'rgba(124,106,255,0.7)';
1242
+ ctx.lineWidth = 1.5;
1243
+ ctx.lineJoin = 'round';
1244
+
1245
+ const sliceW = W / bufLen;
1246
+ let x = 0;
1247
+ for (let i = 0; i < bufLen; i++) {
1248
+ const v = dataArr[i] / 128.0;
1249
+ const y = (v * H) / 2;
1250
+ i === 0 ? ctx.moveTo(x, y) : ctx.lineTo(x, y);
1251
+ x += sliceW;
1252
+ }
1253
+ ctx.stroke();
1254
+ };
1255
+ draw();
1256
+ }
1257
+
1258
+ // ─── Toast ────────────────────────────────────────────────────────────────────
1259
+ let toastTimer;
1260
+ function showToast(msg, type) {
1261
+ const el = document.getElementById('toast');
1262
+ el.textContent = msg;
1263
+ el.className = `toast show ${type}`;
1264
+ clearTimeout(toastTimer);
1265
+ toastTimer = setTimeout(() => el.classList.remove('show'), 3000);
1266
+ }
1267
+
1268
+ init();
1269
+ </script>
1270
+ </body>
1271
+ </html>