Skip to content

🎥 Make videos programmatically with React

Clear legacy Whisper model cache

JonnyBurger9515aa8c1e08
diff --git a/packages/docs/docs/whisper-webgpu/clear-stale-models.mdx b/packages/docs/docs/whisper-webgpu/clear-stale-models.mdx
index ce9103a7fc..667ce9ab6a 100644
--- a/packages/docs/docs/whisper-webgpu/clear-stale-models.mdx
+++ b/packages/docs/docs/whisper-webgpu/clear-stale-models.mdx
@@ -10,7 +10,7 @@ Removes cached model files that are no longer returned by [`getAvailableModels()
Call this function when the page loads to remove models discontinued by a newer version of `@remotion/whisper-webgpu`.
-Model files downloaded from Hugging Face by versions before 4.0.522 are not removed by this function. They remain until the browser evicts them or its cache is cleared.
+This also removes model files downloaded from Hugging Face by versions before 4.0.522. Current models downloaded from `remotion.media` and models used by other Transformers.js workflows are preserved.
```ts twoslash title="clear-stale-models.ts"
import {clearStaleModels} from '@remotion/whisper-webgpu';
diff --git a/packages/docs/docs/whisper-webgpu/index.mdx b/packages/docs/docs/whisper-webgpu/index.mdx
index 0c55add22f..8754df166f 100644
--- a/packages/docs/docs/whisper-webgpu/index.mdx
+++ b/packages/docs/docs/whisper-webgpu/index.mdx
@@ -44,7 +44,7 @@ Model files are downloaded from `remotion.media`.
`@remotion/whisper-webgpu` temporarily sets `env.remoteHost` while loading a model and restores it afterwards. This keeps the shared Transformers.js runtime available to other AI workflows. If your site uses a Content Security Policy, allow `https://remotion.media` in `connect-src`.
-Versions before 4.0.522 downloaded models from Hugging Face. After upgrading, the browser downloads a previously cached model once more because the download URL, and therefore its cache key, has changed. With the default Transformers.js cache settings, subsequent loads use the new cached copy.
+Versions before 4.0.522 downloaded models from Hugging Face. After upgrading, the browser downloads a previously cached model once more because the download URL, and therefore its cache key, has changed. Call [`clearStaleModels()`](/docs/whisper-webgpu/clear-stale-models) to remove the old Hugging Face cache entries. With the default Transformers.js cache settings, subsequent loads use the new cached copy.
## APIs
diff --git a/packages/whisper-webgpu/src/clear-stale-models.ts b/packages/whisper-webgpu/src/clear-stale-models.ts
index dcebb89fad..11f541db51 100644
--- a/packages/whisper-webgpu/src/clear-stale-models.ts
+++ b/packages/whisper-webgpu/src/clear-stale-models.ts
@@ -1 +1,33 @@
-export const clearStaleModels = (): Promise<void> => Promise.resolve();
+import {getAvailableModels} from './models';
+
+const legacyModelUrlPrefixes = getAvailableModels().map(
+ ({modelId}) => `https://huggingface.co/${modelId}/resolve/main/`,
+);
+
+export const clearStaleModels = async (): Promise<void> => {
+ if (typeof caches === 'undefined') {
+ return;
+ }
+
+ const {env} = await import('@huggingface/transformers');
+ let cache: Cache;
+ let requests: readonly Request[];
+ try {
+ cache = await caches.open(env.cacheKey);
+ requests = await cache.keys();
+ } catch {
+ return;
+ }
+
+ await Promise.all(
+ requests.map((request) => {
+ if (
+ legacyModelUrlPrefixes.some((prefix) => request.url.startsWith(prefix))
+ ) {
+ return cache.delete(request);
+ }
+
+ return Promise.resolve(false);
+ }),
+ );
+};
diff --git a/packages/whisper-webgpu/src/test/transcribe.test.ts b/packages/whisper-webgpu/src/test/transcribe.test.ts
index 780c3ab669..71d4719023 100644
--- a/packages/whisper-webgpu/src/test/transcribe.test.ts
+++ b/packages/whisper-webgpu/src/test/transcribe.test.ts
@@ -149,6 +149,62 @@ mock.module('@huggingface/transformers', () => ({
},
}));
+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 {