Skip to content

🎥 Make videos programmatically with React

Fix premature Whisper download completion

Jonny Burger732aacb81ba3
diff --git a/packages/whisper-webgpu/src/load-whisper-model.ts b/packages/whisper-webgpu/src/load-whisper-model.ts
index 2811f4fa9d..c003c27bbc 100644
--- a/packages/whisper-webgpu/src/load-whisper-model.ts
+++ b/packages/whisper-webgpu/src/load-whisper-model.ts
@@ -29,27 +29,6 @@ const getCacheKey = (
backend: ResolvedWhisperWebGpuBackend,
) => `${model}:${backend}`;
-const normalizeProgress = (
- event: Record<string, unknown>,
-): WhisperWebGpuModelLoadProgress => {
- const rawProgress =
- typeof event.progress === 'number' ? event.progress : null;
-
- return {
- status: typeof event.status === 'string' ? event.status : 'progress',
- file: typeof event.file === 'string' ? event.file : null,
- progress:
- rawProgress === null
- ? null
- : Math.max(
- 0,
- Math.min(1, rawProgress > 1 ? rawProgress / 100 : rawProgress),
- ),
- loadedBytes: typeof event.loaded === 'number' ? event.loaded : null,
- totalBytes: typeof event.total === 'number' ? event.total : null,
- };
-};
-
export type LoadWhisperModelOptions = {
model: WhisperWebGpuModel;
backend?: WhisperWebGpuBackend;
@@ -68,6 +47,11 @@ export const loadWhisperModel = async ({
}: LoadWhisperModelOptions): Promise<LoadWhisperModelResult> => {
const resolvedBackend = await resolveBackend(backend);
const cacheKey = getCacheKey(model, resolvedBackend);
+ const modelInfo = getModelInfo(model);
+ const totalBytes =
+ resolvedBackend === 'webgpu'
+ ? modelInfo.webGpuDownloadSize
+ : modelInfo.wasmDownloadSize;
const existing = pipelines.get(cacheKey);
if (existing) {
await existing;
@@ -75,8 +59,8 @@ export const loadWhisperModel = async ({
status: 'ready',
file: null,
progress: 1,
- loadedBytes: null,
- totalBytes: null,
+ loadedBytes: totalBytes,
+ totalBytes,
});
return {backend: resolvedBackend, alreadyLoaded: true};
}
@@ -91,10 +75,11 @@ export const loadWhisperModel = async ({
file: null,
progress: 0,
loadedBytes: 0,
- totalBytes: null,
+ totalBytes,
});
- const {modelId} = getModelInfo(model);
+ const {modelId} = modelInfo;
+ const loadedByFile = new Map<string, number>();
let lastProgress = 0;
let lastLoadedBytes = 0;
const loading = pipeline('automatic-speech-recognition', modelId, {
@@ -106,20 +91,31 @@ export const loadWhisperModel = async ({
progress_callback: onProgress
? (event) => {
const record = event as Record<string, unknown>;
- if (record.status === 'progress_total') {
- const normalized = normalizeProgress(record);
+ if (
+ record.status === 'progress' &&
+ typeof record.file === 'string' &&
+ typeof record.loaded === 'number' &&
+ Number.isFinite(record.loaded)
+ ) {
+ loadedByFile.set(
+ record.file,
+ Math.max(loadedByFile.get(record.file) ?? 0, record.loaded),
+ );
+ const loadedBytes = [...loadedByFile.values()].reduce(
+ (sum, loaded) => sum + loaded,
+ 0,
+ );
lastProgress = Math.max(
lastProgress,
- normalized.progress ?? lastProgress,
- );
- lastLoadedBytes = Math.max(
- lastLoadedBytes,
- normalized.loadedBytes ?? lastLoadedBytes,
+ Math.min(loadedBytes / totalBytes, 0.99),
);
+ lastLoadedBytes = Math.max(lastLoadedBytes, loadedBytes);
onProgress({
- ...normalized,
+ status: 'loading',
+ file: null,
progress: lastProgress,
loadedBytes: lastLoadedBytes,
+ totalBytes,
});
}
@@ -128,8 +124,8 @@ export const loadWhisperModel = async ({
status: 'ready',
file: null,
progress: 1,
- loadedBytes: null,
- totalBytes: null,
+ loadedBytes: Math.max(lastLoadedBytes, totalBytes),
+ totalBytes,
});
}
}
diff --git a/packages/whisper-webgpu/src/test/transcribe.test.ts b/packages/whisper-webgpu/src/test/transcribe.test.ts
index 22b1f60066..b855e6304f 100644
--- a/packages/whisper-webgpu/src/test/transcribe.test.ts
+++ b/packages/whisper-webgpu/src/test/transcribe.test.ts
@@ -47,37 +47,38 @@ mock.module('@huggingface/transformers', () => ({
| ((progress: Record<string, unknown>) => void)
| undefined;
onProgress?.({status: 'initiate', file: 'encoder_model.onnx'});
- onProgress?.({
- status: 'progress_total',
- progress: 40,
- loaded: 40,
- total: 100,
- });
onProgress?.({
status: 'progress',
file: 'encoder_model.onnx',
- progress: 90,
- loaded: 90,
- total: 100,
+ progress: 100,
+ loaded: 500_000_000,
+ total: 500_000_000,
});
onProgress?.({
status: 'progress_total',
- progress: 30,
- loaded: 30,
- total: 100,
+ progress: 100,
+ loaded: 500_000_000,
+ total: 500_000_000,
});
onProgress?.({
status: 'progress',
file: 'decoder_model_merged.onnx',
- progress: 10,
- loaded: 10,
- total: 100,
+ progress: 20,
+ loaded: 100_000_000,
+ total: 485_710_974,
});
onProgress?.({
status: 'progress_total',
- progress: 60,
- loaded: 60,
- total: 100,
+ progress: 60.87,
+ loaded: 600_000_000,
+ total: 985_710_974,
+ });
+ onProgress?.({
+ status: 'progress',
+ file: 'decoder_model_merged.onnx',
+ progress: 100,
+ loaded: 485_710_974,
+ total: 485_710_974,
});
onProgress?.({status: 'done', file: 'encoder_model.onnx'});
onProgress?.({status: 'ready'});
@@ -260,17 +261,46 @@ test('transcribes with timestamps and selects a usable backend', async () => {
expect(disposed).toBe(true);
});
-test('reports aggregate model progress without moving backwards', async () => {
+test('reports model progress without reaching 100% before every file loads', async () => {
const {disposeWhisperModel, loadWhisperModel} = await import('../index');
- const progressValues: Array<number | null> = [];
+ const progressValues: Array<{
+ progress: number | null;
+ loadedBytes: number | null;
+ totalBytes: number | null;
+ }> = [];
await loadWhisperModel({
backend: 'wasm',
model: 'medium.en',
onProgress: (progress) => {
- progressValues.push(progress.progress);
+ progressValues.push({
+ progress: progress.progress,
+ loadedBytes: progress.loadedBytes,
+ totalBytes: progress.totalBytes,
+ });
},
});
- expect(progressValues).toEqual([0, 0.4, 0.4, 0.6, 1]);
+ expect(progressValues[0]).toEqual({
+ progress: 0,
+ loadedBytes: 0,
+ totalBytes: 985_710_974,
+ });
+ expect(progressValues.at(-1)).toEqual({
+ progress: 1,
+ loadedBytes: 985_710_974,
+ totalBytes: 985_710_974,
+ });
+ const downloading = progressValues.slice(0, -1);
+ expect(
+ downloading.every(({progress}) => progress !== null && progress < 1),
+ ).toBe(true);
+ expect(downloading.map(({loadedBytes}) => loadedBytes)).toEqual([
+ 0, 500_000_000, 600_000_000, 985_710_974,
+ ]);
+ expect(downloading.map(({progress}) => progress)).toEqual(
+ [0, 500_000_000, 600_000_000, 985_710_974].map((loaded) =>
+ Math.min(loaded / 985_710_974, 0.99),
+ ),
+ );
await disposeWhisperModel({backend: 'wasm', model: 'medium.en'});
});