Skip to content

🎥 Make videos programmatically with React

add types

Hunain Ahmed2a2f195cb3fc
diff --git a/packages/whisper-wasm/append.js b/packages/whisper-wasm/append.js
index fe08c8dddb..455d899d6e 100644
--- a/packages/whisper-wasm/append.js
+++ b/packages/whisper-wasm/append.js
@@ -26,7 +26,8 @@ const mainJsContent = fs.readFileSync(MAIN_JS, 'utf8');
// Concatenate helpers.ts and main.js, then write to index.ts
fs.writeFileSync(
INDEX_MODULE_TS,
- helpersContent + '\n' + mainJsContent,
+ helpersContent + '\n' + `/* eslint-disable */
+` + '\n' + mainJsContent,
'utf8',
);
diff --git a/packages/whisper-wasm/src/helpers.ts b/packages/whisper-wasm/src/helpers.ts
index a624d15297..9412477f5a 100644
--- a/packages/whisper-wasm/src/helpers.ts
+++ b/packages/whisper-wasm/src/helpers.ts
@@ -1,6 +1,6 @@
-/* eslint-disable no-console, new-cap */
-// @ts-nocheck
+/* eslint-disable no-console, new-cap, no-var */
import type {MainModule} from './emscripten-types';
+import type {DownloadWhisperModel} from './index';
const dbName = 'whisper-wasm';
const dst = 'whisper.bin';
@@ -9,19 +9,19 @@ const kMaxAudio_s = 30 * 60;
const kSampleRate = 16000;
const maxThreadsAllowed = 16;
-let transcriptionText = [];
-let transcriptionProgressPlayback = null;
-let transcriptionChunkPlayback = null;
-let resolver = null;
+let transcriptionText: string[] = [];
+let transcriptionProgressPlayback;
+let transcriptionChunkPlayback;
+let resolver;
let modelLoading = false;
let transcribing = false;
-let instance = null;
-let context = null;
+let instance;
+let context;
let headerWarningPrinted = false;
-const indexedDB = typeof window !== 'undefined' ? window.indexedDB : null;
+const indexedDB = window?.indexedDB;
const printHandler = (text) => {
const progressMatch = text.match(/Progress:\s*(\d+)%/i);
@@ -38,11 +38,7 @@ const printHandler = (text) => {
const timestampStart = chunkMatch[1];
const timestampEnd = chunkMatch[2];
const textOnly = chunkMatch[3].trim();
- transcriptionChunkPlayback?.(
- timestampStart,
- timestampEnd,
- textOnly,
- );
+ transcriptionChunkPlayback?.(timestampStart, timestampEnd, textOnly);
transcriptionText.push(textOnly);
}
@@ -76,7 +72,7 @@ const storeFS = (fname: string, buf: any) => {
// ignore
}
- Module.FS_createDataFile('/', fname, buf, true, true);
+ Module.FS_createDataFile('/', fname, buf, true, true, undefined);
};
const fetchRemote = async (
@@ -94,7 +90,7 @@ const fetchRemote = async (
const total = parseInt(contentLength, 10);
const reader = response.body.getReader();
- const chunks = [];
+ const chunks: Uint8Array<ArrayBufferLike>[] = [];
let receivedLength = 0;
let progressLast = -1;
@@ -139,11 +135,12 @@ const postModelLoaded = (data: Uint8Array<ArrayBuffer>, url: string) => {
const rq = indexedDB.open(dbName, dbVersion);
rq.onsuccess = function (event) {
+ // @ts-expect-error
const db = event.target.result;
const objectStore = db
.transaction(['models'], 'readwrite')
.objectStore('models');
- let putRq = null;
+ let putRq;
try {
putRq = objectStore.put(data, url);
} catch (e) {
@@ -166,7 +163,7 @@ const postModelLoaded = (data: Uint8Array<ArrayBuffer>, url: string) => {
});
};
-const loadModel = (url: string, cbProgress: (arg0: number) => void) => {
+const loadModel = (url: string, cbProgress?: (arg0: number) => void) => {
return new Promise((resolve, reject) => {
checkForHeaders();
if (!navigator.storage || !navigator.storage.estimate) {
@@ -181,11 +178,13 @@ const loadModel = (url: string, cbProgress: (arg0: number) => void) => {
const rq = indexedDB.open(dbName, dbVersion);
rq.onupgradeneeded = (event) => {
+ // @ts-expect-error
const db = event.target.result;
if (event.oldVersion < 1) {
db.createObjectStore('models', {autoIncrement: false});
console.log('DB created');
} else {
+ // @ts-expect-error
const os = event.currentTarget.transaction.objectStore('models');
os.clear();
console.log('DB cleared');
@@ -193,6 +192,7 @@ const loadModel = (url: string, cbProgress: (arg0: number) => void) => {
};
rq.onsuccess = (event) => {
+ // @ts-expect-error
const db = event.target.result;
const transaction = db.transaction(['models'], 'readonly');
const objectStore = transaction.objectStore('models');
@@ -225,7 +225,10 @@ const loadModel = (url: string, cbProgress: (arg0: number) => void) => {
});
};
-export const downloadWhisperModel = async ({model, onProgress}) => {
+export const downloadWhisperModel = async ({
+ model,
+ onProgress,
+}: DownloadWhisperModel) => {
modelLoading = true;
const allowedModels = [
'tiny',
@@ -262,10 +265,6 @@ const getAudioContext = () => {
if (!context) {
context = new AudioContext({
sampleRate: kSampleRate,
- channelCount: 1,
- echoCancellation: false,
- autoGainControl: true,
- noiseSuppression: true,
});
}
@@ -318,7 +317,7 @@ const audioProcessor = (file) => {
reader.onload = async () => {
try {
- const buffer = new Uint8Array(reader.result);
+ const buffer = new Uint8Array(reader.result as ArrayBuffer);
const audioBuffer = await innerContext.decodeAudioData(buffer.buffer);
const processedAudio = await audioDecoder(audioBuffer);
resolve(processedAudio);
diff --git a/packages/whisper-wasm/src/index.ts b/packages/whisper-wasm/src/index.ts
index f3b18dc2f7..8292f9cad2 100644
--- a/packages/whisper-wasm/src/index.ts
+++ b/packages/whisper-wasm/src/index.ts
@@ -1,20 +1,39 @@
-export interface transcribe{
- file: File,
- onProgress?: (p:number) => void,
- onTranscribeChunk? : (start:string, end: string, text:string) => void,
- threads?: number
+/* eslint-disable @typescript-eslint/no-unused-vars */
+
+export interface Transcribe {
+ file: File;
+ onProgress?: (p: number) => void;
+ onTranscribeChunk?: (start: string, end: string, text: string) => void;
+ threads?: number;
}
-export const transcribe = (_args: transcribe) => {
+// Ensures transcribe() returns a Promise that resolves to a string
+export const transcribe = async (_args: Transcribe): Promise<string> => {
throw new Error('cjs not supported');
};
-export interface downloadWhisperModel {
- model: string,
- onProgress?: (progress: number) => void
-}
+export type WhisperModel =
+ | 'tiny'
+ | 'tiny.en'
+ | 'base'
+ | 'base.en'
+ | 'small'
+ | 'small.en'
+ | 'medium'
+ | 'medium.en'
+ | 'large-v1'
+ | 'large-v2'
+ | 'large-v3'
+ | 'large-v3-turbo';
-export const downloadWhisperModel = async (_args:downloadWhisperModel) => {
- throw new Error("cjs not supported")
+export interface DownloadWhisperModel {
+ model: WhisperModel;
+ onProgress?: (progress: number) => void;
}
+// Ensures downloadWhisperModel() returns a Promise that resolves to a string
+export const downloadWhisperModel = async (
+ _args: DownloadWhisperModel,
+): Promise<string> => {
+ throw new Error('cjs not supported');
+};