From 57b9055bbb1e91dfe889d8b9313309d3df395028 Mon Sep 17 00:00:00 2001 From: lutangar Date: Thu, 30 May 2024 16:14:40 +0200 Subject: [PATCH] chore(transcription): make fromPath method async --- .../whisper/transcriber/openai-transcriber.spec.ts | 2 +- .../whisper/transcriber/timestamped-transcriber.spec.ts | 8 ++++++-- .../whisper/transcriber/whisper-ctranslate2.spec.ts | 4 ++-- packages/transcription/src/transcription-model.ts | 6 +++--- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/tests/src/transcription/whisper/transcriber/openai-transcriber.spec.ts b/packages/tests/src/transcription/whisper/transcriber/openai-transcriber.spec.ts index 54fe18b9e3c..12a88e565e5 100644 --- a/packages/tests/src/transcription/whisper/transcriber/openai-transcriber.spec.ts +++ b/packages/tests/src/transcription/whisper/transcriber/openai-transcriber.spec.ts @@ -74,7 +74,7 @@ describe('Open AI Whisper transcriber', function () { this.timeout(3 * 1000 * 60) await transcriber.transcribe({ mediaFilePath: frVideoPath, - model: TranscriptionModel.fromPath(buildAbsoluteFixturePath('transcription/models/tiny.pt')), + model: await TranscriptionModel.fromPath(buildAbsoluteFixturePath('transcription/models/tiny.pt')), language: 'en' }) }) diff --git a/packages/tests/src/transcription/whisper/transcriber/timestamped-transcriber.spec.ts b/packages/tests/src/transcription/whisper/transcriber/timestamped-transcriber.spec.ts index 41603977b73..f9910490372 100644 --- a/packages/tests/src/transcription/whisper/transcriber/timestamped-transcriber.spec.ts +++ b/packages/tests/src/transcription/whisper/transcriber/timestamped-transcriber.spec.ts @@ -67,7 +67,11 @@ describe('Linto timestamped Whisper transcriber', function () { it('May transcribe a media file using a local PyTorch model file', async function () { this.timeout(2 * 1000 * 60) - await transcriber.transcribe({ mediaFilePath: frVideoPath, model: TranscriptionModel.fromPath(buildAbsoluteFixturePath('transcription/models/tiny.pt')), language: 'en' }) + await transcriber.transcribe({ + mediaFilePath: frVideoPath, + model: await TranscriptionModel.fromPath(buildAbsoluteFixturePath('transcription/models/tiny.pt')), + language: 'en' + }) }) it('May transcribe a media file in french', async function () { @@ -93,7 +97,7 @@ describe('Linto timestamped Whisper transcriber', function () { this.timeout(5 * 1000 * 60) const transcribeArgs: WhisperTranscribeArgs = { mediaFilePath: frVideoPath, - model: TranscriptionModel.fromPath(buildAbsoluteFixturePath('transcription/models/tiny.pt')), + model: await TranscriptionModel.fromPath(buildAbsoluteFixturePath('transcription/models/tiny.pt')), language: 'fr', format: 'txt' } diff --git a/packages/tests/src/transcription/whisper/transcriber/whisper-ctranslate2.spec.ts b/packages/tests/src/transcription/whisper/transcriber/whisper-ctranslate2.spec.ts index 6ccc10ad9f0..e258378bc79 100644 --- a/packages/tests/src/transcription/whisper/transcriber/whisper-ctranslate2.spec.ts +++ b/packages/tests/src/transcription/whisper/transcriber/whisper-ctranslate2.spec.ts @@ -75,7 +75,7 @@ describe('Whisper CTranslate2 transcriber', function () { this.timeout(2 * 1000 * 60) const transcript = await transcriber.transcribe({ mediaFilePath: shortVideoPath, - model: TranscriptionModel.fromPath(buildAbsoluteFixturePath('transcription/models/faster-whisper-tiny')), + model: await TranscriptionModel.fromPath(buildAbsoluteFixturePath('transcription/models/faster-whisper-tiny')), language: 'en', format: 'txt' }) @@ -104,7 +104,7 @@ describe('Whisper CTranslate2 transcriber', function () { this.timeout(5 * 1000 * 60) const transcribeArgs: WhisperTranscribeArgs = { mediaFilePath: frVideoPath, - model: TranscriptionModel.fromPath(buildAbsoluteFixturePath('transcription/models/tiny.pt')), + model: await TranscriptionModel.fromPath(buildAbsoluteFixturePath('transcription/models/tiny.pt')), language: 'fr', format: 'txt' } diff --git a/packages/transcription/src/transcription-model.ts b/packages/transcription/src/transcription-model.ts index 830dd5dbf22..01f3bdd4a9f 100644 --- a/packages/transcription/src/transcription-model.ts +++ b/packages/transcription/src/transcription-model.ts @@ -1,5 +1,5 @@ import assert from 'node:assert' -import { existsSync } from 'node:fs' +import { stat } from 'node:fs/promises' import { parse } from 'node:path' export type ModelFormat = 'PyTorch' | 'GGML' | 'ONNX' | 'CTranslate2' // CoreML, OpenVino, Scikit-Learn, TensorFlow/Keras, PySpark @@ -26,8 +26,8 @@ export class TranscriptionModel { this.format = format } - static fromPath (path: string) { - assert(existsSync(path), `${path} doesn't exist.`) + static async fromPath (path: string) { + assert(await stat(path), `${path} doesn't exist.`) return new TranscriptionModel(parse(path).name, path) }