bChaukar commited on
Commit
a700d6f
·
verified ·
1 Parent(s): 4cdd9ce

Update rise-frontend.js

Browse files
Files changed (1) hide show
  1. rise-frontend.js +172 -871
rise-frontend.js CHANGED
@@ -1,895 +1,196 @@
1
-
2
- /*
3
- * RISE Reading Practice Frontend
4
- *
5
- * Connects the reading interface to the FastAPI backend running in Colab.
6
- * Features:
7
- * - AI-generated reading passages
8
- * - Whisper speech recognition
9
- * - Reading accuracy and pronunciation feedback
10
- * - Text-to-speech pronunciation playback
11
- * - Hand gesture controls
12
- * - Session summary
13
- *
14
- * IMPORTANT:
15
- * Set window.RISE_API_BASE in your HTML before loading this file.
16
- */
17
-
18
- (() => {
19
- "use strict";
20
-
21
- const API = (window.RISE_API_BASE || "").replace(/\/+$/, "");
22
- const root = document.getElementById("rise-reading-app");
23
-
24
- if (!root) {
25
- console.error("RISE: Could not find #rise-reading-app.");
26
- return;
27
- }
28
-
29
- if (!API) {
30
- root.innerHTML = `
31
- <p style="color:#b91c1c">
32
- RISE API is not configured. Set window.RISE_API_BASE
33
- to your HTTPS backend URL.
34
- </p>`;
35
- return;
36
- }
37
-
38
- // --------------------------------------------------
39
- // READING INTERFACE
40
- // --------------------------------------------------
41
-
42
- root.innerHTML = `
43
- <style>
44
- #rise-reading-app {
45
- font-family: system-ui, sans-serif;
46
- color: #172033;
47
- max-width: 900px;
48
- margin: 20px auto;
49
- }
50
-
51
- #rise-reading-app .rise-card {
52
- border: 1px solid #dbe3ef;
53
- border-radius: 16px;
54
- padding: 20px;
55
- margin: 12px 0;
56
- background: #fff;
57
- box-shadow: 0 5px 20px #0f172a0d;
58
- }
59
-
60
- #rise-reading-app .rise-row {
61
- display: flex;
62
- gap: 10px;
63
- flex-wrap: wrap;
64
- align-items: center;
65
- }
66
-
67
- #rise-reading-app button {
68
- border: 0;
69
- border-radius: 10px;
70
- padding: 10px 14px;
71
- background: #5746e8;
72
- color: white;
73
- font-weight: 650;
74
- cursor: pointer;
75
- }
76
-
77
- #rise-reading-app button.secondary {
78
- background: #475569;
79
- }
80
-
81
- #rise-reading-app button.danger {
82
- background: #b91c1c;
83
- }
84
-
85
- #rise-reading-app button:disabled {
86
- opacity: 0.5;
87
- cursor: not-allowed;
88
- }
89
-
90
- #rise-reading-app #rise-passage {
91
- font-size: 1.2rem;
92
- line-height: 1.9;
93
- white-space: pre-wrap;
94
- overflow-wrap: anywhere;
95
- background: #f8fafc;
96
- padding: 16px;
97
- border-radius: 12px;
98
- }
99
-
100
- #rise-reading-app .rise-muted {
101
- color: #64748b;
102
- font-size: 0.92rem;
103
- }
104
-
105
- #rise-reading-app .rise-metrics {
106
- display: grid;
107
- grid-template-columns: repeat(
108
- auto-fit,
109
- minmax(130px, 1fr)
110
- );
111
- gap: 8px;
112
- }
113
-
114
- #rise-reading-app .rise-metric {
115
- background: #f1f5f9;
116
- border-radius: 10px;
117
- padding: 10px;
118
- overflow-wrap: anywhere;
119
- }
120
-
121
- #rise-reading-app .rise-metric b {
122
- display: block;
123
- font-size: 1.15rem;
124
- }
125
-
126
- #rise-reading-app #rise-status {
127
- min-height: 1.5em;
128
- }
129
-
130
- #rise-reading-app .rise-err {
131
- color: #b91c1c;
132
- }
133
-
134
- #rise-reading-app .rise-success {
135
- color: #15803d;
136
- }
137
-
138
- #rise-reading-app video {
139
- max-width: 100%;
140
- }
141
- </style>
142
-
143
- <section class="rise-card">
144
- <h2>📖 RISE Reading Practice</h2>
145
-
146
- <p class="rise-muted">
147
- AI-generated passages · Whisper speech recognition ·
148
- pronunciation feedback · hand gestures
149
- </p>
150
-
151
- <div class="rise-row">
152
- <button id="rise-start">Start practice</button>
153
-
154
- <button id="rise-record" disabled>
155
- 🎙️ Start recording
156
- </button>
157
-
158
- <button id="rise-stop" disabled>
159
- Stop &amp; evaluate
160
- </button>
161
-
162
- <button id="rise-finish" class="danger" disabled>
163
- Finish session
164
- </button>
165
- </div>
166
-
167
- <p id="rise-status" role="status" aria-live="polite">
168
- Connecting to the RISE backend...
169
- </p>
170
-
171
- <p id="rise-level" class="rise-muted">
172
- Your selected level and activity will appear here.
173
- </p>
174
-
175
- <div id="rise-passage">
176
- Press Start practice to generate your reading passage.
177
- </div>
178
-
179
- <div id="rise-actions" class="rise-row"
180
- style="margin-top:12px">
181
- </div>
182
- </section>
183
-
184
- <section class="rise-card">
185
- <h3>Speech evaluation</h3>
186
-
187
- <p id="rise-transcript">
188
- Your transcription will appear here.
189
- </p>
190
-
191
- <div id="rise-metrics" class="rise-metrics"></div>
192
-
193
- <p id="rise-feedback"></p>
194
-
195
- <div id="rise-errors"></div>
196
- </section>
197
-
198
- <section class="rise-card">
199
- <h3>Hand gesture control</h3>
200
-
201
- <p class="rise-muted">
202
- Allow camera access to detect a hand gesture.
203
- Your backend determines the gesture and its action.
204
- </p>
205
-
206
- <div class="rise-row">
207
- <button id="rise-gesture" class="secondary" disabled>
208
- 📷 Detect gesture
209
- </button>
210
-
211
- <span id="rise-gesture-result" aria-live="polite"></span>
212
- </div>
213
- </section>
214
- `;
215
-
216
- // --------------------------------------------------
217
- // STATE AND HELPERS
218
- // --------------------------------------------------
219
-
220
- const $ = (id) => root.querySelector(`#${id}`);
221
-
222
- let sessionId = null;
223
- let recorder = null;
224
- let chunks = [];
225
- let startedAt = 0;
226
- let stream = null;
227
- let recordingInProgress = false;
228
- let evaluationInProgress = false;
229
- const activeEmail = (localStorage.getItem("riseActiveUserEmail") || "").trim().toLowerCase();
230
- const profileKey = activeEmail ? "riseUser:" + activeEmail : null;
231
-
232
- function loadUserProfile() {
233
- let profile = profileKey ? JSON.parse(localStorage.getItem(profileKey) || "null") : null;
234
- if (!profile) {
235
- const starting = Math.min(5, Math.max(1, Number(localStorage.getItem("startingLevel")) || 1));
236
- profile = { email: activeEmail, startingLevel: starting, unlockedLevel: starting, attempts: [], sessionsCompleted: 0, averageScore: 0 };
237
- }
238
- profile.startingLevel = Math.min(5, Math.max(1, Number(profile.startingLevel) || 1));
239
- profile.unlockedLevel = Math.min(5, Math.max(profile.startingLevel, Number(profile.unlockedLevel) || profile.startingLevel));
240
- if (!Array.isArray(profile.attempts)) profile.attempts = [];
241
- profile.sessionsCompleted = Number(profile.sessionsCompleted) || 0;
242
- return profile;
243
- }
244
-
245
- function saveEvaluationProgress(data) {
246
- const profile = loadUserProfile();
247
- const metrics = data.metrics || {};
248
- const score = Number(metrics.overall);
249
- profile.attempts.push({
250
- timestamp: new Date().toISOString(),
251
- level: data.level || "beginner",
252
- part: data.part || "sentence",
253
- overall: Number.isFinite(score) ? score : 0,
254
- wordAccuracy: Number(metrics.word_accuracy) || 0,
255
- pronunciation: Number(metrics.pronunciation) || 0,
256
- completion: Number(metrics.completion) || 0,
257
- wer: Number(data.wer) || 0,
258
- passed: Boolean(data.passed)
259
- });
260
- // A level unlocks only after its story is passed at >=80%.
261
- const levelNumbers = { beginner: 1, elementary: 2, intermediate: 3, upper_intermediate: 4, advanced: 5 };
262
- const completedLevel = levelNumbers[data.level];
263
- if (data.passed && data.part === "story" && Number(score) >= 80 && completedLevel) {
264
- profile.unlockedLevel = Math.max(profile.unlockedLevel, Math.min(5, completedLevel + 1));
265
  }
266
- const scores = profile.attempts.map(a => Number(a.overall)).filter(Number.isFinite);
267
- profile.averageScore = scores.length ? Math.round(scores.reduce((sum, n) => sum + n, 0) / scores.length * 100) / 100 : 0;
268
- if (profileKey) localStorage.setItem(profileKey, JSON.stringify(profile));
269
- return profile;
270
- }
271
-
272
- function saveCompletedSession() {
273
- const profile = loadUserProfile();
274
- profile.sessionsCompleted += 1;
275
- if (profileKey) localStorage.setItem(profileKey, JSON.stringify(profile));
276
- }
277
-
278
- const setStatus = (message, isError = false) => {
279
- const status = $("rise-status");
280
- status.textContent = message;
281
- status.className = isError ? "rise-err" : "";
282
  };
283
 
284
- const stopAudioTracks = () => {
285
- if (stream) {
286
- stream.getTracks().forEach((track) => track.stop());
287
- stream = null;
 
 
 
 
 
 
 
 
288
  }
289
  };
290
-
291
- const setSessionControls = (active) => {
292
- $("rise-record").disabled = !active || recordingInProgress;
293
- $("rise-stop").disabled = !active || !recordingInProgress;
294
- $("rise-finish").disabled = !active || evaluationInProgress;
295
- $("rise-gesture").disabled = !active || evaluationInProgress;
296
  };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
 
298
- async function api(path, options = {}) {
299
- const response = await fetch(`${API}${path}`, options);
300
-
301
- const body = await response.json().catch(() => ({}));
302
-
303
- if (!response.ok) {
304
- throw new Error(
305
- body.detail || `Backend request failed (${response.status})`
306
- );
307
- }
308
-
309
- return body;
310
- }
311
-
312
- function showPassage(data) {
313
- const level = String(data.level || "beginner")
314
- .replaceAll("_", " ");
315
-
316
- $("rise-level").textContent =
317
- `Level: ${level} · Part: ${data.part || "sentence"} · ` +
318
- `Pass mark: ${data.threshold ?? 80}%`;
319
-
320
- $("rise-passage").textContent =
321
- data.passage || "No passage was returned by the backend.";
322
- }
323
-
324
- function formatMetric(value, suffix = "%") {
325
- if (value === undefined || value === null) {
326
- return "N/A";
327
- }
328
-
329
- return `${value}${suffix}`;
330
- }
331
-
332
- // --------------------------------------------------
333
- // START READING SESSION
334
- // --------------------------------------------------
335
-
336
- $("rise-start").addEventListener("click", async () => {
337
- if (sessionId) {
338
- setStatus("A session is already active.");
339
- return;
340
- }
341
-
342
- $("rise-start").disabled = true;
343
- setStatus("Starting your session and generating a passage...");
344
-
345
- try {
346
- /*
347
- * practice.html should set RISE_SELECTED_LEVEL.
348
- * If it is not set, the backend starts at Beginner.
349
- */
350
- const selectedLevel =
351
- window.RISE_SELECTED_LEVEL || "beginner";
352
-
353
- const data = await api("/api/session/start", {
354
- method: "POST",
355
- headers: {
356
- "Content-Type": "application/json"
357
- },
358
- body: JSON.stringify({
359
- level: selectedLevel,
360
- part: "sentence"
361
- })
362
- });
363
-
364
- if (!data.session_id) {
365
- throw new Error("The backend did not return a session ID.");
366
- }
367
-
368
- sessionId = data.session_id;
369
-
370
- showPassage(data);
371
-
372
- $("rise-transcript").textContent =
373
- "Your transcription will appear here.";
374
-
375
- $("rise-metrics").innerHTML = "";
376
- $("rise-feedback").textContent = "";
377
- $("rise-errors").innerHTML = "";
378
-
379
- setStatus(
380
- `Your session is ready. Text model: ${data.model || "loaded"}`
381
- );
382
-
383
- setSessionControls(true);
384
-
385
- $("rise-start").disabled = true;
386
-
387
- } catch (error) {
388
- console.error("RISE session-start error:", error);
389
-
390
- setStatus(
391
- `Could not start the session: ${error.message}`,
392
- true
393
- );
394
-
395
- $("rise-start").disabled = false;
396
- }
397
  });
398
-
399
- // --------------------------------------------------
400
- // START AUDIO RECORDING
401
- // --------------------------------------------------
402
-
403
- $("rise-record").addEventListener("click", async () => {
404
- if (!sessionId || recordingInProgress || evaluationInProgress) {
405
- return;
406
- }
407
-
408
- if (!navigator.mediaDevices?.getUserMedia ||
409
- typeof MediaRecorder === "undefined") {
410
- setStatus(
411
- "Audio recording is not supported by this browser. " +
412
- "Use a recent browser over HTTPS.",
413
- true
414
- );
415
- return;
416
- }
417
-
418
  try {
419
- stream = await navigator.mediaDevices.getUserMedia({
420
- audio: {
421
- channelCount: 1,
422
- echoCancellation: true,
423
- noiseSuppression: true
424
- }
425
- });
426
-
427
- let mimeType = "";
428
-
429
- if (MediaRecorder.isTypeSupported("audio/webm;codecs=opus")) {
430
- mimeType = "audio/webm;codecs=opus";
431
- } else if (MediaRecorder.isTypeSupported("audio/webm")) {
432
- mimeType = "audio/webm";
433
- }
434
-
435
- recorder = mimeType
436
- ? new MediaRecorder(stream, { mimeType })
437
- : new MediaRecorder(stream);
438
-
439
- chunks = [];
440
-
441
- recorder.addEventListener("dataavailable", (event) => {
442
- if (event.data && event.data.size > 0) {
443
- chunks.push(event.data);
444
- }
445
- });
446
-
447
- recorder.start(150);
448
-
449
- startedAt = Date.now();
450
- recordingInProgress = true;
451
-
452
- setSessionControls(true);
453
-
454
- setStatus(
455
- "Recording... Read the displayed passage aloud, " +
456
- "then press Stop & evaluate."
457
- );
458
-
459
- } catch (error) {
460
- console.error("RISE recording error:", error);
461
-
462
- stopAudioTracks();
463
-
464
- recordingInProgress = false;
465
- setSessionControls(Boolean(sessionId));
466
-
467
- setStatus(
468
- `Could not start the microphone: ${error.message}. ` +
469
- "Check your browser's microphone permission.",
470
- true
471
- );
472
  }
473
  });
474
 
475
- // --------------------------------------------------
476
- // STOP RECORDING AND EVALUATE SPEECH
477
- // --------------------------------------------------
478
-
479
- $("rise-stop").addEventListener("click", async () => {
480
- if (!recorder ||
481
- recorder.state === "inactive" ||
482
- evaluationInProgress) {
483
- return;
484
- }
485
-
486
- evaluationInProgress = true;
487
- setSessionControls(Boolean(sessionId));
488
-
489
- setStatus("Uploading audio and evaluating your reading with Whisper...");
490
-
491
- const activeRecorder = recorder;
492
-
493
- try {
494
- const audioBlob = await new Promise((resolve, reject) => {
495
- activeRecorder.addEventListener("stop", () => {
496
- if (chunks.length === 0) {
497
- reject(new Error("No audio was recorded. Please try again."));
498
- return;
499
- }
500
-
501
- resolve(
502
- new Blob(chunks, {
503
- type: activeRecorder.mimeType || "audio/webm"
504
- })
505
- );
506
- }, { once: true });
507
-
508
- activeRecorder.stop();
509
- });
510
-
511
- stopAudioTracks();
512
-
513
- recordingInProgress = false;
514
-
515
- const elapsedSeconds = (Date.now() - startedAt) / 1000;
516
-
517
- const formData = new FormData();
518
-
519
- formData.append("audio", audioBlob, "reading.webm");
520
- formData.append(
521
- "elapsed_seconds",
522
- String(elapsedSeconds)
523
- );
524
-
525
- const data = await api(
526
- `/api/session/${encodeURIComponent(sessionId)}/evaluate`,
527
- {
528
- method: "POST",
529
- body: formData
530
- }
531
- );
532
-
533
- // Show the speech recognition result.
534
- $("rise-transcript").textContent =
535
- `Heard: ${data.transcript || "(No speech recognized)"}`;
536
-
537
- // Show evaluation metrics.
538
- const metrics = data.metrics || {};
539
-
540
- const metricItems = [
541
- ["Overall score", formatMetric(metrics.overall)],
542
- ["Word accuracy", formatMetric(metrics.word_accuracy)],
543
- ["Pronunciation", formatMetric(metrics.pronunciation)],
544
- ["Completion", formatMetric(metrics.completion)],
545
- ["Word error rate", formatMetric(data.wer)],
546
- ["Speaking rate", formatMetric(metrics.speaking_rate, " wpm")]
547
- ];
548
-
549
- $("rise-metrics").replaceChildren();
550
-
551
- metricItems.forEach(([label, value]) => {
552
- const card = document.createElement("div");
553
- card.className = "rise-metric";
554
-
555
- const name = document.createElement("span");
556
- name.textContent = label;
557
-
558
- const result = document.createElement("b");
559
- result.textContent = value;
560
-
561
- card.append(name, result);
562
- $("rise-metrics").appendChild(card);
563
- });
564
-
565
- const passed = Boolean(data.passed);
566
- saveEvaluationProgress(data);
567
-
568
- $("rise-feedback").textContent =
569
- `${passed ? "✅ Passed! " : "🔁 Try again. "}` +
570
- (data.message || "");
571
-
572
- $("rise-feedback").className =
573
- passed ? "rise-success" : "";
574
-
575
- // Show detected words and pronunciation playback controls.
576
- $("rise-errors").replaceChildren();
577
-
578
- const events = Array.isArray(data.events) ? data.events : [];
579
-
580
- events
581
- .filter((event) => event.type !== "correct")
582
- .forEach((event) => {
583
- const row = document.createElement("div");
584
- row.className = "rise-row";
585
- row.style.margin = "6px 0";
586
-
587
- const label = document.createElement("span");
588
-
589
- label.textContent =
590
- `${event.word || "Word"}: ${event.type || "mismatch"}` +
591
- (event.heard ? ` (heard "${event.heard}")` : "");
592
-
593
- const button = document.createElement("button");
594
- button.className = "secondary";
595
- button.textContent =
596
- `🔊 Hear "${event.word || "word"}"`;
597
-
598
- button.addEventListener("click", async () => {
599
- try {
600
- const audio = new Audio(
601
- `${API}/api/pronounce/${encodeURIComponent(
602
- event.word || ""
603
- )}`
604
- );
605
-
606
- await audio.play();
607
-
608
- } catch (error) {
609
- setStatus(
610
- `Could not play pronunciation: ${error.message}`,
611
- true
612
- );
613
- }
614
- });
615
-
616
- row.append(label, button);
617
- $("rise-errors").appendChild(row);
618
- });
619
-
620
- // Handle progression returned by the backend.
621
- if (data.completed) {
622
- setStatus(
623
- data.message ||
624
- "Congratulations! You completed the reading levels."
625
- );
626
-
627
- $("rise-passage").textContent =
628
- "Reading session completed. Finish the session to view your summary.";
629
-
630
- sessionId = null;
631
- $("rise-start").disabled = false;
632
- setSessionControls(false);
633
-
634
- } else if (data.next_passage) {
635
- showPassage({
636
- level: data.next_level,
637
- part: data.next_part,
638
- passage: data.next_passage,
639
- threshold: data.threshold
640
- });
641
-
642
- setStatus(
643
- data.message ||
644
- (passed
645
- ? "Great work! Continue with the next activity."
646
- : "You need to try this activity again.")
647
- );
648
-
649
- // The backend determines whether the next passage changes.
650
- setSessionControls(true);
651
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
652
  } else {
653
- setStatus(
654
- data.message ||
655
- "Evaluation completed, but the backend did not return a next passage.",
656
- !passed
657
- );
658
-
659
- setSessionControls(true);
660
  }
661
-
662
- } catch (error) {
663
- console.error("RISE evaluation error:", error);
664
-
665
- stopAudioTracks();
666
- recordingInProgress = false;
667
-
668
- setStatus(
669
- `Could not evaluate the recording: ${error.message}`,
670
- true
671
- );
672
-
673
- setSessionControls(Boolean(sessionId));
674
-
675
- } finally {
676
- evaluationInProgress = false;
677
-
678
- if (sessionId) {
679
- setSessionControls(true);
680
- }
681
- }
682
  });
683
 
684
- // --------------------------------------------------
685
- // FINISH SESSION AND DISPLAY SUMMARY
686
- // --------------------------------------------------
687
-
688
- $("rise-finish").addEventListener("click", async () => {
689
- if (!sessionId || recordingInProgress || evaluationInProgress) {
690
- return;
691
- }
692
-
693
- const finishedSessionId = sessionId;
694
-
695
- $("rise-finish").disabled = true;
696
- $("rise-gesture").disabled = true;
697
-
698
- setStatus("Preparing your session summary...");
699
-
700
- try {
701
- const data = await api(
702
- `/api/session/${encodeURIComponent(finishedSessionId)}/finish`,
703
- {
704
- method: "POST"
705
- }
706
- );
707
-
708
- saveCompletedSession();
709
- setStatus(
710
- `Session finished. Attempts: ${data.attempts ?? "N/A"}; ` +
711
- `average overall score: ${data.average_overall ?? "N/A"}%; ` +
712
- `average word error rate: ${data.average_wer ?? "N/A"}%.`
713
- );
714
-
715
- $("rise-feedback").textContent =
716
- JSON.stringify(data.text_generation ?? {}, null, 2);
717
-
718
- sessionId = null;
719
-
720
- $("rise-start").disabled = false;
721
- setSessionControls(false);
722
-
723
- } catch (error) {
724
- console.error("RISE finish-session error:", error);
725
-
726
- setStatus(
727
- `Could not finish the session: ${error.message}`,
728
- true
729
- );
730
 
731
- setSessionControls(Boolean(sessionId));
732
- }
 
 
 
 
 
 
 
 
 
 
733
  });
734
 
735
- // --------------------------------------------------
736
- // HAND GESTURE DETECTION
737
- // --------------------------------------------------
738
-
739
- $("rise-gesture").addEventListener("click", async () => {
740
- if (!sessionId || evaluationInProgress || recordingInProgress) {
741
- return;
742
- }
743
-
744
- if (!navigator.mediaDevices?.getUserMedia) {
745
- setStatus(
746
- "Camera access is not supported by this browser.",
747
- true
748
- );
749
- return;
750
- }
751
-
752
- let cameraStream = null;
753
-
754
- $("rise-gesture").disabled = true;
755
- $("rise-gesture-result").textContent = "Opening camera...";
756
-
757
- try {
758
- cameraStream = await navigator.mediaDevices.getUserMedia({
759
- video: {
760
- facingMode: "user"
761
- }
762
- });
763
-
764
- const video = document.createElement("video");
765
-
766
- video.muted = true;
767
- video.playsInline = true;
768
- video.srcObject = cameraStream;
769
-
770
  await video.play();
771
-
772
- // Allow the camera to produce a frame.
773
- await new Promise((resolve) => setTimeout(resolve, 700));
774
-
775
- if (!video.videoWidth || !video.videoHeight) {
776
- throw new Error("The camera did not provide a usable image.");
777
- }
778
-
779
- const canvas = document.createElement("canvas");
780
-
781
- canvas.width = video.videoWidth;
782
- canvas.height = video.videoHeight;
783
-
784
- const context = canvas.getContext("2d");
785
-
786
- if (!context) {
787
- throw new Error("Could not capture the camera image.");
788
- }
789
-
790
- context.drawImage(video, 0, 0);
791
-
792
- const imageBlob = await new Promise((resolve, reject) => {
793
- canvas.toBlob(
794
- (blob) => {
795
- if (blob) {
796
- resolve(blob);
797
- } else {
798
- reject(new Error("Could not encode the camera image."));
799
- }
800
- },
801
- "image/jpeg",
802
- 0.85
803
- );
804
- });
805
-
806
- cameraStream.getTracks().forEach((track) => track.stop());
807
- cameraStream = null;
808
-
809
- const formData = new FormData();
810
- formData.append("image", imageBlob, "gesture.jpg");
811
-
812
- const data = await api(
813
- `/api/session/${encodeURIComponent(sessionId)}/gesture`,
814
- {
815
- method: "POST",
816
- body: formData
817
- }
818
- );
819
-
820
- $("rise-gesture-result").textContent =
821
- `Detected: ${data.gesture || "unknown"} · ` +
822
- `Action: ${data.action || "none"}`;
823
-
824
- if (data.passage) {
825
- showPassage({
826
- level: data.level,
827
- part: data.part,
828
- passage: data.passage,
829
- threshold: data.threshold || 80
830
- });
831
- }
832
-
833
- if (data.ended) {
834
- setStatus("Session ended by the hand gesture.");
835
-
836
- sessionId = null;
837
- $("rise-start").disabled = false;
838
- setSessionControls(false);
839
-
840
- } else if (data.action && data.action !== "none") {
841
- setStatus(`Gesture applied: ${data.action}.`);
842
- }
843
-
844
- } catch (error) {
845
- console.error("RISE gesture error:", error);
846
-
847
- setStatus(
848
- `Camera or gesture error: ${error.message}`,
849
- true
850
- );
851
-
852
- $("rise-gesture-result").textContent = "Gesture detection failed.";
853
-
854
- } finally {
855
- if (cameraStream) {
856
- cameraStream.getTracks().forEach((track) => track.stop());
857
- }
858
-
859
- $("rise-gesture").disabled = !sessionId;
860
  }
861
  });
862
 
863
- // --------------------------------------------------
864
- // CHECK BACKEND CONNECTIVITY
865
- // --------------------------------------------------
866
-
867
- api("/api/health")
868
- .then((health) => {
869
- if (!health.ok) {
870
- throw new Error("The backend health check did not return OK.");
871
- }
872
-
873
- const models = health.models || {};
874
-
875
- setStatus(
876
- `Backend connected. Whisper: ${
877
- models.whisper ? "ready" : "unavailable"
878
- }; text generation: ${
879
- models.text_generation ? "ready" : "unavailable"
880
- }; gestures: ${
881
- models.gesture ? "ready" : "unavailable"
882
- }.`
883
- );
884
- })
885
- .catch((error) => {
886
- console.error("RISE health-check error:", error);
887
-
888
- setStatus(
889
- "Backend not reachable. Check the API URL and make sure " +
890
- "the Colab server and Cloudflare tunnel are running.",
891
- true
892
- );
893
- });
894
-
895
- })();
 
1
+ const unique = combined.filter((r) => {
2
+ const key = r && r.timestamp != null
3
+ ? `timestamp:${r.timestamp}`
4
+ : `fallback:${r?.level || ''}|${r?.part || ''}|${r?.overall || 0}|${r?.word_accuracy || 0}|${r?.wer || 0}`;
5
+ if (seen.has(key)) return false;
6
+ seen.add(key);
7
+ return true;
8
+ });
9
+ localStorage.setItem('riseHistory', JSON.stringify(unique));
10
+ return unique;
11
+ } catch (error) {
12
+ console.warn('Could not save RISE session history:', error);
13
+ return Array.isArray(history) ? history : [];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  };
16
 
17
+ const renderHistory = (history) => {
18
+ const el = $('#rise-history',root);
19
+ const cumulative = saveCumulativeHistory(history);
20
+ if(!cumulative.length){el.innerHTML='';return;}
21
+ el.innerHTML = `<h3>Session history</h3><table class="rise-history"><thead><tr><th>Level / part</th><th>Score</th><th>Accuracy</th><th>WER</th></tr></thead><tbody>${cumulative.slice().reverse().slice(0,20).map(r=>`<tr><td>${esc(labelLevel(r.level))} / ${esc(labelPart(r.part))}</td><td>${Number(r.overall||0).toFixed(1)}%</td><td>${Number(r.word_accuracy||0).toFixed(1)}%</td><td>${Number(r.wer||0).toFixed(1)}%</td></tr>`).join('')}</tbody></table>`;
22
+ };
23
+ const playPronunciations = async (items) => {
24
+ for (const item of (items || [])) {
25
+ if(item.audio_base64){
26
+ try { const bytes = Uint8Array.from(atob(item.audio_base64), c=>c.charCodeAt(0)); const url=URL.createObjectURL(new Blob([bytes],{type:'audio/mpeg'})); const audio=new Audio(url); await audio.play(); await new Promise(resolve=>{audio.onended=resolve;audio.onerror=resolve;}); URL.revokeObjectURL(url); }
27
+ catch(e){ console.warn('Could not autoplay pronunciation audio',e); }
28
+ }
29
  }
30
  };
31
+ const renderEvents = (events) => {
32
+ const el=$('#rise-events',root); if(!events || !events.length){el.innerHTML='';return;}
33
+ el.innerHTML=events.map(e=>`<span class="rise-word ${esc(e.type)}" title="${esc(e.feedback || '')}">${e.type==='correct'?'✓':'✗'} ${esc(e.word)}${e.heard && e.type!=='correct'?' · heard: '+esc(e.heard):''}</span>`).join('');
 
 
 
34
  };
35
+ const renderMetrics = (data) => {
36
+ const m=data.metrics || {}; const passed=!!data.passed;
37
+ $('#rise-results',root).innerHTML=`<h3>${passed?'Passed':'Score report'}</h3><div class="rise-results">
38
+ <div class="rise-metric"><small>Overall score</small><strong>${Number(m.overall||0).toFixed(1)}%</strong></div>
39
+ <div class="rise-metric"><small>Word accuracy</small><strong>${Number(m.word_accuracy||0).toFixed(1)}%</strong></div>
40
+ <div class="rise-metric"><small>Pronunciation</small><strong>${Number(m.pronunciation||0).toFixed(1)}%</strong></div>
41
+ <div class="rise-metric"><small>Completion</small><strong>${Number(m.completion||0).toFixed(1)}%</strong></div>
42
+ <div class="rise-metric"><small>Word error rate</small><strong>${Number(data.wer||0).toFixed(1)}%</strong></div>
43
+ <div class="rise-metric"><small>Speaking rate</small><strong>${Number(m.speaking_rate||0).toFixed(1)} <small>wpm</small></strong></div>
44
+ <div class="rise-metric"><small>Hesitations</small><strong>${Number(m.hesitations||0)}</strong></div>
45
+ <div class="rise-metric"><small>Mismatches</small><strong>${Number(m.mismatches||0)}</strong></div>
46
+ <div class="rise-metric"><small>Skipped words</small><strong>${Number(m.skipped||0)}</strong></div></div>`;
47
+ };
48
+ const stopTracks = () => { if(mediaStream){mediaStream.getTracks().forEach(t=>t.stop());mediaStream=null;} };
49
 
50
+ $('#rise-check',root).addEventListener('click', async () => {
51
+ saveApi(); try { const data=await request('/api/health'); $('#rise-connection',root).textContent='Backend connected'; setStatus(`Connected. Whisper loaded: ${data.whisper_loaded ? 'yes' : 'unknown'}.`, 'success'); }
52
+ catch(e){ $('#rise-connection',root).textContent='Backend unavailable'; setStatus(e.message,'error'); }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  });
54
+ $('#rise-start',root).addEventListener('click', async () => {
55
+ saveApi(); try { setStatus('Starting RISE session…'); const data=await request('/api/session/start',jsonOptions({level:$('#rise-level',root).value,part:'sentence'})); session=data; renderSession(data); $('#rise-results',root).innerHTML=''; $('#rise-decision',root).classList.add('rise-hidden'); setStatus('Session started. Read one phrase at a time, then stop to evaluate it.','success'); }
56
+ catch(e){setStatus(e.message,'error');}
57
+ });
58
+ $('#rise-record',root).addEventListener('click', async () => {
59
+ if(!session || busy) return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  try {
61
+ mediaStream=await navigator.mediaDevices.getUserMedia({audio:{channelCount:1,echoCancellation:true,noiseSuppression:true,autoGainControl:true}});
62
+ const mime=['audio/webm;codecs=opus','audio/webm','audio/mp4'].find(t=>MediaRecorder.isTypeSupported(t));
63
+ recorder=mime?new MediaRecorder(mediaStream,{mimeType:mime}):new MediaRecorder(mediaStream); chunks=[];
64
+ recorder.ondataavailable=e=>{if(e.data.size)chunks.push(e.data);};
65
+ recorder.onstart=()=>{setBusy(true);$('#rise-record',root).disabled=true;setStatus('Recording phrase… read a short phrase, then press Stop & evaluate.');};
66
+ recorder.onstop=async()=>{
67
+ stopTracks(); setBusy(false);
68
+ const blob=new Blob(chunks,{type:recorder.mimeType||'audio/webm'});
69
+ if(blob.size<1500){setStatus('Recording was too short. Please try again.','error');return;}
70
+ const fd=new FormData();fd.append('session_id',session.session_id);fd.append('audio',blob,'reading.webm');
71
+ try {
72
+ setStatus('Sending audio to Whisper and evaluating…');
73
+ const data=await request('/api/evaluate',{method:'POST',body:fd});
74
+ renderSession(data);
75
+ $('#rise-heard',root).textContent=data.heard?'Whisper heard: '+data.heard:'';
76
+ renderEvents(data.events);
77
+ await playPronunciations(data.pronunciations);
78
+ setStatus(data.message,data.passage_complete?'success':'');
79
+ } catch(e){setStatus(e.message,'error');}
80
+ };
81
+ recorder.start(200);
82
+ } catch(e){
83
+ stopTracks();setBusy(false);
84
+ setStatus('Microphone unavailable. Allow microphone access in your browser, then try again. '+e.message,'error');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  }
86
  });
87
 
88
+ $('#rise-stop',root).addEventListener('click',()=>{
89
+ if(recorder && recorder.state!=='inactive')recorder.stop();
90
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
+ $('#rise-finish',root).addEventListener('click',async()=>{
93
+ if(!session||busy)return;
94
+ const fd=new FormData();fd.append('session_id',session.session_id);
95
+ try{
96
+ setStatus('Calculating your reading results…');
97
+ const data=await request('/api/session/finish',{method:'POST',body:fd});
98
+ recordPracticeDay();
99
+ renderMetrics(data);
100
+ renderHistory(Array.isArray(data.history)?data.history:(data.record?[data.record]:[]));
101
+ setStatus(data.message,data.passed?'success':'');
102
+ if(data.status==='retry_decision'){
103
+ $('#rise-decision',root).classList.remove('rise-hidden');
104
+ $('#rise-decision-text',root).textContent=data.message;
105
+ $('#rise-record',root).disabled=true;
106
+ } else if(data.status==='completed_all'){
107
+ session={...session,session_ended:true};
108
+ $('#rise-passage',root).textContent=data.message;
109
+ $('#rise-record',root).disabled=true;
110
+ $('#rise-finish',root).disabled=true;
111
  } else {
112
+ renderSession(data);
113
+ $('#rise-events',root).innerHTML='';
114
+ $('#rise-heard',root).textContent='';
115
+ setStatus(data.message+' A new passage is ready.','success');
 
 
 
116
  }
117
+ }catch(e){setStatus(e.message,'error');}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  });
119
 
120
+ const decide = async retry =>{
121
+ try{
122
+ const data=await request('/api/session/decision',jsonOptions({session_id:session.session_id,retry}));
123
+ $('#rise-decision',root).classList.add('rise-hidden');
124
+ if(data.session_ended){
125
+ session={...session,session_ended:true};
126
+ $('#rise-passage',root).textContent=data.message;
127
+ $('#rise-record',root).disabled=true;
128
+ $('#rise-finish',root).disabled=true;
129
+ setStatus(data.message);
130
+ renderHistory(data.history||[]);
131
+ }else{
132
+ renderSession(data);
133
+ $('#rise-results',root).innerHTML='';
134
+ $('#rise-events',root).innerHTML='';
135
+ setStatus(data.message,'success');
136
+ }
137
+ }catch(e){setStatus(e.message,'error');}
138
+ };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
+ $('#rise-retry',root).addEventListener('click',()=>decide(true));
141
+ $('#rise-give-up',root).addEventListener('click',()=>decide(false));
142
+
143
+ $('#rise-pronounce',root).addEventListener('click',async()=>{
144
+ const word=$('#rise-pronounce-word',root).value.trim();
145
+ if(!word){setStatus('Enter a word first.','error');return;}
146
+ try{
147
+ const data=await request('/api/pronounce',jsonOptions({word}));
148
+ if(data.audio_base64)await playPronunciations([data]);
149
+ else throw new Error(data.audio_error||'Pronunciation audio is unavailable.');
150
+ setStatus('Playing pronunciation for “'+word+'”.','success');
151
+ }catch(e){setStatus(e.message,'error');}
152
  });
153
 
154
+ $('#rise-gesture',root).addEventListener('click',async()=>{
155
+ if(!session||busy)return;
156
+ let stream;
157
+ try{
158
+ setStatus('Opening camera for one gesture snapshot…');
159
+ stream=await navigator.mediaDevices.getUserMedia({video:{facingMode:'user'}});
160
+ const video=document.createElement('video');
161
+ video.srcObject=stream;
162
+ video.muted=true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  await video.play();
164
+ await new Promise(r=>setTimeout(r,500));
165
+ const canvas=document.createElement('canvas');
166
+ canvas.width=video.videoWidth;
167
+ canvas.height=video.videoHeight;
168
+ canvas.getContext('2d').drawImage(video,0,0);
169
+ stream.getTracks().forEach(t=>t.stop());
170
+ stream=null;
171
+ const image_data=canvas.toDataURL('image/jpeg',0.85);
172
+ const data=await request('/api/gesture',jsonOptions({session_id:session.session_id,image_data}));
173
+ if(data.passage){
174
+ renderSession(data);
175
+ $('#rise-results',root).innerHTML='';
176
+ $('#rise-events',root).innerHTML='';
177
+ }
178
+ if(data.session_ended){
179
+ session={...session,session_ended:true};
180
+ $('#rise-record',root).disabled=true;
181
+ $('#rise-finish',root).disabled=true;
182
+ }
183
+ setStatus(data.message,data.gesture?'success':'');
184
+ }catch(e){
185
+ if(stream)stream.getTracks().forEach(t=>t.stop());
186
+ setStatus('Camera/gesture check failed. Allow camera access and try again. '+e.message,'error');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  }
188
  });
189
 
190
+ if(apiBase){
191
+ $('#rise-connection',root).textContent='URL configured';
192
+ request('/api/health')
193
+ .then(()=>{$('#rise-connection',root).textContent='Backend connected';})
194
+ .catch(()=>{$('#rise-connection',root).textContent='Backend not reachable';});
195
+ }
196
+ })();