bChaukar commited on
Commit
ab0e5f3
·
verified ·
1 Parent(s): 464ed8d

Update rise-frontend.js

Browse files
Files changed (1) hide show
  1. rise-frontend.js +813 -91
rise-frontend.js CHANGED
@@ -1,123 +1,845 @@
1
- /* RISE reading widget. Set window.RISE_API_BASE before loading this script. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  (() => {
3
- const API = (window.RISE_API_BASE || "").replace(/\/$/, "");
 
 
4
  const root = document.getElementById("rise-reading-app");
5
- if (!root) return;
 
 
 
 
 
6
  if (!API) {
7
- root.innerHTML = '<p style="color:#b91c1c">RISE API is not configured. Set window.RISE_API_BASE to your HTTPS backend URL.</p>';
 
 
 
 
8
  return;
9
  }
 
 
 
 
 
10
  root.innerHTML = `
11
  <style>
12
- #rise-reading-app{font-family:system-ui,sans-serif;color:#172033;max-width:900px;margin:20px auto}
13
- #rise-reading-app .rise-card{border:1px solid #dbe3ef;border-radius:16px;padding:20px;margin:12px 0;background:#fff;box-shadow:0 5px 20px #0f172a0d}
14
- #rise-reading-app .rise-row{display:flex;gap:10px;flex-wrap:wrap;align-items:center}
15
- #rise-reading-app button{border:0;border-radius:10px;padding:10px 14px;background:#1d4ed8;color:white;font-weight:650;cursor:pointer}
16
- #rise-reading-app button.secondary{background:#475569} #rise-reading-app button.danger{background:#b91c1c}
17
- #rise-reading-app button:disabled{opacity:.5;cursor:not-allowed}
18
- #rise-reading-app #rise-passage{font-size:1.2rem;line-height:1.9;white-space:pre-wrap;background:#f8fafc;padding:16px;border-radius:12px}
19
- #rise-reading-app .rise-muted{color:#64748b;font-size:.92rem} #rise-reading-app .rise-metrics{display:grid;grid-template-columns:repeat(auto-fit,minmax(130px,1fr));gap:8px}
20
- #rise-reading-app .rise-metric{background:#f1f5f9;border-radius:10px;padding:10px} #rise-reading-app .rise-metric b{display:block;font-size:1.15rem}
21
- #rise-reading-app #rise-status{min-height:1.5em} #rise-reading-app .rise-err{color:#b91c1c}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  </style>
 
23
  <section class="rise-card">
24
  <h2>📖 RISE Reading Practice</h2>
25
- <p class="rise-muted">AI-generated passages · Whisper speech recognition · pronunciation feedback · hand gestures</p>
26
- <div class="rise-row"><button id="rise-start">Start practice</button><button id="rise-record" disabled>● Start recording</button><button id="rise-stop" disabled>Stop & evaluate</button><button id="rise-finish" class="danger" disabled>Finish session</button></div>
27
- <p id="rise-status" role="status">Ready to start.</p>
28
- <p id="rise-level" class="rise-muted">Level and part will appear here.</p>
29
- <div id="rise-passage">Press “Start practice” to generate your reading passage.</div>
30
- <div id="rise-actions" class="rise-row" style="margin-top:12px"></div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  </section>
32
- <section class="rise-card"><h3>Speech evaluation</h3><p id="rise-transcript">Your transcription will appear here.</p><div id="rise-metrics" class="rise-metrics"></div><p id="rise-feedback"></p><div id="rise-errors"></div></section>
33
- <section class="rise-card"><h3>Hand gesture control</h3><p class="rise-muted">Allow camera access, then capture a gesture: 1–5 fingers select a level, thumbs up restarts at Level 1, thumbs down ends the session.</p><div class="rise-row"><button id="rise-gesture" class="secondary" disabled>📷 Detect gesture</button><span id="rise-gesture-result"></span></div></section>
34
  `;
35
- const $ = id => root.querySelector(`#${id}`);
36
- let sessionId = null, recorder = null, chunks = [], startedAt = 0, stream = null;
37
- const setStatus = (text, isError=false) => { $("rise-status").textContent = text; $("rise-status").className = isError ? "rise-err" : ""; };
38
- const setBusy = busy => { ["rise-start","rise-record","rise-stop","rise-finish","rise-gesture"].forEach(id => $(id).disabled = busy || (id !== "rise-start" && !sessionId)); };
39
- async function api(path, options={}) {
40
- const res = await fetch(`${API}${path}`, options);
41
- const body = await res.json().catch(() => ({}));
42
- if (!res.ok) throw new Error(body.detail || `Request failed (${res.status})`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  return body;
44
  }
 
45
  function showPassage(data) {
46
- $("rise-level").textContent = `Level: ${data.level.replaceAll("_"," ")} · Part: ${data.part} · Pass mark: ${data.threshold || 80}%`;
47
- $("rise-passage").textContent = data.passage || "Session completed.";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  }
 
 
 
 
 
49
  $("rise-start").addEventListener("click", async () => {
 
 
 
 
 
 
 
 
50
  try {
51
- setBusy(true); setStatus("Starting session and preparing the first passage…");
52
- const data = await api("/api/session/start", {method:"POST", headers:{"Content-Type":"application/json"}, body:JSON.stringify({
53
- level: window.RISE_SELECTED_LEVEL || "beginner",
54
- part: "sentence"
55
- });
56
- sessionId = data.session_id; showPassage(data); setStatus(`Ready. Text model: ${data.model}`);
57
- $("rise-start").disabled = true; $("rise-record").disabled = false; $("rise-finish").disabled = false; $("rise-gesture").disabled = false;
58
- } catch(e) { setStatus(e.message, true); }
59
- finally { if (!sessionId) $("rise-start").disabled = false; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  });
 
 
 
 
 
61
  $("rise-record").addEventListener("click", async () => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  try {
63
- stream = await navigator.mediaDevices.getUserMedia({audio:{channelCount:1,echoCancellation:true,noiseSuppression:true}});
64
- const mime = MediaRecorder.isTypeSupported("audio/webm;codecs=opus") ? "audio/webm;codecs=opus" : "";
65
- recorder = mime ? new MediaRecorder(stream,{mimeType:mime}) : new MediaRecorder(stream);
66
- chunks = []; recorder.ondataavailable = e => { if(e.data.size) chunks.push(e.data); };
67
- recorder.start(150); startedAt = Date.now();
68
- $("rise-record").disabled = true; $("rise-stop").disabled = false; setStatus("Recording… read the passage aloud, then press Stop & evaluate.");
69
- } catch(e) { setStatus(`Could not start microphone: ${e.message}. Check browser microphone permission.`, true); }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  });
 
 
 
 
 
71
  $("rise-stop").addEventListener("click", async () => {
72
- if (!recorder || recorder.state === "inactive") return;
73
- $("rise-stop").disabled = true; setStatus("Uploading audio and evaluating speech with Whisper…");
74
- recorder.onstop = async () => {
75
- if (stream) stream.getTracks().forEach(t => t.stop());
76
- try {
77
- const blob = new Blob(chunks,{type:recorder.mimeType || "audio/webm"});
78
- const fd = new FormData(); fd.append("audio",blob,"reading.webm"); fd.append("elapsed_seconds",String((Date.now()-startedAt)/1000));
79
- const data = await api(`/api/session/${sessionId}/evaluate`,{method:"POST",body:fd});
80
- $("rise-transcript").textContent = `Heard: ${data.transcript}`;
81
- $("rise-metrics").innerHTML = [["Overall",data.metrics.overall+"%"],["Word accuracy",data.metrics.word_accuracy+"%"],["Pronunciation",data.metrics.pronunciation+"%"],["Completion",data.metrics.completion+"%"],["WER",data.wer+"%"],["Speaking rate",data.metrics.speaking_rate+" wpm"]].map(([k,v])=>`<div class="rise-metric"><span>${k}</span><b>${v}</b></div>`).join("");
82
- $("rise-feedback").textContent = `${data.passed ? "✅ Passed. " : "🔁 Try again. "}${data.message}`;
83
- $("rise-errors").innerHTML = "";
84
- for (const ev of data.events.filter(e=>e.type!=="correct")) {
85
- const line = document.createElement("div"); line.className="rise-row"; line.style.margin="6px 0";
86
- const label = document.createElement("span"); label.textContent = `${ev.word}: ${ev.type}${ev.heard ? ` (heard “${ev.heard}”)` : ""}`;
87
- line.appendChild(label);
88
- const btn = document.createElement("button"); btn.className="secondary"; btn.textContent=`🔊 Hear “${ev.word}”`; btn.addEventListener("click",()=>{const a=new Audio(`${API}/api/pronounce/${encodeURIComponent(ev.word)}`); a.play().catch(err=>setStatus(`Could not play pronunciation: ${err.message}`,true));}); line.appendChild(btn); $("rise-errors").appendChild(line);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  }
90
- if (data.completed) { setStatus(data.message); $("rise-passage").textContent="You completed all five levels. Finish session to see the summary."; $("rise-record").disabled=true; $("rise-gesture").disabled=true; }
91
- else { showPassage({level:data.next_level,part:data.next_part,passage:data.next_passage,threshold:data.threshold}); setStatus(data.message); $("rise-record").disabled=false; }
92
- } catch(e) { setStatus(e.message,true); $("rise-record").disabled=false; }
93
- finally { $("rise-stop").disabled=true; }
94
- };
95
- recorder.stop();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  });
 
 
 
 
 
97
  $("rise-finish").addEventListener("click", async () => {
98
- if (!sessionId) return;
 
 
 
 
 
 
 
 
 
 
99
  try {
100
- const data = await api(`/api/session/${sessionId}/finish`,{method:"POST"});
101
- setStatus(`Session finished. Attempts: ${data.attempts}; average overall: ${data.average_overall}%; average WER: ${data.average_wer}%.`);
102
- $("rise-feedback").textContent = JSON.stringify(data.text_generation,null,2);
103
- $("rise-record").disabled=true; $("rise-stop").disabled=true; $("rise-gesture").disabled=true; $("rise-finish").disabled=true;
104
- } catch(e) { setStatus(e.message,true); }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  });
 
 
 
 
 
106
  $("rise-gesture").addEventListener("click", async () => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  try {
108
- const cam = await navigator.mediaDevices.getUserMedia({video:{facingMode:"user"}});
109
- const video = document.createElement("video"); video.srcObject=cam; video.muted=true; await video.play();
110
- await new Promise(r=>setTimeout(r,500));
111
- const canvas=document.createElement("canvas"); canvas.width=video.videoWidth; canvas.height=video.videoHeight;
112
- canvas.getContext("2d").drawImage(video,0,0); cam.getTracks().forEach(t=>t.stop());
113
- const blob=await new Promise(resolve=>canvas.toBlob(resolve,"image/jpeg",0.85));
114
- const fd=new FormData(); fd.append("image",blob,"gesture.jpg");
115
- const data=await api(`/api/session/${sessionId}/gesture`,{method:"POST",body:fd});
116
- $("rise-gesture-result").textContent=`Detected: ${data.gesture} · Action: ${data.action}`;
117
- if (data.passage) showPassage({level:data.level,part:data.part,passage:data.passage,threshold:80});
118
- if (data.ended) { setStatus("Session ended by thumbs-down gesture."); $("rise-record").disabled=true; $("rise-gesture").disabled=true; }
119
- else if (data.action!=="none") setStatus(`Gesture applied: ${data.action}.`);
120
- } catch(e) { setStatus(`Camera/gesture error: ${e.message}`,true); }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  });
122
- api("/api/health").then(h=>setStatus(`Backend connected. Models: Whisper ${h.models.whisper?"ready":"loading/unavailable"}, text ${h.models.text_model||"loading/unavailable"}, gestures ${h.models.gesture?"ready":"unavailable"}.`)).catch(()=>setStatus("Backend not reachable yet. Check the API URL and ensure Colab server/tunnel are running.",true));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  })();
 
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
+
230
+ const setStatus = (message, isError = false) => {
231
+ const status = $("rise-status");
232
+ status.textContent = message;
233
+ status.className = isError ? "rise-err" : "";
234
+ };
235
+
236
+ const stopAudioTracks = () => {
237
+ if (stream) {
238
+ stream.getTracks().forEach((track) => track.stop());
239
+ stream = null;
240
+ }
241
+ };
242
+
243
+ const setSessionControls = (active) => {
244
+ $("rise-record").disabled = !active || recordingInProgress;
245
+ $("rise-stop").disabled = !active || !recordingInProgress;
246
+ $("rise-finish").disabled = !active || evaluationInProgress;
247
+ $("rise-gesture").disabled = !active || evaluationInProgress;
248
+ };
249
+
250
+ async function api(path, options = {}) {
251
+ const response = await fetch(`${API}${path}`, options);
252
+
253
+ const body = await response.json().catch(() => ({}));
254
+
255
+ if (!response.ok) {
256
+ throw new Error(
257
+ body.detail || `Backend request failed (${response.status})`
258
+ );
259
+ }
260
+
261
  return body;
262
  }
263
+
264
  function showPassage(data) {
265
+ const level = String(data.level || "beginner")
266
+ .replaceAll("_", " ");
267
+
268
+ $("rise-level").textContent =
269
+ `Level: ${level} · Part: ${data.part || "sentence"} · ` +
270
+ `Pass mark: ${data.threshold ?? 80}%`;
271
+
272
+ $("rise-passage").textContent =
273
+ data.passage || "No passage was returned by the backend.";
274
+ }
275
+
276
+ function formatMetric(value, suffix = "%") {
277
+ if (value === undefined || value === null) {
278
+ return "N/A";
279
+ }
280
+
281
+ return `${value}${suffix}`;
282
  }
283
+
284
+ // --------------------------------------------------
285
+ // START READING SESSION
286
+ // --------------------------------------------------
287
+
288
  $("rise-start").addEventListener("click", async () => {
289
+ if (sessionId) {
290
+ setStatus("A session is already active.");
291
+ return;
292
+ }
293
+
294
+ $("rise-start").disabled = true;
295
+ setStatus("Starting your session and generating a passage...");
296
+
297
  try {
298
+ /*
299
+ * practice.html should set RISE_SELECTED_LEVEL.
300
+ * If it is not set, the backend starts at Beginner.
301
+ */
302
+ const selectedLevel =
303
+ window.RISE_SELECTED_LEVEL || "beginner";
304
+
305
+ const data = await api("/api/session/start", {
306
+ method: "POST",
307
+ headers: {
308
+ "Content-Type": "application/json"
309
+ },
310
+ body: JSON.stringify({
311
+ level: selectedLevel,
312
+ part: "sentence"
313
+ })
314
+ });
315
+
316
+ if (!data.session_id) {
317
+ throw new Error("The backend did not return a session ID.");
318
+ }
319
+
320
+ sessionId = data.session_id;
321
+
322
+ showPassage(data);
323
+
324
+ $("rise-transcript").textContent =
325
+ "Your transcription will appear here.";
326
+
327
+ $("rise-metrics").innerHTML = "";
328
+ $("rise-feedback").textContent = "";
329
+ $("rise-errors").innerHTML = "";
330
+
331
+ setStatus(
332
+ `Your session is ready. Text model: ${data.model || "loaded"}`
333
+ );
334
+
335
+ setSessionControls(true);
336
+
337
+ $("rise-start").disabled = true;
338
+
339
+ } catch (error) {
340
+ console.error("RISE session-start error:", error);
341
+
342
+ setStatus(
343
+ `Could not start the session: ${error.message}`,
344
+ true
345
+ );
346
+
347
+ $("rise-start").disabled = false;
348
+ }
349
  });
350
+
351
+ // --------------------------------------------------
352
+ // START AUDIO RECORDING
353
+ // --------------------------------------------------
354
+
355
  $("rise-record").addEventListener("click", async () => {
356
+ if (!sessionId || recordingInProgress || evaluationInProgress) {
357
+ return;
358
+ }
359
+
360
+ if (!navigator.mediaDevices?.getUserMedia ||
361
+ typeof MediaRecorder === "undefined") {
362
+ setStatus(
363
+ "Audio recording is not supported by this browser. " +
364
+ "Use a recent browser over HTTPS.",
365
+ true
366
+ );
367
+ return;
368
+ }
369
+
370
  try {
371
+ stream = await navigator.mediaDevices.getUserMedia({
372
+ audio: {
373
+ channelCount: 1,
374
+ echoCancellation: true,
375
+ noiseSuppression: true
376
+ }
377
+ });
378
+
379
+ let mimeType = "";
380
+
381
+ if (MediaRecorder.isTypeSupported("audio/webm;codecs=opus")) {
382
+ mimeType = "audio/webm;codecs=opus";
383
+ } else if (MediaRecorder.isTypeSupported("audio/webm")) {
384
+ mimeType = "audio/webm";
385
+ }
386
+
387
+ recorder = mimeType
388
+ ? new MediaRecorder(stream, { mimeType })
389
+ : new MediaRecorder(stream);
390
+
391
+ chunks = [];
392
+
393
+ recorder.addEventListener("dataavailable", (event) => {
394
+ if (event.data && event.data.size > 0) {
395
+ chunks.push(event.data);
396
+ }
397
+ });
398
+
399
+ recorder.start(150);
400
+
401
+ startedAt = Date.now();
402
+ recordingInProgress = true;
403
+
404
+ setSessionControls(true);
405
+
406
+ setStatus(
407
+ "Recording... Read the displayed passage aloud, " +
408
+ "then press Stop & evaluate."
409
+ );
410
+
411
+ } catch (error) {
412
+ console.error("RISE recording error:", error);
413
+
414
+ stopAudioTracks();
415
+
416
+ recordingInProgress = false;
417
+ setSessionControls(Boolean(sessionId));
418
+
419
+ setStatus(
420
+ `Could not start the microphone: ${error.message}. ` +
421
+ "Check your browser's microphone permission.",
422
+ true
423
+ );
424
+ }
425
  });
426
+
427
+ // --------------------------------------------------
428
+ // STOP RECORDING AND EVALUATE SPEECH
429
+ // --------------------------------------------------
430
+
431
  $("rise-stop").addEventListener("click", async () => {
432
+ if (!recorder ||
433
+ recorder.state === "inactive" ||
434
+ evaluationInProgress) {
435
+ return;
436
+ }
437
+
438
+ evaluationInProgress = true;
439
+ setSessionControls(Boolean(sessionId));
440
+
441
+ setStatus("Uploading audio and evaluating your reading with Whisper...");
442
+
443
+ const activeRecorder = recorder;
444
+
445
+ try {
446
+ const audioBlob = await new Promise((resolve, reject) => {
447
+ activeRecorder.addEventListener("stop", () => {
448
+ if (chunks.length === 0) {
449
+ reject(new Error("No audio was recorded. Please try again."));
450
+ return;
451
+ }
452
+
453
+ resolve(
454
+ new Blob(chunks, {
455
+ type: activeRecorder.mimeType || "audio/webm"
456
+ })
457
+ );
458
+ }, { once: true });
459
+
460
+ activeRecorder.stop();
461
+ });
462
+
463
+ stopAudioTracks();
464
+
465
+ recordingInProgress = false;
466
+
467
+ const elapsedSeconds = (Date.now() - startedAt) / 1000;
468
+
469
+ const formData = new FormData();
470
+
471
+ formData.append("audio", audioBlob, "reading.webm");
472
+ formData.append(
473
+ "elapsed_seconds",
474
+ String(elapsedSeconds)
475
+ );
476
+
477
+ const data = await api(
478
+ `/api/session/${encodeURIComponent(sessionId)}/evaluate`,
479
+ {
480
+ method: "POST",
481
+ body: formData
482
  }
483
+ );
484
+
485
+ // Show the speech recognition result.
486
+ $("rise-transcript").textContent =
487
+ `Heard: ${data.transcript || "(No speech recognized)"}`;
488
+
489
+ // Show evaluation metrics.
490
+ const metrics = data.metrics || {};
491
+
492
+ const metricItems = [
493
+ ["Overall score", formatMetric(metrics.overall)],
494
+ ["Word accuracy", formatMetric(metrics.word_accuracy)],
495
+ ["Pronunciation", formatMetric(metrics.pronunciation)],
496
+ ["Completion", formatMetric(metrics.completion)],
497
+ ["Word error rate", formatMetric(data.wer)],
498
+ ["Speaking rate", formatMetric(metrics.speaking_rate, " wpm")]
499
+ ];
500
+
501
+ $("rise-metrics").replaceChildren();
502
+
503
+ metricItems.forEach(([label, value]) => {
504
+ const card = document.createElement("div");
505
+ card.className = "rise-metric";
506
+
507
+ const name = document.createElement("span");
508
+ name.textContent = label;
509
+
510
+ const result = document.createElement("b");
511
+ result.textContent = value;
512
+
513
+ card.append(name, result);
514
+ $("rise-metrics").appendChild(card);
515
+ });
516
+
517
+ const passed = Boolean(data.passed);
518
+
519
+ $("rise-feedback").textContent =
520
+ `${passed ? "✅ Passed! " : "🔁 Try again. "}` +
521
+ (data.message || "");
522
+
523
+ $("rise-feedback").className =
524
+ passed ? "rise-success" : "";
525
+
526
+ // Show detected words and pronunciation playback controls.
527
+ $("rise-errors").replaceChildren();
528
+
529
+ const events = Array.isArray(data.events) ? data.events : [];
530
+
531
+ events
532
+ .filter((event) => event.type !== "correct")
533
+ .forEach((event) => {
534
+ const row = document.createElement("div");
535
+ row.className = "rise-row";
536
+ row.style.margin = "6px 0";
537
+
538
+ const label = document.createElement("span");
539
+
540
+ label.textContent =
541
+ `${event.word || "Word"}: ${event.type || "mismatch"}` +
542
+ (event.heard ? ` (heard "${event.heard}")` : "");
543
+
544
+ const button = document.createElement("button");
545
+ button.className = "secondary";
546
+ button.textContent =
547
+ `🔊 Hear "${event.word || "word"}"`;
548
+
549
+ button.addEventListener("click", async () => {
550
+ try {
551
+ const audio = new Audio(
552
+ `${API}/api/pronounce/${encodeURIComponent(
553
+ event.word || ""
554
+ )}`
555
+ );
556
+
557
+ await audio.play();
558
+
559
+ } catch (error) {
560
+ setStatus(
561
+ `Could not play pronunciation: ${error.message}`,
562
+ true
563
+ );
564
+ }
565
+ });
566
+
567
+ row.append(label, button);
568
+ $("rise-errors").appendChild(row);
569
+ });
570
+
571
+ // Handle progression returned by the backend.
572
+ if (data.completed) {
573
+ setStatus(
574
+ data.message ||
575
+ "Congratulations! You completed the reading levels."
576
+ );
577
+
578
+ $("rise-passage").textContent =
579
+ "Reading session completed. Finish the session to view your summary.";
580
+
581
+ sessionId = null;
582
+ $("rise-start").disabled = false;
583
+ setSessionControls(false);
584
+
585
+ } else if (data.next_passage) {
586
+ showPassage({
587
+ level: data.next_level,
588
+ part: data.next_part,
589
+ passage: data.next_passage,
590
+ threshold: data.threshold
591
+ });
592
+
593
+ setStatus(
594
+ data.message ||
595
+ (passed
596
+ ? "Great work! Continue with the next activity."
597
+ : "You need to try this activity again.")
598
+ );
599
+
600
+ // The backend determines whether the next passage changes.
601
+ setSessionControls(true);
602
+
603
+ } else {
604
+ setStatus(
605
+ data.message ||
606
+ "Evaluation completed, but the backend did not return a next passage.",
607
+ !passed
608
+ );
609
+
610
+ setSessionControls(true);
611
+ }
612
+
613
+ } catch (error) {
614
+ console.error("RISE evaluation error:", error);
615
+
616
+ stopAudioTracks();
617
+ recordingInProgress = false;
618
+
619
+ setStatus(
620
+ `Could not evaluate the recording: ${error.message}`,
621
+ true
622
+ );
623
+
624
+ setSessionControls(Boolean(sessionId));
625
+
626
+ } finally {
627
+ evaluationInProgress = false;
628
+
629
+ if (sessionId) {
630
+ setSessionControls(true);
631
+ }
632
+ }
633
  });
634
+
635
+ // --------------------------------------------------
636
+ // FINISH SESSION AND DISPLAY SUMMARY
637
+ // --------------------------------------------------
638
+
639
  $("rise-finish").addEventListener("click", async () => {
640
+ if (!sessionId || recordingInProgress || evaluationInProgress) {
641
+ return;
642
+ }
643
+
644
+ const finishedSessionId = sessionId;
645
+
646
+ $("rise-finish").disabled = true;
647
+ $("rise-gesture").disabled = true;
648
+
649
+ setStatus("Preparing your session summary...");
650
+
651
  try {
652
+ const data = await api(
653
+ `/api/session/${encodeURIComponent(finishedSessionId)}/finish`,
654
+ {
655
+ method: "POST"
656
+ }
657
+ );
658
+
659
+ setStatus(
660
+ `Session finished. Attempts: ${data.attempts ?? "N/A"}; ` +
661
+ `average overall score: ${data.average_overall ?? "N/A"}%; ` +
662
+ `average word error rate: ${data.average_wer ?? "N/A"}%.`
663
+ );
664
+
665
+ $("rise-feedback").textContent =
666
+ JSON.stringify(data.text_generation ?? {}, null, 2);
667
+
668
+ sessionId = null;
669
+
670
+ $("rise-start").disabled = false;
671
+ setSessionControls(false);
672
+
673
+ } catch (error) {
674
+ console.error("RISE finish-session error:", error);
675
+
676
+ setStatus(
677
+ `Could not finish the session: ${error.message}`,
678
+ true
679
+ );
680
+
681
+ setSessionControls(Boolean(sessionId));
682
+ }
683
  });
684
+
685
+ // --------------------------------------------------
686
+ // HAND GESTURE DETECTION
687
+ // --------------------------------------------------
688
+
689
  $("rise-gesture").addEventListener("click", async () => {
690
+ if (!sessionId || evaluationInProgress || recordingInProgress) {
691
+ return;
692
+ }
693
+
694
+ if (!navigator.mediaDevices?.getUserMedia) {
695
+ setStatus(
696
+ "Camera access is not supported by this browser.",
697
+ true
698
+ );
699
+ return;
700
+ }
701
+
702
+ let cameraStream = null;
703
+
704
+ $("rise-gesture").disabled = true;
705
+ $("rise-gesture-result").textContent = "Opening camera...";
706
+
707
  try {
708
+ cameraStream = await navigator.mediaDevices.getUserMedia({
709
+ video: {
710
+ facingMode: "user"
711
+ }
712
+ });
713
+
714
+ const video = document.createElement("video");
715
+
716
+ video.muted = true;
717
+ video.playsInline = true;
718
+ video.srcObject = cameraStream;
719
+
720
+ await video.play();
721
+
722
+ // Allow the camera to produce a frame.
723
+ await new Promise((resolve) => setTimeout(resolve, 700));
724
+
725
+ if (!video.videoWidth || !video.videoHeight) {
726
+ throw new Error("The camera did not provide a usable image.");
727
+ }
728
+
729
+ const canvas = document.createElement("canvas");
730
+
731
+ canvas.width = video.videoWidth;
732
+ canvas.height = video.videoHeight;
733
+
734
+ const context = canvas.getContext("2d");
735
+
736
+ if (!context) {
737
+ throw new Error("Could not capture the camera image.");
738
+ }
739
+
740
+ context.drawImage(video, 0, 0);
741
+
742
+ const imageBlob = await new Promise((resolve, reject) => {
743
+ canvas.toBlob(
744
+ (blob) => {
745
+ if (blob) {
746
+ resolve(blob);
747
+ } else {
748
+ reject(new Error("Could not encode the camera image."));
749
+ }
750
+ },
751
+ "image/jpeg",
752
+ 0.85
753
+ );
754
+ });
755
+
756
+ cameraStream.getTracks().forEach((track) => track.stop());
757
+ cameraStream = null;
758
+
759
+ const formData = new FormData();
760
+ formData.append("image", imageBlob, "gesture.jpg");
761
+
762
+ const data = await api(
763
+ `/api/session/${encodeURIComponent(sessionId)}/gesture`,
764
+ {
765
+ method: "POST",
766
+ body: formData
767
+ }
768
+ );
769
+
770
+ $("rise-gesture-result").textContent =
771
+ `Detected: ${data.gesture || "unknown"} · ` +
772
+ `Action: ${data.action || "none"}`;
773
+
774
+ if (data.passage) {
775
+ showPassage({
776
+ level: data.level,
777
+ part: data.part,
778
+ passage: data.passage,
779
+ threshold: data.threshold || 80
780
+ });
781
+ }
782
+
783
+ if (data.ended) {
784
+ setStatus("Session ended by the hand gesture.");
785
+
786
+ sessionId = null;
787
+ $("rise-start").disabled = false;
788
+ setSessionControls(false);
789
+
790
+ } else if (data.action && data.action !== "none") {
791
+ setStatus(`Gesture applied: ${data.action}.`);
792
+ }
793
+
794
+ } catch (error) {
795
+ console.error("RISE gesture error:", error);
796
+
797
+ setStatus(
798
+ `Camera or gesture error: ${error.message}`,
799
+ true
800
+ );
801
+
802
+ $("rise-gesture-result").textContent = "Gesture detection failed.";
803
+
804
+ } finally {
805
+ if (cameraStream) {
806
+ cameraStream.getTracks().forEach((track) => track.stop());
807
+ }
808
+
809
+ $("rise-gesture").disabled = !sessionId;
810
+ }
811
  });
812
+
813
+ // --------------------------------------------------
814
+ // CHECK BACKEND CONNECTIVITY
815
+ // --------------------------------------------------
816
+
817
+ api("/api/health")
818
+ .then((health) => {
819
+ if (!health.ok) {
820
+ throw new Error("The backend health check did not return OK.");
821
+ }
822
+
823
+ const models = health.models || {};
824
+
825
+ setStatus(
826
+ `Backend connected. Whisper: ${
827
+ models.whisper ? "ready" : "unavailable"
828
+ }; text generation: ${
829
+ models.text_generation ? "ready" : "unavailable"
830
+ }; gestures: ${
831
+ models.gesture ? "ready" : "unavailable"
832
+ }.`
833
+ );
834
+ })
835
+ .catch((error) => {
836
+ console.error("RISE health-check error:", error);
837
+
838
+ setStatus(
839
+ "Backend not reachable. Check the API URL and make sure " +
840
+ "the Colab server and Cloudflare tunnel are running.",
841
+ true
842
+ );
843
+ });
844
+
845
  })();