Spaces:
Running
Running
Update rise-frontend.js
Browse files- rise-frontend.js +387 -137
rise-frontend.js
CHANGED
|
@@ -1,196 +1,446 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 (
|
| 12 |
-
console.warn('
|
| 13 |
return Array.isArray(history) ? history : [];
|
| 14 |
}
|
| 15 |
};
|
| 16 |
|
| 17 |
-
const renderHistory =
|
| 18 |
-
const
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
};
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
};
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
};
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 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>
|
| 43 |
-
<div class="rise-metric"><small>Speaking rate</small><strong>${Number(m.speaking_rate||0).toFixed(1)}
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
};
|
| 48 |
-
const stopTracks = () => { if(mediaStream){mediaStream.getTracks().forEach(t=>t.stop());mediaStream=null;} };
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
});
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
});
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
try {
|
| 61 |
-
mediaStream=await navigator.mediaDevices.getUserMedia({audio:{
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
recorder
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
try {
|
| 72 |
-
setStatus('Sending audio to Whisper
|
| 73 |
-
const data=await request('/api/evaluate',{method:'POST',body:fd});
|
| 74 |
renderSession(data);
|
| 75 |
-
$('#rise-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
|
| 85 |
}
|
| 86 |
});
|
| 87 |
|
| 88 |
-
$('#rise-stop'
|
| 89 |
-
if(recorder && recorder.state!=='inactive')recorder.stop();
|
| 90 |
});
|
| 91 |
|
| 92 |
-
$('#rise-finish'
|
| 93 |
-
if(!session||busy)return;
|
| 94 |
-
const fd=new FormData();
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
| 98 |
recordPracticeDay();
|
| 99 |
renderMetrics(data);
|
| 100 |
-
renderHistory(
|
| 101 |
-
setStatus(data.message,data.passed?'success':'');
|
| 102 |
-
if(data.status==='retry_decision'){
|
| 103 |
-
$('#rise-decision'
|
| 104 |
-
$('#rise-decision-text'
|
| 105 |
-
$('#rise-record'
|
| 106 |
-
} else if(data.status==='completed_all'){
|
| 107 |
-
session={...session,session_ended:true};
|
| 108 |
-
$('#rise-passage'
|
| 109 |
-
$('#rise-record'
|
| 110 |
-
$('#rise-finish'
|
| 111 |
} else {
|
| 112 |
renderSession(data);
|
| 113 |
-
$('#rise-events'
|
| 114 |
-
$('#rise-heard'
|
| 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',
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
$('#rise-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
renderSession(data);
|
| 133 |
-
$('#rise-results'
|
| 134 |
-
$('#rise-events'
|
| 135 |
-
setStatus(data.message,'success');
|
| 136 |
}
|
| 137 |
-
}catch(e){setStatus(e.message,'error');}
|
| 138 |
};
|
| 139 |
|
| 140 |
-
$('#rise-retry'
|
| 141 |
-
$('#rise-give-up'
|
| 142 |
-
|
| 143 |
-
$('#rise-pronounce'
|
| 144 |
-
const word=$('#rise-pronounce-word'
|
| 145 |
-
if(!word){setStatus('Enter a word first.','error');return;}
|
| 146 |
-
try{
|
| 147 |
-
const data=await request('/api/pronounce',
|
| 148 |
-
if(data.audio_base64)
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
| 152 |
});
|
| 153 |
|
| 154 |
-
$('#rise-gesture'
|
| 155 |
-
if(!session||busy)return;
|
| 156 |
let stream;
|
| 157 |
-
try{
|
| 158 |
-
setStatus('Opening camera for
|
| 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
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
| 177 |
}
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 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'
|
| 194 |
-
.catch(()=>{$('#rise-connection'
|
| 195 |
}
|
| 196 |
-
})();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
|
| 3 |
+
code = r'''/* RISE frontend adapter */
|
| 4 |
+
(() => {
|
| 5 |
+
const $ = (s, root = document) => root.querySelector(s);
|
| 6 |
+
const root = document.getElementById('rise-reader') ||
|
| 7 |
+
document.getElementById('rise-reading-app');
|
| 8 |
+
|
| 9 |
+
if (!root) {
|
| 10 |
+
console.error('RISE: Could not find #rise-reader or #rise-reading-app.');
|
| 11 |
+
return;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
root.id = 'rise-reader';
|
| 15 |
+
|
| 16 |
+
const levels = ['beginner', 'elementary', 'intermediate', 'upper_intermediate', 'advanced'];
|
| 17 |
+
const levelLabels = ['Beginner', 'Elementary', 'Intermediate', 'Upper Intermediate', 'Advanced'];
|
| 18 |
+
let apiBase = (window.RISE_API_BASE || localStorage.getItem('riseApiBase') || '').replace(/\/$/, '');
|
| 19 |
+
let session = null, mediaStream = null, recorder = null, chunks = [], busy = false;
|
| 20 |
+
|
| 21 |
+
const esc = s => String(s ?? '').replace(/[&<>"']/g, c => ({
|
| 22 |
+
'&':'&', '<':'<', '>':'>', '"':'"', "'":'''
|
| 23 |
+
}[c]));
|
| 24 |
+
const labelLevel = s => levelLabels[levels.indexOf(s)] || s;
|
| 25 |
+
const labelPart = s => s ? s[0].toUpperCase() + s.slice(1) : '';
|
| 26 |
+
|
| 27 |
+
root.innerHTML = `
|
| 28 |
+
<style>
|
| 29 |
+
#rise-reader{font-family:inherit;color:var(--body-text-color,#172033);margin:20px auto;max-width:1000px}
|
| 30 |
+
#rise-reader *{box-sizing:border-box}
|
| 31 |
+
.rise-shell{border:1px solid #dce3eb;border-radius:18px;padding:24px;background:var(--background-fill-primary,#fff)}
|
| 32 |
+
.rise-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px;margin:18px 0}
|
| 33 |
+
.rise-field label{display:block;font-weight:650;margin-bottom:6px}
|
| 34 |
+
.rise-field input,.rise-field select{width:100%;padding:11px;border:1px solid #cbd5e1;border-radius:9px;font:inherit}
|
| 35 |
+
.rise-actions{display:flex;flex-wrap:wrap;gap:9px;margin:15px 0}
|
| 36 |
+
.rise-btn{padding:10px 14px;border:1px solid #cbd5e1;border-radius:9px;cursor:pointer;font:inherit;font-weight:600}
|
| 37 |
+
.rise-primary{background:#5b4be0;color:white;border:0}
|
| 38 |
+
.rise-btn:disabled{opacity:.5;cursor:not-allowed}
|
| 39 |
+
.rise-passage{white-space:pre-wrap;font-size:1.15rem;line-height:1.9;padding:18px;background:#f7f8fc;border-radius:12px;margin:16px 0;color:#172033}
|
| 40 |
+
.rise-status{min-height:24px;margin:12px 0}
|
| 41 |
+
.rise-error{color:#b91c1c}.rise-success{color:#047857}
|
| 42 |
+
.rise-pill{display:inline-block;padding:5px 9px;margin:3px;border-radius:20px;background:#eef2ff}
|
| 43 |
+
.rise-results{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:10px}
|
| 44 |
+
.rise-metric{border:1px solid #dce3eb;border-radius:10px;padding:12px}
|
| 45 |
+
.rise-metric strong{display:block;font-size:1.2rem}
|
| 46 |
+
.rise-events{display:flex;flex-wrap:wrap;gap:6px;margin:12px 0}
|
| 47 |
+
.rise-word{padding:5px 8px;border-radius:6px;background:#dcfce7;color:#166534}
|
| 48 |
+
.rise-hidden{display:none!important}
|
| 49 |
+
.rise-history{width:100%;border-collapse:collapse}
|
| 50 |
+
.rise-history th,.rise-history td{text-align:left;padding:8px;border-bottom:1px solid #ddd}
|
| 51 |
+
@media(max-width:600px){.rise-grid{grid-template-columns:1fr}.rise-results{grid-template-columns:repeat(2,minmax(0,1fr))}}
|
| 52 |
+
</style>
|
| 53 |
+
|
| 54 |
+
<div class="rise-shell">
|
| 55 |
+
<h2>RISE Reading Practice</h2>
|
| 56 |
+
<p>Read the passage aloud. RISE evaluates your speech and tracks your progress.</p>
|
| 57 |
+
<div><span class="rise-pill" id="rise-connection">Backend not checked</span></div>
|
| 58 |
+
|
| 59 |
+
<div class="rise-grid">
|
| 60 |
+
<div class="rise-field">
|
| 61 |
+
<label for="rise-api-url">Colab API URL</label>
|
| 62 |
+
<input id="rise-api-url" type="url" placeholder="https://your-tunnel.trycloudflare.com">
|
| 63 |
+
</div>
|
| 64 |
+
<div class="rise-field">
|
| 65 |
+
<label for="rise-level">Reading level</label>
|
| 66 |
+
<select id="rise-level">
|
| 67 |
+
${levels.map((v,i)=>`<option value="${v}">${levelLabels[i]}</option>`).join('')}
|
| 68 |
+
</select>
|
| 69 |
+
</div>
|
| 70 |
+
</div>
|
| 71 |
+
|
| 72 |
+
<div class="rise-actions">
|
| 73 |
+
<button class="rise-btn" id="rise-check">Check connection</button>
|
| 74 |
+
<button class="rise-btn rise-primary" id="rise-start">Start session</button>
|
| 75 |
+
</div>
|
| 76 |
+
<div id="rise-status" class="rise-status" role="status">Check your backend connection before starting.</div>
|
| 77 |
+
|
| 78 |
+
<div id="rise-workspace" class="rise-hidden">
|
| 79 |
+
<div>
|
| 80 |
+
<span class="rise-pill" id="rise-level-pill"></span>
|
| 81 |
+
<span class="rise-pill" id="rise-part-pill"></span>
|
| 82 |
+
<span class="rise-pill">Pass mark: 80%</span>
|
| 83 |
+
</div>
|
| 84 |
+
<div id="rise-passage" class="rise-passage"></div>
|
| 85 |
+
<div id="rise-progress-text">0 words evaluated</div>
|
| 86 |
+
|
| 87 |
+
<div class="rise-actions">
|
| 88 |
+
<button class="rise-btn rise-primary" id="rise-record">Start recording</button>
|
| 89 |
+
<button class="rise-btn" id="rise-stop" disabled>Stop & evaluate</button>
|
| 90 |
+
<button class="rise-btn" id="rise-finish">Finish passage</button>
|
| 91 |
+
<button class="rise-btn" id="rise-gesture">Check gesture</button>
|
| 92 |
+
</div>
|
| 93 |
+
|
| 94 |
+
<p id="rise-heard"></p>
|
| 95 |
+
<div id="rise-events" class="rise-events"></div>
|
| 96 |
+
<div class="rise-field">
|
| 97 |
+
<label for="rise-pronounce-word">Pronounce a word</label>
|
| 98 |
+
<input id="rise-pronounce-word" placeholder="Enter a word">
|
| 99 |
+
<button class="rise-btn" id="rise-pronounce">Play pronunciation</button>
|
| 100 |
+
</div>
|
| 101 |
+
<div id="rise-results"></div>
|
| 102 |
+
<div id="rise-decision" class="rise-hidden">
|
| 103 |
+
<p id="rise-decision-text"></p>
|
| 104 |
+
<button class="rise-btn rise-primary" id="rise-retry">Try again</button>
|
| 105 |
+
<button class="rise-btn" id="rise-give-up">Stop for now</button>
|
| 106 |
+
</div>
|
| 107 |
+
<div id="rise-history"></div>
|
| 108 |
+
</div>
|
| 109 |
+
</div>`;
|
| 110 |
+
|
| 111 |
+
$('#rise-api-url').value = apiBase;
|
| 112 |
+
if (levels.includes(window.RISE_SELECTED_LEVEL)) {
|
| 113 |
+
$('#rise-level').value = window.RISE_SELECTED_LEVEL;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
const setStatus = (msg, type='') => {
|
| 117 |
+
const el = $('#rise-status');
|
| 118 |
+
el.textContent = msg;
|
| 119 |
+
el.className = 'rise-status ' + (type ? 'rise-' + type : '');
|
| 120 |
+
};
|
| 121 |
+
|
| 122 |
+
const saveApi = () => {
|
| 123 |
+
apiBase = $('#rise-api-url').value.trim().replace(/\/$/, '');
|
| 124 |
+
if (apiBase) localStorage.setItem('riseApiBase', apiBase);
|
| 125 |
+
};
|
| 126 |
+
|
| 127 |
+
const request = async (path, options={}) => {
|
| 128 |
+
if (!apiBase) throw new Error('Enter your current Colab Cloudflare URL.');
|
| 129 |
+
let response;
|
| 130 |
+
try {
|
| 131 |
+
response = await fetch(apiBase + path, options);
|
| 132 |
+
} catch (e) {
|
| 133 |
+
throw new Error('Backend connection failed. Check Colab and your current tunnel URL.');
|
| 134 |
+
}
|
| 135 |
+
const data = await response.json().catch(() => ({}));
|
| 136 |
+
if (!response.ok) throw new Error(data.detail || `HTTP ${response.status}`);
|
| 137 |
+
return data;
|
| 138 |
+
};
|
| 139 |
+
|
| 140 |
+
const postJSON = body => ({
|
| 141 |
+
method:'POST',
|
| 142 |
+
headers:{'Content-Type':'application/json'},
|
| 143 |
+
body:JSON.stringify(body)
|
| 144 |
+
});
|
| 145 |
+
|
| 146 |
+
const renderSession = data => {
|
| 147 |
+
session = {...(session || {}), ...data};
|
| 148 |
+
$('#rise-workspace').classList.remove('rise-hidden');
|
| 149 |
+
$('#rise-level-pill').textContent = 'Level: ' + labelLevel(session.level);
|
| 150 |
+
$('#rise-part-pill').textContent = 'Part: ' + labelPart(session.part);
|
| 151 |
+
$('#rise-passage').textContent = session.passage || 'No passage returned.';
|
| 152 |
+
$('#rise-progress-text').textContent =
|
| 153 |
+
`${session.cursor || 0} / ${session.total_words || 0} words evaluated (${session.progress_percent || 0}%)`;
|
| 154 |
+
$('#rise-record').disabled = busy || !session.passage || !!session.session_ended ||
|
| 155 |
+
(session.total_words != null && session.cursor >= session.total_words);
|
| 156 |
+
$('#rise-finish').disabled = busy || !session.passage || !!session.session_ended;
|
| 157 |
+
};
|
| 158 |
+
|
| 159 |
+
const saveCumulativeHistory = history => {
|
| 160 |
+
try {
|
| 161 |
+
const old = JSON.parse(localStorage.getItem('riseHistory') || '[]');
|
| 162 |
+
const combined = [...(Array.isArray(old) ? old : []), ...(Array.isArray(history) ? history : [])];
|
| 163 |
+
const seen = new Set();
|
| 164 |
+
const unique = combined.filter(r => {
|
| 165 |
+
const key = r.timestamp != null
|
| 166 |
+
? 'timestamp:' + r.timestamp
|
| 167 |
+
: `${r.level}|${r.part}|${r.overall}|${r.word_accuracy}|${r.wer}`;
|
| 168 |
if (seen.has(key)) return false;
|
| 169 |
seen.add(key);
|
| 170 |
return true;
|
| 171 |
});
|
| 172 |
localStorage.setItem('riseHistory', JSON.stringify(unique));
|
| 173 |
return unique;
|
| 174 |
+
} catch (e) {
|
| 175 |
+
console.warn('History storage failed', e);
|
| 176 |
return Array.isArray(history) ? history : [];
|
| 177 |
}
|
| 178 |
};
|
| 179 |
|
| 180 |
+
const renderHistory = history => {
|
| 181 |
+
const records = saveCumulativeHistory(history);
|
| 182 |
+
$('#rise-history').innerHTML = records.length ? `
|
| 183 |
+
<h3>Session history</h3><table class="rise-history">
|
| 184 |
+
<thead><tr><th>Level / part</th><th>Score</th><th>Accuracy</th><th>WER</th></tr></thead>
|
| 185 |
+
<tbody>${records.slice().reverse().slice(0,20).map(r=>`
|
| 186 |
+
<tr><td>${esc(labelLevel(r.level))} / ${esc(labelPart(r.part))}</td>
|
| 187 |
+
<td>${Number(r.overall||0).toFixed(1)}%</td>
|
| 188 |
+
<td>${Number(r.word_accuracy||0).toFixed(1)}%</td>
|
| 189 |
+
<td>${Number(r.wer||0).toFixed(1)}%</td></tr>`).join('')}
|
| 190 |
+
</tbody></table>` : '';
|
| 191 |
};
|
| 192 |
+
|
| 193 |
+
const playPronunciations = async items => {
|
| 194 |
+
for (const item of items || []) {
|
| 195 |
+
if (!item.audio_base64) continue;
|
| 196 |
+
try {
|
| 197 |
+
const bytes = Uint8Array.from(atob(item.audio_base64), c=>c.charCodeAt(0));
|
| 198 |
+
const url = URL.createObjectURL(new Blob([bytes],{type:'audio/mpeg'}));
|
| 199 |
+
const audio = new Audio(url);
|
| 200 |
+
await audio.play();
|
| 201 |
+
await new Promise(resolve => {audio.onended=resolve;audio.onerror=resolve;});
|
| 202 |
+
URL.revokeObjectURL(url);
|
| 203 |
+
} catch(e) { console.warn('Audio playback failed',e); }
|
| 204 |
}
|
| 205 |
};
|
| 206 |
+
|
| 207 |
+
const renderEvents = events => {
|
| 208 |
+
$('#rise-events').innerHTML = (events || []).map(e => `
|
| 209 |
+
<span class="rise-word ${esc(e.type)}" title="${esc(e.feedback||'')}">
|
| 210 |
+
${e.type==='correct'?'✓':'✗'} ${esc(e.word)}
|
| 211 |
+
${e.heard && e.type!=='correct'?' · heard: '+esc(e.heard):''}</span>`).join('');
|
| 212 |
};
|
| 213 |
+
|
| 214 |
+
const renderMetrics = data => {
|
| 215 |
+
const m = data.metrics || {};
|
| 216 |
+
$('#rise-results').innerHTML = `
|
| 217 |
+
<h3>${data.passed?'Passed':'Score report'}</h3>
|
| 218 |
+
<div class="rise-results">
|
| 219 |
+
<div class="rise-metric"><small>Overall</small><strong>${Number(m.overall||0).toFixed(1)}%</strong></div>
|
| 220 |
<div class="rise-metric"><small>Word accuracy</small><strong>${Number(m.word_accuracy||0).toFixed(1)}%</strong></div>
|
| 221 |
<div class="rise-metric"><small>Pronunciation</small><strong>${Number(m.pronunciation||0).toFixed(1)}%</strong></div>
|
| 222 |
<div class="rise-metric"><small>Completion</small><strong>${Number(m.completion||0).toFixed(1)}%</strong></div>
|
| 223 |
+
<div class="rise-metric"><small>WER</small><strong>${Number(data.wer||0).toFixed(1)}%</strong></div>
|
| 224 |
+
<div class="rise-metric"><small>Speaking rate</small><strong>${Number(m.speaking_rate||0).toFixed(1)} wpm</strong></div>
|
| 225 |
<div class="rise-metric"><small>Hesitations</small><strong>${Number(m.hesitations||0)}</strong></div>
|
| 226 |
<div class="rise-metric"><small>Mismatches</small><strong>${Number(m.mismatches||0)}</strong></div>
|
| 227 |
+
<div class="rise-metric"><small>Skipped</small><strong>${Number(m.skipped||0)}</strong></div>
|
| 228 |
+
</div>`;
|
| 229 |
+
};
|
| 230 |
+
|
| 231 |
+
const recordPracticeDay = () => {
|
| 232 |
+
try {
|
| 233 |
+
const days = JSON.parse(localStorage.getItem('risePracticeDays') || '[]');
|
| 234 |
+
const now = new Date();
|
| 235 |
+
const today = `${now.getFullYear()}-${String(now.getMonth()+1).padStart(2,'0')}-${String(now.getDate()).padStart(2,'0')}`;
|
| 236 |
+
if (!days.includes(today)) days.push(today);
|
| 237 |
+
localStorage.setItem('risePracticeDays', JSON.stringify(days));
|
| 238 |
+
} catch(e) { console.warn('Could not save practice day',e); }
|
| 239 |
};
|
|
|
|
| 240 |
|
| 241 |
+
const stopTracks = () => {
|
| 242 |
+
if (mediaStream) {
|
| 243 |
+
mediaStream.getTracks().forEach(t=>t.stop());
|
| 244 |
+
mediaStream = null;
|
| 245 |
+
}
|
| 246 |
+
};
|
| 247 |
+
|
| 248 |
+
const setBusy = value => {
|
| 249 |
+
busy = value;
|
| 250 |
+
$('#rise-start').disabled = value;
|
| 251 |
+
$('#rise-record').disabled = value || !session;
|
| 252 |
+
$('#rise-stop').disabled = !value;
|
| 253 |
+
$('#rise-finish').disabled = value || !session;
|
| 254 |
+
$('#rise-gesture').disabled = value || !session;
|
| 255 |
+
};
|
| 256 |
+
|
| 257 |
+
$('#rise-check').addEventListener('click', async () => {
|
| 258 |
+
saveApi();
|
| 259 |
+
try {
|
| 260 |
+
const data = await request('/api/health');
|
| 261 |
+
$('#rise-connection').textContent = 'Backend connected';
|
| 262 |
+
setStatus(`Connected. Service: ${data.service || 'RISE'}. Whisper: ${data.models?.whisper ? 'yes' : 'no'}.`,'success');
|
| 263 |
+
} catch(e) {
|
| 264 |
+
$('#rise-connection').textContent = 'Backend unavailable';
|
| 265 |
+
setStatus(e.message,'error');
|
| 266 |
+
}
|
| 267 |
});
|
| 268 |
+
|
| 269 |
+
$('#rise-start').addEventListener('click', async () => {
|
| 270 |
+
saveApi();
|
| 271 |
+
try {
|
| 272 |
+
setStatus('Starting RISE session…');
|
| 273 |
+
const data = await request('/api/session/start',postJSON({
|
| 274 |
+
level:$('#rise-level').value, part:'sentence'
|
| 275 |
+
}));
|
| 276 |
+
session = data;
|
| 277 |
+
renderSession(data);
|
| 278 |
+
$('#rise-results').innerHTML = '';
|
| 279 |
+
$('#rise-decision').classList.add('rise-hidden');
|
| 280 |
+
setStatus('Session started. Read the passage aloud.','success');
|
| 281 |
+
} catch(e) { setStatus(e.message,'error'); }
|
| 282 |
});
|
| 283 |
+
|
| 284 |
+
$('#rise-record').addEventListener('click', async () => {
|
| 285 |
+
if (!session || busy) return;
|
| 286 |
try {
|
| 287 |
+
mediaStream = await navigator.mediaDevices.getUserMedia({audio:{
|
| 288 |
+
channelCount:1,echoCancellation:true,noiseSuppression:true,autoGainControl:true
|
| 289 |
+
}});
|
| 290 |
+
const mime = ['audio/webm;codecs=opus','audio/webm','audio/mp4']
|
| 291 |
+
.find(t=>MediaRecorder.isTypeSupported(t));
|
| 292 |
+
recorder = mime ? new MediaRecorder(mediaStream,{mimeType:mime}) : new MediaRecorder(mediaStream);
|
| 293 |
+
chunks = [];
|
| 294 |
+
recorder.ondataavailable = e => {if(e.data.size) chunks.push(e.data);};
|
| 295 |
+
recorder.onstart = () => {
|
| 296 |
+
setBusy(true);
|
| 297 |
+
setStatus('Recording. Read the phrase, then press Stop & evaluate.');
|
| 298 |
+
};
|
| 299 |
+
recorder.onstop = async () => {
|
| 300 |
+
stopTracks();
|
| 301 |
+
setBusy(false);
|
| 302 |
+
const blob = new Blob(chunks,{type:recorder.mimeType||'audio/webm'});
|
| 303 |
+
if (blob.size < 1500) {
|
| 304 |
+
setStatus('Recording was too short. Please try again.','error');
|
| 305 |
+
return;
|
| 306 |
+
}
|
| 307 |
+
const fd = new FormData();
|
| 308 |
+
fd.append('session_id',session.session_id);
|
| 309 |
+
fd.append('audio',blob,'reading.webm');
|
| 310 |
try {
|
| 311 |
+
setStatus('Sending audio to Whisper for evaluation…');
|
| 312 |
+
const data = await request('/api/evaluate',{method:'POST',body:fd});
|
| 313 |
renderSession(data);
|
| 314 |
+
$('#rise-heard').textContent = data.heard ? 'Whisper heard: '+data.heard : '';
|
| 315 |
renderEvents(data.events);
|
| 316 |
await playPronunciations(data.pronunciations);
|
| 317 |
+
setStatus(data.message||'Evaluation complete.',data.passage_complete?'success':'');
|
| 318 |
+
} catch(e) {setStatus(e.message,'error');}
|
| 319 |
};
|
| 320 |
recorder.start(200);
|
| 321 |
+
} catch(e) {
|
| 322 |
+
stopTracks(); setBusy(false);
|
| 323 |
+
setStatus('Microphone unavailable. Allow microphone access. '+e.message,'error');
|
| 324 |
}
|
| 325 |
});
|
| 326 |
|
| 327 |
+
$('#rise-stop').addEventListener('click', () => {
|
| 328 |
+
if (recorder && recorder.state!=='inactive') recorder.stop();
|
| 329 |
});
|
| 330 |
|
| 331 |
+
$('#rise-finish').addEventListener('click', async () => {
|
| 332 |
+
if (!session || busy) return;
|
| 333 |
+
const fd = new FormData();
|
| 334 |
+
fd.append('session_id',session.session_id);
|
| 335 |
+
try {
|
| 336 |
+
setStatus('Calculating reading results…');
|
| 337 |
+
const data = await request('/api/session/finish',{method:'POST',body:fd});
|
| 338 |
recordPracticeDay();
|
| 339 |
renderMetrics(data);
|
| 340 |
+
renderHistory(data.history || (data.record ? [data.record] : []));
|
| 341 |
+
setStatus(data.message||'Results ready.',data.passed?'success':'');
|
| 342 |
+
if (data.status==='retry_decision') {
|
| 343 |
+
$('#rise-decision').classList.remove('rise-hidden');
|
| 344 |
+
$('#rise-decision-text').textContent = data.message || 'Would you like to retry?';
|
| 345 |
+
$('#rise-record').disabled = true;
|
| 346 |
+
} else if (data.status==='completed_all') {
|
| 347 |
+
session = {...session,session_ended:true};
|
| 348 |
+
$('#rise-passage').textContent = data.message || 'Session completed.';
|
| 349 |
+
$('#rise-record').disabled = true;
|
| 350 |
+
$('#rise-finish').disabled = true;
|
| 351 |
} else {
|
| 352 |
renderSession(data);
|
| 353 |
+
$('#rise-events').innerHTML = '';
|
| 354 |
+
$('#rise-heard').textContent = '';
|
|
|
|
| 355 |
}
|
| 356 |
+
} catch(e) {setStatus(e.message,'error');}
|
| 357 |
});
|
| 358 |
|
| 359 |
+
const decide = async retry => {
|
| 360 |
+
try {
|
| 361 |
+
const data = await request('/api/session/decision',postJSON({
|
| 362 |
+
session_id:session.session_id,retry
|
| 363 |
+
}));
|
| 364 |
+
$('#rise-decision').classList.add('rise-hidden');
|
| 365 |
+
if (data.session_ended) {
|
| 366 |
+
session = {...session,session_ended:true};
|
| 367 |
+
$('#rise-passage').textContent = data.message || 'Session ended.';
|
| 368 |
+
$('#rise-record').disabled = true;
|
| 369 |
+
$('#rise-finish').disabled = true;
|
| 370 |
+
renderHistory(data.history || []);
|
| 371 |
+
setStatus(data.message||'Session ended.');
|
| 372 |
+
} else {
|
| 373 |
renderSession(data);
|
| 374 |
+
$('#rise-results').innerHTML = '';
|
| 375 |
+
$('#rise-events').innerHTML = '';
|
| 376 |
+
setStatus(data.message||'New passage ready.','success');
|
| 377 |
}
|
| 378 |
+
} catch(e) {setStatus(e.message,'error');}
|
| 379 |
};
|
| 380 |
|
| 381 |
+
$('#rise-retry').addEventListener('click',()=>decide(true));
|
| 382 |
+
$('#rise-give-up').addEventListener('click',()=>decide(false));
|
| 383 |
+
|
| 384 |
+
$('#rise-pronounce').addEventListener('click',async()=>{
|
| 385 |
+
const word = $('#rise-pronounce-word').value.trim();
|
| 386 |
+
if (!word) {setStatus('Enter a word first.','error');return;}
|
| 387 |
+
try {
|
| 388 |
+
const data = await request('/api/pronounce',postJSON({word}));
|
| 389 |
+
if (data.audio_base64) {
|
| 390 |
+
await playPronunciations([data]);
|
| 391 |
+
setStatus('Playing pronunciation for "'+word+'".','success');
|
| 392 |
+
} else {
|
| 393 |
+
throw new Error(data.audio_error||'Pronunciation audio unavailable.');
|
| 394 |
+
}
|
| 395 |
+
} catch(e) {setStatus(e.message,'error');}
|
| 396 |
});
|
| 397 |
|
| 398 |
+
$('#rise-gesture').addEventListener('click',async()=>{
|
| 399 |
+
if (!session || busy) return;
|
| 400 |
let stream;
|
| 401 |
+
try {
|
| 402 |
+
setStatus('Opening camera for gesture recognition…');
|
| 403 |
+
stream = await navigator.mediaDevices.getUserMedia({video:{facingMode:'user'}});
|
| 404 |
+
const video = document.createElement('video');
|
| 405 |
+
video.srcObject = stream;
|
| 406 |
+
video.muted = true;
|
| 407 |
await video.play();
|
| 408 |
await new Promise(r=>setTimeout(r,500));
|
| 409 |
+
const canvas = document.createElement('canvas');
|
| 410 |
+
canvas.width = video.videoWidth;
|
| 411 |
+
canvas.height = video.videoHeight;
|
| 412 |
canvas.getContext('2d').drawImage(video,0,0);
|
| 413 |
stream.getTracks().forEach(t=>t.stop());
|
| 414 |
+
stream = null;
|
| 415 |
+
const data = await request('/api/gesture',postJSON({
|
| 416 |
+
session_id:session.session_id,
|
| 417 |
+
image_data:canvas.toDataURL('image/jpeg',0.85)
|
| 418 |
+
}));
|
| 419 |
+
if (data.passage) renderSession(data);
|
| 420 |
+
if (data.session_ended) {
|
| 421 |
+
session = {...session,session_ended:true};
|
| 422 |
+
$('#rise-record').disabled = true;
|
| 423 |
+
$('#rise-finish').disabled = true;
|
| 424 |
}
|
| 425 |
+
setStatus(data.message||'Gesture check complete.',data.gesture?'success':'');
|
| 426 |
+
} catch(e) {
|
| 427 |
+
if (stream) stream.getTracks().forEach(t=>t.stop());
|
| 428 |
+
setStatus('Gesture check failed. Allow camera access. '+e.message,'error');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
}
|
| 430 |
});
|
| 431 |
|
| 432 |
+
if (apiBase) {
|
|
|
|
| 433 |
request('/api/health')
|
| 434 |
+
.then(()=>{$('#rise-connection').textContent='Backend connected';})
|
| 435 |
+
.catch(()=>{$('#rise-connection').textContent='Backend not reachable';});
|
| 436 |
}
|
| 437 |
+
})();
|
| 438 |
+
'''
|
| 439 |
+
|
| 440 |
+
# Save the complete JavaScript in one cell.
|
| 441 |
+
output = Path("/content/rise-frontend.js")
|
| 442 |
+
output.write_text(code, encoding="utf-8")
|
| 443 |
+
|
| 444 |
+
print(f"Saved complete frontend JavaScript to: {output}")
|
| 445 |
+
print(f"File size: {output.stat().st_size:,} bytes")
|
| 446 |
+
print("Now replace rise-frontend.js in your Hugging Face Space with this file.")
|