Spaces:
Running
Running
Update rise-frontend.js
Browse files- rise-frontend.js +813 -91
rise-frontend.js
CHANGED
|
@@ -1,123 +1,845 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
(() => {
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
const root = document.getElementById("rise-reading-app");
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
if (!API) {
|
| 7 |
-
root.innerHTML =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
return;
|
| 9 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
root.innerHTML = `
|
| 11 |
<style>
|
| 12 |
-
#rise-reading-app
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
#rise-reading-app .rise-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
</style>
|
|
|
|
| 23 |
<section class="rise-card">
|
| 24 |
<h2>📖 RISE Reading Practice</h2>
|
| 25 |
-
|
| 26 |
-
<
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
<
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return body;
|
| 44 |
}
|
|
|
|
| 45 |
function showPassage(data) {
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
$("rise-start").addEventListener("click", async () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
try {
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
$("rise-record").addEventListener("click", async () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
try {
|
| 63 |
-
stream = await navigator.mediaDevices.getUserMedia({
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
| 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 & 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 |
})();
|