🎥 Make videos programmatically with React
import {expect, mock, test} from 'bun:test';
import {existsSync} from 'node:fs';
import {mkdtemp, readFile, rm, unlink} from 'node:fs/promises';
import {tmpdir} from 'node:os';
import {join} from 'node:path';
let pipelineInitialization:
| {
modelId: string;
options: Record<string, unknown>;
}
| undefined;
let pipelineEnvironment:
| {
remoteHost: string;
remotePathTemplate: string;
}
| undefined;
let pipelineEnvironmentAfterLoading:
| {
remoteHost: string;
remotePathTemplate: string;
}
| undefined;
let transcriptionCall:
| {
audio: Float32Array;
options: Record<string, unknown>;
}
| undefined;
let disposed = false;
let disposeCalls = 0;
let pipelineInitializationCount = 0;
const pipelineInitializationModelIds: string[] = [];
let pipelineInitializationGate: Promise<void> | null = null;
let onPipelineInitializationStarted: (() => void) | null = null;
let transcriptionGate: Promise<void> | null = null;
let onTranscriptionStarted: (() => void) | null = null;
let cacheCheck:
| {
task: string;
modelId: string;
options: Record<string, unknown>;
}
| undefined;
let cacheCheckEnvironment:
| {
remoteHost: string;
remotePathTemplate: string;
}
| undefined;
let cacheCheckError: Error | null = null;
let cacheClear:
| {
task: string;
modelId: string;
options: Record<string, unknown>;
}
| undefined;
let webGpuProbe:
| {
model: Uint8Array;
options: Record<string, unknown>;
}
| undefined;
let webGpuProbeError: Error | null = null;
let webGpuProbeReleaseCalls = 0;
const originalTransformersEnvironment = {
remoteHost: 'https://huggingface.co/',
remotePathTemplate: '{model}/resolve/{revision}/',
};
const transformersEnvironment = {...originalTransformersEnvironment};
const downloadFiles = ['config.json', 'onnx/encoder_model.onnx'];
const downloadRequests: string[] = [];
let downloadCacheDir = '';
let checkDownloadedFiles = false;
for (const [key, value] of Object.entries({
useCustomCache: false,
customCache: null,
useBrowserCache: false,
useFSCache: true,
cacheDir: downloadCacheDir,
fetch: (url: string) => {
downloadRequests.push(url);
const middle = Math.floor(url.length / 2);
return Promise.resolve(
new Response(
new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(url.slice(0, middle)));
controller.enqueue(new TextEncoder().encode(url.slice(middle)));
controller.close();
},
}),
),
);
},
})) {
Object.defineProperty(transformersEnvironment, key, {
configurable: true,
writable: true,
value,
});
}
const fakePipeline = Object.assign(
async (audio: Float32Array, options: Record<string, unknown>) => {
transcriptionCall = {audio, options};
onTranscriptionStarted?.();
await transcriptionGate;
return {
text: ' Hello world free today.',
chunks: [
{text: ' Hello', timestamp: [0.25, 0.75]},
{text: ' world', timestamp: [1, 1.5]},
{text: ' free', timestamp: [1.8, 1.7]},
{text: ' today.', timestamp: [2.2, null]},
],
};
},
{
dispose: () => {
disposed = true;
disposeCalls++;
return Promise.resolve();
},
},
);
mock.module('@huggingface/transformers', () => ({
env: transformersEnvironment,
ModelRegistry: {
is_pipeline_cached_files: (_task: string, modelId: string) => {
const files = downloadFiles.map((file) => ({
file,
cached: existsSync(join(downloadCacheDir, modelId, file)),
}));
return Promise.resolve({
files,
allCached: files.every(({cached}) => cached),
});
},
clear_pipeline_cache: (
task: string,
modelId: string,
options: Record<string, unknown>,
) => {
cacheClear = {task, modelId, options};
return Promise.resolve({filesDeleted: 5, filesCached: 5, files: []});
},
is_pipeline_cached: (
task: string,
modelId: string,
options: Record<string, unknown>,
) => {
cacheCheck = {task, modelId, options};
cacheCheckEnvironment = {...transformersEnvironment};
if (cacheCheckError) {
return Promise.reject(cacheCheckError);
}
return Promise.resolve(
checkDownloadedFiles
? downloadFiles.every((file) =>
existsSync(join(downloadCacheDir, modelId, file)),
)
: true,
);
},
},
pipeline: async (
_task: string,
modelId: string,
options: Record<string, unknown>,
) => {
pipelineInitializationCount++;
pipelineInitializationModelIds.push(modelId);
pipelineInitialization = {modelId, options};
pipelineEnvironment = {...transformersEnvironment};
onPipelineInitializationStarted?.();
await pipelineInitializationGate;
pipelineEnvironmentAfterLoading = {...transformersEnvironment};
const onProgress = options.progress_callback as
| ((progress: Record<string, unknown>) => void)
| undefined;
onProgress?.({status: 'initiate', file: 'encoder_model.onnx'});
onProgress?.({
status: 'progress',
file: 'encoder_model.onnx',
progress: 100,
loaded: 500_000_000,
total: 500_000_000,
});
onProgress?.({
status: 'progress_total',
progress: 100,
loaded: 500_000_000,
total: 500_000_000,
});
onProgress?.({
status: 'progress',
file: 'decoder_model_merged.onnx',
progress: 20,
loaded: 100_000_000,
total: 485_710_974,
});
onProgress?.({
status: 'progress_total',
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'});
return fakePipeline;
},
}));
mock.module('onnxruntime-node', () => ({
InferenceSession: {
create: (model: Uint8Array, options: Record<string, unknown>) => {
webGpuProbe = {model, options};
if (webGpuProbeError) {
return Promise.reject(webGpuProbeError);
}
return Promise.resolve({
release: () => {
webGpuProbeReleaseCalls++;
return Promise.resolve();
},
});
},
},
}));
test('downloads files without WebGPU and initializes only after download', async () => {
downloadCacheDir = await mkdtemp(
join(tmpdir(), 'remotion-whisper-download-'),
);
(
transformersEnvironment as typeof transformersEnvironment & {
cacheDir: string;
}
).cacheDir = downloadCacheDir;
checkDownloadedFiles = true;
downloadRequests.length = 0;
try {
const {downloadWhisperModel, loadWhisperModel} = await import('../index');
await expect(loadWhisperModel({model: 'tiny.en'})).rejects.toThrow(
'downloadWhisperModel() first',
);
const initializationsBeforeDownload = pipelineInitializationCount;
const progress: number[] = [];
const result = await downloadWhisperModel({
model: 'tiny.en',
onProgress: ({progress: value}) => progress.push(value),
});
expect(result.alreadyDownloaded).toBe(false);
expect(pipelineInitializationCount).toBe(initializationsBeforeDownload);
expect(downloadRequests).toEqual([
'https://remotion.media/models/whisper-tiny.en_timestamped-v1/config.json',
'https://remotion.media/models/whisper-tiny.en_timestamped-v1/onnx/encoder_model.onnx',
]);
expect(progress.at(-1)).toBe(1);
expect(
await readFile(
join(downloadCacheDir, 'whisper-tiny.en_timestamped-v1', 'config.json'),
'utf8',
),
).toBe(downloadRequests[0]);
await unlink(
join(
downloadCacheDir,
'whisper-tiny.en_timestamped-v1',
'onnx/encoder_model.onnx',
),
);
expect(
(await downloadWhisperModel({model: 'tiny.en'})).alreadyDownloaded,
).toBe(false);
expect(downloadRequests.slice(2)).toEqual([downloadRequests[1]]);
await using loaded = await loadWhisperModel({model: 'tiny.en'});
expect(loaded.alreadyLoaded).toBe(false);
expect(pipelineInitializationCount).toBe(initializationsBeforeDownload + 1);
expect(
(await downloadWhisperModel({model: 'tiny.en'})).alreadyDownloaded,
).toBe(true);
expect(downloadRequests).toHaveLength(3);
} finally {
checkDownloadedFiles = false;
await rm(downloadCacheDir, {recursive: true, force: true});
}
});
test('removes legacy Hugging Face Whisper entries from the shared browser cache', async () => {
const originalCaches = Object.getOwnPropertyDescriptor(globalThis, 'caches');
const cacheEnvironment =
transformersEnvironment as typeof transformersEnvironment & {
cacheKey: string;
};
cacheEnvironment.cacheKey = 'custom-transformers-cache';
const legacyTiny = new Request(
'https://huggingface.co/onnx-community/whisper-tiny_timestamped/resolve/main/config.json',
);
const legacySmallModel = new Request(
'https://huggingface.co/onnx-community/whisper-small_timestamped/resolve/main/onnx/encoder_model.onnx',
);
const currentTiny = new Request(
'https://remotion.media/models/whisper-tiny_timestamped-v1/config.json',
);
const unrelatedModel = new Request(
'https://huggingface.co/onnx-community/background-removal/resolve/main/config.json',
);
const deleted: string[] = [];
Object.defineProperty(globalThis, 'caches', {
configurable: true,
value: {
open: (cacheKey: string) => {
expect(cacheKey).toBe('custom-transformers-cache');
return Promise.resolve({
delete: (request: Request) => {
deleted.push(request.url);
return Promise.resolve(true);
},
keys: () =>
Promise.resolve([
legacyTiny,
legacySmallModel,
currentTiny,
unrelatedModel,
]),
});
},
} as unknown as CacheStorage,
});
try {
const {clearStaleModels} = await import('../index');
await clearStaleModels();
expect(deleted).toEqual([legacyTiny.url, legacySmallModel.url]);
} finally {
Reflect.deleteProperty(cacheEnvironment, 'cacheKey');
if (originalCaches) {
Object.defineProperty(globalThis, 'caches', originalCaches);
} else {
Reflect.deleteProperty(globalThis, 'caches');
}
}
});
test('transcribes with word timestamps using WebGPU', async () => {
const api = await import('../index');
const {
canUseWhisperWebGpu,
clearStaleModels,
disposeWhisperModel,
getAvailableModels,
isWhisperModelCached,
toCaptions,
transcribe,
WhisperWebGpuUnsupportedReason,
} = api;
await expect(clearStaleModels()).resolves.toBeUndefined();
const channelWaveform = new Float32Array(16_000 * 3);
const result = await transcribe({
channelWaveform,
language: 'en',
model: 'small.en',
});
expect(pipelineInitialization?.modelId).toBe(
'whisper-small.en_timestamped-v1',
);
expect(pipelineEnvironment).toEqual({
remoteHost: 'https://remotion.media/',
remotePathTemplate: 'models/{model}/',
});
expect(pipelineEnvironmentAfterLoading).toEqual(pipelineEnvironment);
expect(pipelineInitialization?.options).toMatchObject({
device: 'webgpu',
dtype: {encoder_model: 'fp32', decoder_model_merged: 'q4'},
});
expect(transcriptionCall?.audio).toBe(channelWaveform);
expect(transcriptionCall?.options).toEqual({
return_timestamps: 'word',
chunk_length_s: 30,
stride_length_s: 5,
force_full_sequences: false,
do_sample: false,
temperature: 1,
top_k: 50,
repetition_penalty: 1,
no_repeat_ngram_size: 0,
});
expect(result).toEqual({
text: 'Hello world free today.',
model: 'small.en',
words: [
{text: 'Hello', startInSeconds: 0.25, endInSeconds: 0.75},
{text: ' world', startInSeconds: 1, endInSeconds: 1.5},
{text: ' free', startInSeconds: 1.8, endInSeconds: 2.2},
{text: ' today.', startInSeconds: 2.2, endInSeconds: 3},
],
});
expect(toCaptions({whisperWebGpuOutput: result})).toEqual({
captions: [
{
text: 'Hello',
startMs: 250,
endMs: 750,
timestampMs: 500,
confidence: null,
},
{
text: ' world',
startMs: 1000,
endMs: 1500,
timestampMs: 1250,
confidence: null,
},
{
text: ' free',
startMs: 1800,
endMs: 2200,
timestampMs: 2000,
confidence: null,
},
{
text: ' today.',
startMs: 2200,
endMs: 3000,
timestampMs: 2600,
confidence: null,
},
],
});
const models = getAvailableModels();
expect(models.map((model) => model.name)).toEqual([
'tiny',
'tiny.en',
'base',
'base.en',
'small',
'small.en',
'medium',
'medium.en',
'large-v3-turbo',
]);
expect(models.find((model) => model.name === 'small.en')).toEqual({
name: 'small.en',
modelId: 'onnx-community/whisper-small.en_timestamped',
parameters: 244_000_000,
multilingual: false,
supportsTranslation: false,
webGpuDownloadSize: 586_209_938,
});
expect(models.find((model) => model.name === 'large-v3-turbo')).toMatchObject(
{
multilingual: true,
supportsTranslation: false,
webGpuDownloadSize: 1_608_611_679,
},
);
expect(await isWhisperModelCached({model: 'small.en'})).toBe(true);
expect(cacheCheck).toEqual({
task: 'automatic-speech-recognition',
modelId: 'whisper-small.en_timestamped-v1',
options: {
device: 'webgpu',
dtype: {encoder_model: 'fp32', decoder_model_merged: 'q4'},
},
});
expect(cacheCheckEnvironment).toEqual({
remoteHost: 'https://remotion.media/',
remotePathTemplate: 'models/{model}/',
});
expect(transformersEnvironment).toEqual(originalTransformersEnvironment);
expect(await canUseWhisperWebGpu()).toEqual({supported: true});
expect(webGpuProbe?.model).toHaveLength(130);
expect(webGpuProbe?.options).toEqual({executionProviders: ['webgpu']});
expect(webGpuProbeReleaseCalls).toBe(1);
webGpuProbeError = new Error('Failed to get a WebGPU adapter');
try {
expect(await canUseWhisperWebGpu()).toEqual({
supported: false,
reason: WhisperWebGpuUnsupportedReason.WebGpuUnavailable,
detailedReason:
'ONNX Runtime could not initialize WebGPU: Failed to get a WebGPU adapter',
});
} finally {
webGpuProbeError = null;
}
const originalWindow = Object.getOwnPropertyDescriptor(globalThis, 'window');
const originalNavigator = Object.getOwnPropertyDescriptor(
globalThis,
'navigator',
);
let adapter: object | null = null;
Object.defineProperty(globalThis, 'window', {
configurable: true,
value: {
crossOriginIsolated: false,
isSecureContext: true,
},
});
Object.defineProperty(globalThis, 'navigator', {
configurable: true,
value: {
gpu: {
requestAdapter: () => Promise.resolve(adapter),
},
hardwareConcurrency: 8,
},
});
try {
expect(await canUseWhisperWebGpu()).toEqual({
supported: false,
reason: WhisperWebGpuUnsupportedReason.WebGpuUnavailable,
detailedReason: 'No usable WebGPU adapter is available in this browser.',
});
adapter = {};
expect(await canUseWhisperWebGpu()).toEqual({supported: true});
} finally {
if (originalWindow) {
Object.defineProperty(globalThis, 'window', originalWindow);
} else {
delete (globalThis as {window?: unknown}).window;
}
if (originalNavigator) {
Object.defineProperty(globalThis, 'navigator', originalNavigator);
} else {
delete (globalThis as {navigator?: unknown}).navigator;
}
}
await disposeWhisperModel();
expect(disposed).toBe(true);
});
test('loads every public model from its explicit immutable hosted path', async () => {
const {disposeWhisperModel, getAvailableModels, loadWhisperModel} =
await import('../index');
const models = getAvailableModels();
const initializationsBeforeLoading = pipelineInitializationModelIds.length;
for (const model of models) {
await loadWhisperModel({model: model.name});
await disposeWhisperModel({model: model.name});
}
expect(
pipelineInitializationModelIds.slice(initializationsBeforeLoading),
).toEqual([
'whisper-tiny_timestamped-v1',
'whisper-tiny.en_timestamped-v1',
'whisper-base_timestamped-v1',
'whisper-base.en_timestamped-v1',
'whisper-small_timestamped-v1',
'whisper-small.en_timestamped-v1',
'whisper-medium_timestamped-v1',
'whisper-medium.en_timestamped-v1',
'whisper-large-v3-turbo_timestamped-v1',
]);
expect(models.map(({modelId}) => modelId)).toEqual([
'onnx-community/whisper-tiny_timestamped',
'onnx-community/whisper-tiny.en_timestamped',
'onnx-community/whisper-base_timestamped',
'onnx-community/whisper-base.en_timestamped',
'onnx-community/whisper-small_timestamped',
'onnx-community/whisper-small.en_timestamped',
'onnx-community/whisper-medium_timestamped',
'onnx-community/whisper-medium.en_timestamped',
'onnx-community/whisper-large-v3-turbo_timestamped',
]);
expect(pipelineInitialization?.options).toMatchObject({
device: 'webgpu',
dtype: {encoder_model: 'fp16', decoder_model_merged: 'q4'},
});
expect(transformersEnvironment).toEqual(originalTransformersEnvironment);
});
test('restores the Transformers environment if a hosted model operation fails', async () => {
const {isWhisperModelCached} = await import('../index');
cacheCheckError = new Error('Could not read the model cache');
try {
await expect(isWhisperModelCached({model: 'tiny'})).rejects.toThrow(
'Could not read the model cache',
);
expect(transformersEnvironment).toEqual(originalTransformersEnvironment);
} finally {
cacheCheckError = null;
}
});
test('reports initialization separately from download progress', async () => {
const {disposeWhisperModel, loadWhisperModel} = await import('../index');
const progressValues: Array<{
progress: number | null;
loadedBytes: number | null;
totalBytes: number | null;
}> = [];
await loadWhisperModel({
model: 'medium.en',
onProgress: (progress) => {
progressValues.push({
progress: progress.progress,
loadedBytes: progress.loadedBytes,
totalBytes: progress.totalBytes,
});
},
});
expect(progressValues[0]).toEqual({
progress: null,
loadedBytes: null,
totalBytes: null,
});
expect(progressValues.at(-1)).toEqual({
progress: 1,
loadedBytes: null,
totalBytes: null,
});
expect(progressValues).toHaveLength(2);
await disposeWhisperModel({model: 'medium.en'});
});
test('keeps a loaded model alive until every async disposable handle is released', async () => {
const {loadWhisperModel} = await import('../index');
const initializationsBeforeLoading = pipelineInitializationCount;
const disposalsBeforeLoading = disposeCalls;
{
await using firstLoad = await loadWhisperModel({model: 'tiny.en'});
expect(firstLoad.alreadyLoaded).toBe(false);
expect(pipelineInitializationCount).toBe(initializationsBeforeLoading + 1);
{
await using secondLoad = await loadWhisperModel({model: 'tiny.en'});
expect(secondLoad.alreadyLoaded).toBe(true);
}
expect(disposeCalls).toBe(disposalsBeforeLoading);
}
expect(disposeCalls).toBe(disposalsBeforeLoading + 1);
});
test('validates and forwards transcription settings through transcribe()', async () => {
const {disposeWhisperModel, transcribe} = await import('../index');
const channelWaveform = new Float32Array(16_000);
await expect(
transcribe({channelWaveform, model: 'tiny', language: 'auto'}),
).rejects.toThrow(
'The language option is required for the multilingual model "tiny" because automatic language detection is not supported.',
);
await expect(
transcribe({channelWaveform, model: 'tiny.en', language: 'de'}),
).rejects.toThrow(
'The English-only model "tiny.en" does not support the language "de".',
);
await expect(
transcribe({
channelWaveform,
model: 'tiny.en',
task: 'translate',
}),
).rejects.toThrow('does not support translation');
await expect(
transcribe({
channelWaveform,
language: 'en',
model: 'large-v3-turbo',
task: 'translate',
}),
).rejects.toThrow('does not support translation');
await expect(
transcribe({
channelWaveform,
model: 'tiny.en',
chunkLengthInSeconds: Number.NaN,
}),
).rejects.toThrow(
'chunkLengthInSeconds must be a finite number greater than 0.',
);
await expect(
transcribe({
channelWaveform,
model: 'tiny.en',
strideLengthInSeconds: Number.NaN,
}),
).rejects.toThrow(
'strideLengthInSeconds must be a finite, non-negative number and less than half of chunkLengthInSeconds.',
);
const invalidOptions: Array<{
options: Record<string, unknown>;
optionName: string;
}> = [
{options: {forceFullSequences: 'yes'}, optionName: 'forceFullSequences'},
{options: {doSample: 'yes'}, optionName: 'doSample'},
{options: {temperature: Number.NaN}, optionName: 'temperature'},
{options: {temperature: 0}, optionName: 'temperature'},
{options: {topK: Number.NaN}, optionName: 'topK'},
{options: {topK: -1}, optionName: 'topK'},
{options: {topK: 1.5}, optionName: 'topK'},
{options: {repetitionPenalty: Number.NaN}, optionName: 'repetitionPenalty'},
{options: {repetitionPenalty: 0}, optionName: 'repetitionPenalty'},
{options: {noRepeatNgramSize: Number.NaN}, optionName: 'noRepeatNgramSize'},
{options: {noRepeatNgramSize: -1}, optionName: 'noRepeatNgramSize'},
{options: {noRepeatNgramSize: 1.5}, optionName: 'noRepeatNgramSize'},
{options: {task: 'summarize'}, optionName: 'task'},
];
for (const {options, optionName} of invalidOptions) {
await expect(
transcribe({
channelWaveform,
model: 'tiny.en',
...options,
} as never),
).rejects.toThrow(optionName);
}
await transcribe({
channelWaveform,
model: 'tiny',
language: 'de',
task: 'translate',
forceFullSequences: true,
doSample: true,
temperature: 0.7,
topK: 25,
repetitionPenalty: 1.2,
noRepeatNgramSize: 3,
});
expect(transcriptionCall?.options).toEqual({
return_timestamps: 'word',
chunk_length_s: 30,
stride_length_s: 5,
language: 'de',
task: 'translate',
force_full_sequences: true,
do_sample: true,
temperature: 0.7,
top_k: 25,
repetition_penalty: 1.2,
no_repeat_ngram_size: 3,
});
await disposeWhisperModel({model: 'tiny'});
});
test('shares concurrent initialization and keeps the model host scoped until every operation completes', async () => {
const {
disposeWhisperModel,
isWhisperModelCached,
loadWhisperModel,
transcribe,
} = await import('../index');
let releaseInitialization: () => void = () => {};
pipelineInitializationGate = new Promise<void>((resolve) => {
releaseInitialization = resolve;
});
const initializationStarted = new Promise<void>((resolve) => {
onPipelineInitializationStarted = resolve;
});
const initializationsBeforeLoading = pipelineInitializationCount;
const disposalsBeforeLoading = disposeCalls;
const firstProgress: number[] = [];
const secondProgress: number[] = [];
const firstLoad = loadWhisperModel({
model: 'base',
onProgress: ({progress}) => {
if (progress !== null) {
firstProgress.push(progress);
}
},
});
await initializationStarted;
const transcription = transcribe({
channelWaveform: new Float32Array(16_000),
language: 'en',
model: 'base',
onModelLoadProgress: ({progress}) => {
if (progress !== null) {
secondProgress.push(progress);
}
},
});
expect(pipelineInitializationCount - initializationsBeforeLoading).toBe(1);
expect(transformersEnvironment).toEqual({
remoteHost: 'https://remotion.media/',
remotePathTemplate: 'models/{model}/',
});
expect(
(
transformersEnvironment as typeof transformersEnvironment &
Record<symbol, unknown>
)[Symbol.for('@remotion/whisper-webgpu/model-host-state')],
).toMatchObject({
activeOperations: 1,
previousRemoteConfiguration: originalTransformersEnvironment,
});
await expect(isWhisperModelCached({model: 'tiny.en'})).resolves.toBe(true);
expect(cacheCheck).toMatchObject({
modelId: 'whisper-tiny.en_timestamped-v1',
});
expect(transformersEnvironment).toEqual({
remoteHost: 'https://remotion.media/',
remotePathTemplate: 'models/{model}/',
});
const disposal = disposeWhisperModel({model: 'base'});
await Promise.resolve();
expect(disposeCalls).toBe(disposalsBeforeLoading);
releaseInitialization();
await expect(firstLoad).resolves.toMatchObject({alreadyLoaded: false});
await expect(transcription).resolves.toMatchObject({model: 'base'});
expect(firstProgress.at(-1)).toBe(1);
expect(secondProgress).toEqual(firstProgress);
await disposal;
expect(disposeCalls).toBe(disposalsBeforeLoading + 1);
expect(transformersEnvironment).toEqual(originalTransformersEnvironment);
pipelineInitializationGate = null;
onPipelineInitializationStarted = null;
});
test('removes a downloaded model from memory and the persistent cache', async () => {
const {loadWhisperModel, removeWhisperModel} = await import('../index');
const disposalsBeforeRemoval = disposeCalls;
await loadWhisperModel({model: 'large-v3-turbo'});
await removeWhisperModel({model: 'large-v3-turbo'});
expect(disposeCalls).toBe(disposalsBeforeRemoval + 1);
expect(cacheClear).toEqual({
task: 'automatic-speech-recognition',
modelId: 'whisper-large-v3-turbo_timestamped-v1',
options: {
device: 'webgpu',
dtype: {
decoder_model_merged: 'q4',
encoder_model: 'fp16',
},
},
});
});
test('only restores Transformers environment fields left unchanged by the consumer', async () => {
const {disposeWhisperModel, loadWhisperModel} = await import('../index');
let releaseInitialization: () => void = () => {};
pipelineInitializationGate = new Promise<void>((resolve) => {
releaseInitialization = resolve;
});
const initializationStarted = new Promise<void>((resolve) => {
onPipelineInitializationStarted = resolve;
});
const loading = loadWhisperModel({model: 'small'});
try {
await initializationStarted;
transformersEnvironment.remoteHost = 'https://models.example.com/';
releaseInitialization();
await loading;
expect(transformersEnvironment).toEqual({
remoteHost: 'https://models.example.com/',
remotePathTemplate: '{model}/resolve/{revision}/',
});
await disposeWhisperModel({model: 'small'});
} finally {
releaseInitialization();
pipelineInitializationGate = null;
onPipelineInitializationStarted = null;
Object.assign(transformersEnvironment, originalTransformersEnvironment);
}
});
test('waits for active transcriptions before disposal', async () => {
const {disposeWhisperModel, transcribe} = await import('../index');
let releaseTranscription: () => void = () => {};
transcriptionGate = new Promise<void>((resolve) => {
releaseTranscription = resolve;
});
const transcriptionStarted = new Promise<void>((resolve) => {
onTranscriptionStarted = resolve;
});
const disposalsBeforeTranscription = disposeCalls;
const transcription = transcribe({
channelWaveform: new Float32Array(16_000),
model: 'base.en',
});
await transcriptionStarted;
const disposal = disposeWhisperModel({model: 'base.en'});
await Promise.resolve();
expect(disposeCalls).toBe(disposalsBeforeTranscription);
releaseTranscription();
await expect(transcription).resolves.toMatchObject({model: 'base.en'});
await disposal;
expect(disposeCalls).toBe(disposalsBeforeTranscription + 1);
transcriptionGate = null;
onTranscriptionStarted = null;
});