copilot-swe-agent[bot] ping-long-github commited on
fix: verify logic and finalize infrastructure for Open-ASR Model Explorer
Browse filesAgent-Logs-Url: https://github.com/SiliconLanguage/model-explorer-open-asr/sessions/3d5bce09-c4b9-4ca1-8887-dfd53867fc2b
Co-authored-by: ping-long-github <155590345+ping-long-github@users.noreply.github.com>
- README.md +15 -1
- docker-compose.yml +61 -0
- frontend/Dockerfile +28 -0
- frontend/nginx.conf +42 -0
- frontend/src/components/ModelSelector.jsx +1 -0
- frontend/src/services/inferenceRouter.js +46 -24
- frontend/src/workers/webgpu.worker.js +31 -10
README.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Open-ASR Model Explorer
|
| 2 |
|
| 3 |
[](https://huggingface.co/spaces)
|
|
@@ -63,6 +73,7 @@ A hybrid inference testbed for evaluating top open-source ASR models (Cohere, Qw
|
|
| 63 |
| `Qwen3-ASR-1.7B` | **Server (vLLM)** | Routes to FastAPI backend |
|
| 64 |
| `ibm-granite/granite-4.0-1b-speech` | **Server (vLLM)** | Routes to FastAPI backend |
|
| 65 |
| `Cohere-Transcribe-WebGPU` | **Client (WebGPU)** | Runs in-browser via transformers.js |
|
|
|
|
| 66 |
|
| 67 |
### WebGPU Client-Side Features
|
| 68 |
- **Zero-server transcription** β audio never leaves the browser
|
|
@@ -80,11 +91,14 @@ A hybrid inference testbed for evaluating top open-source ASR models (Cohere, Qw
|
|
| 80 |
|
| 81 |
```
|
| 82 |
model-explorer-open-asr/
|
|
|
|
| 83 |
βββ backend/
|
| 84 |
β βββ app.py # FastAPI application with vLLM integration
|
| 85 |
β βββ requirements.txt # Python dependencies
|
| 86 |
β βββ Dockerfile # CUDA 12.1 container image
|
| 87 |
βββ frontend/
|
|
|
|
|
|
|
| 88 |
βββ index.html
|
| 89 |
βββ package.json
|
| 90 |
βββ vite.config.js
|
|
@@ -166,7 +180,7 @@ docker run --gpus all -p 8000:8000 \
|
|
| 166 |
docker compose up --build
|
| 167 |
```
|
| 168 |
|
| 169 |
-
|
| 170 |
|
| 171 |
---
|
| 172 |
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Open-ASR Model Explorer
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 8000
|
| 8 |
+
pinned: false
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
# Open-ASR Model Explorer
|
| 12 |
|
| 13 |
[](https://huggingface.co/spaces)
|
|
|
|
| 73 |
| `Qwen3-ASR-1.7B` | **Server (vLLM)** | Routes to FastAPI backend |
|
| 74 |
| `ibm-granite/granite-4.0-1b-speech` | **Server (vLLM)** | Routes to FastAPI backend |
|
| 75 |
| `Cohere-Transcribe-WebGPU` | **Client (WebGPU)** | Runs in-browser via transformers.js |
|
| 76 |
+
| `Whisper-Large-v3-Turbo-WebGPU` | **Client (WebGPU)** | Whisper Large v3 Turbo in-browser via transformers.js |
|
| 77 |
|
| 78 |
### WebGPU Client-Side Features
|
| 79 |
- **Zero-server transcription** β audio never leaves the browser
|
|
|
|
| 91 |
|
| 92 |
```
|
| 93 |
model-explorer-open-asr/
|
| 94 |
+
βββ docker-compose.yml # Full-stack orchestration (backend + frontend)
|
| 95 |
βββ backend/
|
| 96 |
β βββ app.py # FastAPI application with vLLM integration
|
| 97 |
β βββ requirements.txt # Python dependencies
|
| 98 |
β βββ Dockerfile # CUDA 12.1 container image
|
| 99 |
βββ frontend/
|
| 100 |
+
βββ Dockerfile # Multi-stage Vite build β Nginx serve
|
| 101 |
+
βββ nginx.conf # Nginx: API proxy + SSE streaming + SPA fallback
|
| 102 |
βββ index.html
|
| 103 |
βββ package.json
|
| 104 |
βββ vite.config.js
|
|
|
|
| 180 |
docker compose up --build
|
| 181 |
```
|
| 182 |
|
| 183 |
+
The UI will be available at `http://localhost:3000` and the API at `http://localhost:8000`.
|
| 184 |
|
| 185 |
---
|
| 186 |
|
docker-compose.yml
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Open-ASR Model Explorer β Docker Compose
|
| 2 |
+
# Usage:
|
| 3 |
+
# docker compose up --build # start both services
|
| 4 |
+
# docker compose up --build backend # backend only
|
| 5 |
+
# docker compose down -v # stop and remove volumes
|
| 6 |
+
#
|
| 7 |
+
# The frontend is served by Nginx on http://localhost:3000.
|
| 8 |
+
# The backend FastAPI API is available directly on http://localhost:8000.
|
| 9 |
+
# Nginx proxies /api/* β http://backend:8000/* so the UI calls /api/β¦ .
|
| 10 |
+
|
| 11 |
+
services:
|
| 12 |
+
|
| 13 |
+
# ββ FastAPI + vLLM backend ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 14 |
+
backend:
|
| 15 |
+
build:
|
| 16 |
+
context: ./backend
|
| 17 |
+
dockerfile: Dockerfile
|
| 18 |
+
ports:
|
| 19 |
+
- "8000:8000"
|
| 20 |
+
volumes:
|
| 21 |
+
# Persist downloaded HuggingFace model weights across container restarts.
|
| 22 |
+
- hf_cache:/hf_cache
|
| 23 |
+
environment:
|
| 24 |
+
- HF_HOME=/hf_cache
|
| 25 |
+
- TRANSFORMERS_CACHE=/hf_cache
|
| 26 |
+
# Uncomment and set HF_TOKEN if you access gated models on HuggingFace
|
| 27 |
+
# (e.g. Cohere-transcribe-03-2026 or IBM Granite may require acceptance
|
| 28 |
+
# of model terms before download).
|
| 29 |
+
# - HF_TOKEN=${HF_TOKEN}
|
| 30 |
+
# GPU access β requires the NVIDIA Container Toolkit.
|
| 31 |
+
# Remove the `deploy` block entirely if running CPU-only.
|
| 32 |
+
deploy:
|
| 33 |
+
resources:
|
| 34 |
+
reservations:
|
| 35 |
+
devices:
|
| 36 |
+
- driver: nvidia
|
| 37 |
+
count: all
|
| 38 |
+
capabilities: [gpu]
|
| 39 |
+
restart: unless-stopped
|
| 40 |
+
healthcheck:
|
| 41 |
+
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"]
|
| 42 |
+
interval: 30s
|
| 43 |
+
timeout: 10s
|
| 44 |
+
start_period: 60s
|
| 45 |
+
retries: 3
|
| 46 |
+
|
| 47 |
+
# ββ React / Nginx frontend ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 48 |
+
frontend:
|
| 49 |
+
build:
|
| 50 |
+
context: ./frontend
|
| 51 |
+
dockerfile: Dockerfile
|
| 52 |
+
ports:
|
| 53 |
+
- "3000:80"
|
| 54 |
+
depends_on:
|
| 55 |
+
backend:
|
| 56 |
+
condition: service_healthy
|
| 57 |
+
restart: unless-stopped
|
| 58 |
+
|
| 59 |
+
volumes:
|
| 60 |
+
# Named volume for HuggingFace model weight cache.
|
| 61 |
+
hf_cache:
|
frontend/Dockerfile
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ββ Stage 1: build the Vite / React app ββββββββββββββββββββββββββββββββββββββ
|
| 2 |
+
FROM node:20-alpine AS builder
|
| 3 |
+
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Install dependencies first to leverage Docker layer caching
|
| 7 |
+
COPY package.json package-lock.json ./
|
| 8 |
+
RUN npm ci --ignore-scripts
|
| 9 |
+
|
| 10 |
+
# Copy source and build
|
| 11 |
+
COPY . .
|
| 12 |
+
RUN npm run build
|
| 13 |
+
|
| 14 |
+
# ββ Stage 2: serve the built assets with Nginx ββββββββββββββββββββββββββββββββ
|
| 15 |
+
FROM nginx:1.27-alpine
|
| 16 |
+
|
| 17 |
+
# Replace the default config with our custom one
|
| 18 |
+
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
| 19 |
+
|
| 20 |
+
# Copy the compiled app
|
| 21 |
+
COPY --from=builder /app/dist /usr/share/nginx/html
|
| 22 |
+
|
| 23 |
+
EXPOSE 80
|
| 24 |
+
|
| 25 |
+
HEALTHCHECK --interval=15s --timeout=5s --start-period=5s --retries=3 \
|
| 26 |
+
CMD wget -qO- http://localhost:80/ || exit 1
|
| 27 |
+
|
| 28 |
+
CMD ["nginx", "-g", "daemon off;"]
|
frontend/nginx.conf
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
server {
|
| 2 |
+
listen 80;
|
| 3 |
+
server_name _;
|
| 4 |
+
|
| 5 |
+
root /usr/share/nginx/html;
|
| 6 |
+
index index.html;
|
| 7 |
+
|
| 8 |
+
# ββ API proxy β FastAPI backend βββββββββββββββββββββββββββββββββββββββββ
|
| 9 |
+
# Strips the /api prefix before forwarding so the backend routes stay clean.
|
| 10 |
+
location /api/ {
|
| 11 |
+
proxy_pass http://backend:8000/;
|
| 12 |
+
proxy_http_version 1.1;
|
| 13 |
+
|
| 14 |
+
proxy_set_header Host $host;
|
| 15 |
+
proxy_set_header X-Real-IP $remote_addr;
|
| 16 |
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
| 17 |
+
proxy_set_header X-Forwarded-Proto $scheme;
|
| 18 |
+
|
| 19 |
+
# SSE / chunked streaming: disable all buffering so tokens reach the
|
| 20 |
+
# browser as soon as the backend writes them.
|
| 21 |
+
proxy_set_header Connection '';
|
| 22 |
+
proxy_buffering off;
|
| 23 |
+
proxy_cache off;
|
| 24 |
+
chunked_transfer_encoding on;
|
| 25 |
+
|
| 26 |
+
# Generous timeouts for long transcription jobs.
|
| 27 |
+
proxy_read_timeout 600s;
|
| 28 |
+
proxy_send_timeout 600s;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
# ββ Static assets with long-lived cache ββββββββββββββββββββββββββββββββ
|
| 32 |
+
location ~* \.(js|css|wasm|woff2?|ttf|ico|png|svg)$ {
|
| 33 |
+
expires 1y;
|
| 34 |
+
add_header Cache-Control "public, immutable";
|
| 35 |
+
try_files $uri =404;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
# ββ SPA fallback β all unmatched routes serve index.html βββββββββββββββ
|
| 39 |
+
location / {
|
| 40 |
+
try_files $uri $uri/ /index.html;
|
| 41 |
+
}
|
| 42 |
+
}
|
frontend/src/components/ModelSelector.jsx
CHANGED
|
@@ -10,6 +10,7 @@ const MODELS = [
|
|
| 10 |
{ id: 'Qwen3-ASR-1.7B', label: 'Qwen3-ASR 1.7B', mode: 'server' },
|
| 11 |
{ id: 'ibm-granite/granite-4.0-1b-speech', label: 'IBM Granite 4.0 1B Speech', mode: 'server' },
|
| 12 |
{ id: 'Cohere-Transcribe-WebGPU', label: 'Cohere Transcribe (WebGPU)', mode: 'webgpu' },
|
|
|
|
| 13 |
];
|
| 14 |
|
| 15 |
const BADGE = {
|
|
|
|
| 10 |
{ id: 'Qwen3-ASR-1.7B', label: 'Qwen3-ASR 1.7B', mode: 'server' },
|
| 11 |
{ id: 'ibm-granite/granite-4.0-1b-speech', label: 'IBM Granite 4.0 1B Speech', mode: 'server' },
|
| 12 |
{ id: 'Cohere-Transcribe-WebGPU', label: 'Cohere Transcribe (WebGPU)', mode: 'webgpu' },
|
| 13 |
+
{ id: 'Whisper-Large-v3-Turbo-WebGPU', label: 'Whisper Large v3 Turbo (WebGPU)', mode: 'webgpu' },
|
| 14 |
];
|
| 15 |
|
| 16 |
const BADGE = {
|
frontend/src/services/inferenceRouter.js
CHANGED
|
@@ -15,6 +15,7 @@
|
|
| 15 |
|
| 16 |
let workerInstance = null;
|
| 17 |
let workerReadyResolve = null;
|
|
|
|
| 18 |
const pendingCallbacks = new Map(); // id β { resolve, reject, onProgress }
|
| 19 |
|
| 20 |
function getWorker() {
|
|
@@ -29,12 +30,25 @@ function getWorker() {
|
|
| 29 |
workerInstance.addEventListener('message', (event) => {
|
| 30 |
const { type, id, stable, unstable, transcript, ttft_ms, itl_ms, rtfx, message } = event.data;
|
| 31 |
|
|
|
|
| 32 |
if (type === 'ready' && workerReadyResolve) {
|
| 33 |
-
workerReadyResolve
|
| 34 |
workerReadyResolve = null;
|
|
|
|
|
|
|
| 35 |
return;
|
| 36 |
}
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
const cb = pendingCallbacks.get(id);
|
| 39 |
if (!cb) return;
|
| 40 |
|
|
@@ -58,8 +72,9 @@ function getWorker() {
|
|
| 58 |
|
| 59 |
async function ensureModelLoaded(modelId) {
|
| 60 |
const worker = getWorker();
|
| 61 |
-
return new Promise((resolve) => {
|
| 62 |
workerReadyResolve = resolve;
|
|
|
|
| 63 |
worker.postMessage({ type: 'load', model: modelId });
|
| 64 |
});
|
| 65 |
}
|
|
@@ -97,31 +112,38 @@ async function routeServer(modelId, audioFile, onProgress) {
|
|
| 97 |
let buffer = '';
|
| 98 |
let finalResult = null;
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
const
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
}
|
| 121 |
-
} catch {
|
| 122 |
-
// ignore malformed SSE frames
|
| 123 |
}
|
| 124 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
}
|
| 126 |
|
| 127 |
if (!finalResult) throw new Error('Stream ended without a final result.');
|
|
|
|
| 15 |
|
| 16 |
let workerInstance = null;
|
| 17 |
let workerReadyResolve = null;
|
| 18 |
+
let workerReadyReject = null; // companion reject for the load promise
|
| 19 |
const pendingCallbacks = new Map(); // id β { resolve, reject, onProgress }
|
| 20 |
|
| 21 |
function getWorker() {
|
|
|
|
| 30 |
workerInstance.addEventListener('message', (event) => {
|
| 31 |
const { type, id, stable, unstable, transcript, ttft_ms, itl_ms, rtfx, message } = event.data;
|
| 32 |
|
| 33 |
+
// ββ Load lifecycle messages βββββββββββββββββββββββββββββββββββββββββββββ
|
| 34 |
if (type === 'ready' && workerReadyResolve) {
|
| 35 |
+
const res = workerReadyResolve;
|
| 36 |
workerReadyResolve = null;
|
| 37 |
+
workerReadyReject = null;
|
| 38 |
+
res();
|
| 39 |
return;
|
| 40 |
}
|
| 41 |
|
| 42 |
+
// A null id means the error came from loadModel(), not a transcription.
|
| 43 |
+
if (type === 'error' && id == null) {
|
| 44 |
+
const rej = workerReadyReject;
|
| 45 |
+
workerReadyResolve = null;
|
| 46 |
+
workerReadyReject = null;
|
| 47 |
+
rej?.(new Error(message ?? 'Model failed to load'));
|
| 48 |
+
return;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// ββ Transcription lifecycle messages ββββββββββββββββββββββββββββββββββββ
|
| 52 |
const cb = pendingCallbacks.get(id);
|
| 53 |
if (!cb) return;
|
| 54 |
|
|
|
|
| 72 |
|
| 73 |
async function ensureModelLoaded(modelId) {
|
| 74 |
const worker = getWorker();
|
| 75 |
+
return new Promise((resolve, reject) => {
|
| 76 |
workerReadyResolve = resolve;
|
| 77 |
+
workerReadyReject = reject;
|
| 78 |
worker.postMessage({ type: 'load', model: modelId });
|
| 79 |
});
|
| 80 |
}
|
|
|
|
| 112 |
let buffer = '';
|
| 113 |
let finalResult = null;
|
| 114 |
|
| 115 |
+
try {
|
| 116 |
+
while (true) {
|
| 117 |
+
const { value, done } = await reader.read();
|
| 118 |
+
if (done) break;
|
| 119 |
+
buffer += decoder.decode(value, { stream: true });
|
| 120 |
+
|
| 121 |
+
// Parse Server-Sent Events
|
| 122 |
+
const events = buffer.split('\n\n');
|
| 123 |
+
buffer = events.pop() ?? '';
|
| 124 |
+
|
| 125 |
+
for (const event of events) {
|
| 126 |
+
const dataLine = event.split('\n').find((l) => l.startsWith('data: '));
|
| 127 |
+
if (!dataLine) continue;
|
| 128 |
+
try {
|
| 129 |
+
const payload = JSON.parse(dataLine.slice(6));
|
| 130 |
+
if (payload.done) {
|
| 131 |
+
finalResult = payload;
|
| 132 |
+
} else if (payload.token) {
|
| 133 |
+
onProgress?.({ token: payload.token, ttft_ms: payload.ttft_ms });
|
| 134 |
+
} else if (payload.ttft_ms != null) {
|
| 135 |
+
onProgress?.({ ttft_ms: payload.ttft_ms });
|
| 136 |
+
}
|
| 137 |
+
} catch {
|
| 138 |
+
// ignore malformed SSE frames
|
| 139 |
}
|
|
|
|
|
|
|
| 140 |
}
|
| 141 |
}
|
| 142 |
+
} finally {
|
| 143 |
+
// Always release the reader so the underlying HTTP connection can be
|
| 144 |
+
// returned to the pool. This prevents stream leaks when an exception
|
| 145 |
+
// is thrown mid-stream (e.g. network error or component unmount).
|
| 146 |
+
reader.cancel();
|
| 147 |
}
|
| 148 |
|
| 149 |
if (!finalResult) throw new Error('Stream ended without a final result.');
|
frontend/src/workers/webgpu.worker.js
CHANGED
|
@@ -27,7 +27,9 @@ import { pipeline, env } from '@xenova/transformers';
|
|
| 27 |
// ββ Cache configuration βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
// Point transformers.js at the Cache Storage API so ONNX weights survive
|
| 29 |
// page reloads without re-downloading from the CDN.
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
env.allowLocalModels = false;
|
| 32 |
|
| 33 |
// ββ State βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -70,10 +72,16 @@ class LocalAgreement2 {
|
|
| 70 |
*/
|
| 71 |
update(current) {
|
| 72 |
const agreed = longestCommonPrefix(this.prev, current);
|
| 73 |
-
// Extend the stable set with newly confirmed tokens
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
this.prev = current;
|
| 78 |
const stable = this.stableTokens.join('');
|
| 79 |
const unstable = current.slice(this.stableTokens.length).join('');
|
|
@@ -94,14 +102,27 @@ async function loadModel(modelId) {
|
|
| 94 |
}
|
| 95 |
try {
|
| 96 |
self.postMessage({ type: 'loading', modelId });
|
| 97 |
-
|
| 98 |
-
//
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
loadedModelId = modelId;
|
| 103 |
self.postMessage({ type: 'ready' });
|
| 104 |
} catch (err) {
|
|
|
|
|
|
|
|
|
|
| 105 |
self.postMessage({ type: 'error', id: null, message: String(err) });
|
| 106 |
}
|
| 107 |
}
|
|
|
|
| 27 |
// ββ Cache configuration βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
// Point transformers.js at the Cache Storage API so ONNX weights survive
|
| 29 |
// page reloads without re-downloading from the CDN.
|
| 30 |
+
// useBrowserCache relies on the Cache Storage API which is only available in
|
| 31 |
+
// secure contexts (HTTPS or localhost); it is a no-op if the API is absent.
|
| 32 |
+
env.useBrowserCache = typeof caches !== 'undefined';
|
| 33 |
env.allowLocalModels = false;
|
| 34 |
|
| 35 |
// ββ State βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 72 |
*/
|
| 73 |
update(current) {
|
| 74 |
const agreed = longestCommonPrefix(this.prev, current);
|
| 75 |
+
// Extend the stable set with newly confirmed tokens, but never claim more
|
| 76 |
+
// stable tokens than actually exist in the current hypothesis. Capping at
|
| 77 |
+
// current.length prevents stale over-commitment when the model produces a
|
| 78 |
+
// shorter hypothesis (e.g. at the start of a new audio chunk), which would
|
| 79 |
+
// otherwise show confirmed text that is no longer in the active hypothesis.
|
| 80 |
+
const newStableLen = Math.min(
|
| 81 |
+
Math.max(agreed.length, this.stableTokens.length),
|
| 82 |
+
current.length,
|
| 83 |
+
);
|
| 84 |
+
this.stableTokens = current.slice(0, newStableLen);
|
| 85 |
this.prev = current;
|
| 86 |
const stable = this.stableTokens.join('');
|
| 87 |
const unstable = current.slice(this.stableTokens.length).join('');
|
|
|
|
| 102 |
}
|
| 103 |
try {
|
| 104 |
self.postMessage({ type: 'loading', modelId });
|
| 105 |
+
try {
|
| 106 |
+
// Prefer WebGPU (fp16) for maximum throughput.
|
| 107 |
+
asr = await pipeline('automatic-speech-recognition', modelId, {
|
| 108 |
+
device: 'webgpu',
|
| 109 |
+
dtype: 'fp16',
|
| 110 |
+
});
|
| 111 |
+
} catch {
|
| 112 |
+
// WebGPU unavailable (non-secure context, old browser, no GPU) β
|
| 113 |
+
// fall back to WASM fp32 so the app still works.
|
| 114 |
+
self.postMessage({ type: 'loading', modelId, fallback: 'wasm' });
|
| 115 |
+
asr = await pipeline('automatic-speech-recognition', modelId, {
|
| 116 |
+
device: 'wasm',
|
| 117 |
+
dtype: 'fp32',
|
| 118 |
+
});
|
| 119 |
+
}
|
| 120 |
loadedModelId = modelId;
|
| 121 |
self.postMessage({ type: 'ready' });
|
| 122 |
} catch (err) {
|
| 123 |
+
// Reset state so a subsequent load attempt can retry cleanly.
|
| 124 |
+
asr = null;
|
| 125 |
+
loadedModelId = null;
|
| 126 |
self.postMessage({ type: 'error', id: null, message: String(err) });
|
| 127 |
}
|
| 128 |
}
|