Show Piper startup and download stages
Build and publish frontend / publish (push) Successful in 50s
Build and publish frontend / publish (push) Successful in 50s
This commit is contained in:
+16
-3
@@ -7,15 +7,27 @@ const VOICE_ID = 'sv_SE-nst-medium';
|
||||
const ASSET_ROOT = '/static/piper-wasm-v1';
|
||||
const localFetch = self.fetch.bind(self);
|
||||
|
||||
function reportStage(id, stage) {
|
||||
if (Number.isSafeInteger(id)) self.postMessage({id, type: 'progress', progress: {stage}});
|
||||
}
|
||||
|
||||
// Keep the TTS library's OPFS cache logic, but serve models/config from the
|
||||
// same-origin static asset service instead of its built-in external model URL.
|
||||
self.fetch = async (resource, options) => {
|
||||
const requestedUrl = resource instanceof Request ? resource.url : String(resource);
|
||||
const localModelUrl = localPiperModelUrl(requestedUrl, self.location.origin);
|
||||
if (!localModelUrl) return localFetch(resource, options);
|
||||
if (!localModelUrl) {
|
||||
if (activeRequestId && requestedUrl.includes('/static/piper-wasm-v1/onnx/')) {
|
||||
reportStage(activeRequestId, 'loading-runtime-assets');
|
||||
}
|
||||
return localFetch(resource, options);
|
||||
}
|
||||
const isModel = localModelUrl.endsWith('.onnx');
|
||||
if (activeRequestId) reportStage(activeRequestId, isModel ? 'loading-model' : 'loading-model-info');
|
||||
const redirected = resource instanceof Request ? new Request(localModelUrl, resource) : localModelUrl;
|
||||
const response = await localFetch(redirected, options);
|
||||
if (!activeRequestId || !localModelUrl.endsWith('.onnx')) return response;
|
||||
if (!activeRequestId || !isModel) return response;
|
||||
reportStage(activeRequestId, 'reading-model');
|
||||
return trackModelDownload(response, {
|
||||
id: activeRequestId,
|
||||
postMessage: message => self.postMessage(message),
|
||||
@@ -47,9 +59,10 @@ async function synthesizeMessage(event) {
|
||||
const {id, text} = event.data || {};
|
||||
if (!Number.isSafeInteger(id) || typeof text !== 'string') return;
|
||||
activeRequestId = id;
|
||||
self.postMessage({id, type: 'progress', progress: {stage: 'starting'}});
|
||||
reportStage(id, 'loading-runtime');
|
||||
try {
|
||||
const session = await getSession();
|
||||
reportStage(id, 'model-ready');
|
||||
const blob = await session.predict(text);
|
||||
const audio = await blob.arrayBuffer();
|
||||
self.postMessage({id, type: 'audio', audio}, [audio]);
|
||||
|
||||
Reference in New Issue
Block a user