Skip to content

🎥 Make videos programmatically with React

sizes

Jonny Burgera83253868f91
diff --git a/packages/whisper-wasm/src/constants.ts b/packages/whisper-wasm/src/constants.ts
index 8da8fe402a..1f6674d441 100644
--- a/packages/whisper-wasm/src/constants.ts
+++ b/packages/whisper-wasm/src/constants.ts
@@ -12,3 +12,12 @@ export const MODELS = [
] as const;
export type WhisperModel = (typeof MODELS)[number];
+
+export const SIZES: {[key in WhisperModel]: number} = {
+ tiny: 77691713,
+ 'tiny.en': 77704715,
+ base: 147951465,
+ 'base.en': 147964211,
+ small: 487601967,
+ 'small.en': 487614201,
+};
diff --git a/packages/whisper-wasm/src/download-whisper-model.ts b/packages/whisper-wasm/src/download-whisper-model.ts
index a17f3a689a..a6c960192b 100644
--- a/packages/whisper-wasm/src/download-whisper-model.ts
+++ b/packages/whisper-wasm/src/download-whisper-model.ts
@@ -1,7 +1,5 @@
-/* eslint-disable no-console */
-
import type {WhisperModel} from './constants';
-import {MODELS} from './constants';
+import {MODELS, SIZES} from './constants';
import {getObject} from './db/get-object-from-db';
import {putObject} from './db/put-object';
import {fetchRemote} from './download-model';
@@ -21,18 +19,22 @@ export const downloadWhisperModel = async ({
onProgress,
}: DownloadWhisperModelParams): Promise<DownloadWhisperModelResult> => {
if (!model || !MODELS.includes(model)) {
- throw new Error(
- `Invalid model name. Supported models: ${MODELS.join(', ')}.`,
+ return Promise.reject(
+ new Error(`Invalid model name. Supported models: ${MODELS.join(', ')}.`),
);
}
if (!window.indexedDB) {
- throw new Error('IndexedDB is not available in this environment.');
+ return Promise.reject(
+ new Error('IndexedDB is not available in this environment.'),
+ );
}
if (!navigator.storage || !navigator.storage.estimate) {
- throw new Error(
- 'navigator.storage.estimate() API is not available in this environment',
+ return Promise.reject(
+ new Error(
+ 'navigator.storage.estimate() API is not available in this environment',
+ ),
);
}
@@ -47,8 +49,28 @@ export const downloadWhisperModel = async ({
}
const estimate = await navigator.storage.estimate();
- console.log('Estimate quota:', estimate.quota);
- console.log('Estimate usage:', estimate.usage);
+
+ if (estimate.quota === undefined) {
+ return Promise.reject(
+ new Error('navigator.storage.estimate() API returned undefined quota.'),
+ );
+ }
+
+ if (estimate.usage === undefined) {
+ return Promise.reject(
+ new Error('navigator.storage.estimate() API returned undefined usage.'),
+ );
+ }
+
+ const remaining = estimate.quota - estimate.usage;
+
+ if (remaining < SIZES[model]) {
+ return Promise.reject(
+ new Error(
+ `Not enough space to download the model. IndexedDB quota: ${estimate.quota} bytes, usage: ${estimate.usage} bytes, remaining: ${remaining} bytes, model size: ${SIZES[model]} bytes`,
+ ),
+ );
+ }
const data = await fetchRemote({url, onProgress});
await putObject({key: url, value: data});
diff --git a/packages/whisper-wasm/src/get-model-url.ts b/packages/whisper-wasm/src/get-model-url.ts
index 9394e1279a..bedc4fde52 100644
--- a/packages/whisper-wasm/src/get-model-url.ts
+++ b/packages/whisper-wasm/src/get-model-url.ts
@@ -3,3 +3,11 @@ import type {WhisperModel} from './constants';
export const getModelUrl = (model: WhisperModel) => {
return `https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-${model}.bin`;
};
+
+export const sizes = {
+ tiny: 74_000_000,
+ base: 244_000_000,
+ small: 769_000_000,
+ medium: 1550_000_000,
+ large: 3050_000_000,
+};