diff --git a/src/WhisperHelper.ts b/src/WhisperHelper.ts index 066b5db..39cdee3 100644 --- a/src/WhisperHelper.ts +++ b/src/WhisperHelper.ts @@ -1,7 +1,7 @@ import path from 'path' import fs from 'fs' -import { MODELS, MODELS_LIST, MODEL_OBJECT } from './constants' +import { MODELS_LIST, MODEL_OBJECT, WHISPER_CPP_PATH, WHISPER_CPP_MAIN_PATH } from './constants' import { IOptions } from '.' export const constructCommand = (filePath: string, args: IOptions): string => { @@ -15,7 +15,7 @@ export const constructCommand = (filePath: string, args: IOptions): string => { errors.push(`[Nodejs-whisper] Error: Enter a valid model name. Available models are: ${MODELS_LIST.join(', ')}`) } - const modelPath = path.join(__dirname, '..', 'cpp', 'whisper.cpp', 'models', MODEL_OBJECT[args.modelName]) + const modelPath = path.join(WHISPER_CPP_PATH, 'models', MODEL_OBJECT[args.modelName]) if (!fs.existsSync(modelPath)) { errors.push( '[Nodejs-whisper] Error: Model file does not exist. Please ensure the model is downloaded and correctly placed.' @@ -27,7 +27,7 @@ export const constructCommand = (filePath: string, args: IOptions): string => { } const modelName = MODEL_OBJECT[args.modelName as keyof typeof MODEL_OBJECT] - let command = `./main ${constructOptionsFlags(args)} -l ${args.whisperOptions?.language ? args.whisperOptions?.language : 'auto'} -m "./models/${modelName}" -f "${filePath}"` + let command = `${WHISPER_CPP_MAIN_PATH} ${constructOptionsFlags(args)} -l ${args.whisperOptions?.language ? args.whisperOptions?.language : 'auto'} -m "./models/${modelName}" -f "${filePath}"` return command } diff --git a/src/autoDownloadModel.ts b/src/autoDownloadModel.ts index feadaaf..ba05c9e 100644 --- a/src/autoDownloadModel.ts +++ b/src/autoDownloadModel.ts @@ -1,7 +1,7 @@ import path from 'path' import shell from 'shelljs' import fs from 'fs' -import { MODELS_LIST, MODELS } from './constants' +import { MODEL_OBJECT, MODELS_LIST, WHISPER_CPP_PATH } from './constants' export default async function autoDownloadModel( logger = console, @@ -19,9 +19,9 @@ export default async function autoDownloadModel( } try { - const modelDirectory = path.join(__dirname, '..', 'cpp', 'whisper.cpp', 'models') + const modelDirectory = path.join(WHISPER_CPP_PATH, 'models') shell.cd(modelDirectory) - const modelAlreadyExist = fs.existsSync(path.join(modelDirectory, autoDownloadModelName)) + const modelAlreadyExist = fs.existsSync(path.join(modelDirectory, MODEL_OBJECT[autoDownloadModelName])) if (modelAlreadyExist) { logger.debug(`[Nodejs-whisper] ${autoDownloadModel} already exist. Skipping download.`) diff --git a/src/constants.ts b/src/constants.ts index 716a8fc..b4dee17 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,3 +1,5 @@ +import path from 'path' + export const MODELS_LIST = [ 'tiny', 'tiny.en', @@ -41,3 +43,8 @@ export const MODEL_OBJECT = { } export const DEFAULT_MODEL = 'tiny.en' + +export const WHISPER_CPP_PATH = path.join(__dirname, '..', 'cpp', 'whisper.cpp') + +export const WHISPER_CPP_MAIN_PATH = + process.platform === 'win32' ? 'build\\bin\\Release\\whisper-cli.exe' : './build/bin/whisper-cli' diff --git a/src/downloadModel.ts b/src/downloadModel.ts index afdd3d9..03f51db 100644 --- a/src/downloadModel.ts +++ b/src/downloadModel.ts @@ -5,7 +5,7 @@ import path from 'path' import shell from 'shelljs' import readlineSync from 'readline-sync' -import { MODELS_LIST, DEFAULT_MODEL, MODELS } from './constants' +import { MODELS_LIST, DEFAULT_MODEL, MODELS, WHISPER_CPP_PATH, MODEL_OBJECT } from './constants' import fs from 'fs' const askForModel = async (logger = console): Promise => { @@ -48,12 +48,12 @@ const askIfUserWantToUseCuda = async (logger = console) => { async function downloadModel(logger = console) { try { - shell.cd(path.join(__dirname, '..', './cpp/whisper.cpp/models')) + shell.cd(path.join(WHISPER_CPP_PATH, 'models')) let anyModelExist = [] MODELS.forEach(model => { - if (!fs.existsSync(path.join(__dirname, '..', `./cpp/whisper.cpp/models/${model}`))) { + if (!fs.existsSync(path.join(WHISPER_CPP_PATH, 'models', MODEL_OBJECT[model]))) { } else { anyModelExist.push(model) } diff --git a/src/whisper.ts b/src/whisper.ts index b4486c7..1a37f61 100644 --- a/src/whisper.ts +++ b/src/whisper.ts @@ -1,10 +1,6 @@ -import fs from 'fs' -import path from 'path' import shell from 'shelljs' -import { MODELS } from './constants' +import { WHISPER_CPP_PATH, WHISPER_CPP_MAIN_PATH } from './constants' -const WHISPER_CPP_PATH = path.join(__dirname, '..', 'cpp', 'whisper.cpp') -const WHISPER_CPP_MAIN_PATH = './main' const projectDir = process.cwd() export interface IShellOptions { @@ -57,13 +53,13 @@ export async function whisperShell( export async function executeCppCommand(command: string, logger = console, withCuda: boolean): Promise { try { shell.cd(WHISPER_CPP_PATH) - if (!shell.which(WHISPER_CPP_MAIN_PATH)) { + if (!shell.which(WHISPER_CPP_MAIN_PATH.replace(/\\/g, '/'))) { logger.debug('[Nodejs-whisper] whisper.cpp not initialized.') const makeCommand = withCuda ? 'WHISPER_CUDA=1 make -j' : 'make -j' shell.exec(makeCommand) - if (!shell.which(WHISPER_CPP_MAIN_PATH)) { + if (!shell.which(WHISPER_CPP_MAIN_PATH.replace(/\\/g, '/'))) { throw new Error( "[Nodejs-whisper] 'make' command failed. Please run 'make' command in /whisper.cpp directory." )