Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/WhisperHelper.ts
Original file line number Diff line number Diff line change
@@ -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 => {
Expand All @@ -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.'
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions src/autoDownloadModel.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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.`)
Expand Down
7 changes: 7 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path'

export const MODELS_LIST = [
'tiny',
'tiny.en',
Expand Down Expand Up @@ -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'
6 changes: 3 additions & 3 deletions src/downloadModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> => {
Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 3 additions & 7 deletions src/whisper.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -57,13 +53,13 @@ export async function whisperShell(
export async function executeCppCommand(command: string, logger = console, withCuda: boolean): Promise<string> {
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."
)
Expand Down
Loading