Skip to content

Commit

Permalink
fix: Current directory remains to be whisper.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
chetan authored and chetan committed Oct 29, 2023
1 parent af9a4f2 commit fec5527
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cpp/whisper.cpp
Submodule whisper.cpp updated from a792c4 to 54c978
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodejs-whisper",
"version": "0.1.3",
"version": "0.1.5",
"description": "Node bindings for OpenAI's Whisper. Optimized for CPU.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
4 changes: 4 additions & 0 deletions src/autoDownloadModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MODELS_LIST, MODELS } from './constants'
import fs from 'fs'

export default async function autoDownloadModel(autoDownloadModelName?: string) {
const projectDir = process.cwd()
try {
if (autoDownloadModelName) {
if (!MODELS_LIST.includes(autoDownloadModelName))
Expand All @@ -23,6 +24,8 @@ export default async function autoDownloadModel(autoDownloadModelName?: string)

return new Promise((resolve, reject) => {
if (anyModelExist.length > 0) {
console.log('[Nodejs-whisper] Models already exist. Skipping download.')

resolve('Models already exist. Skipping download.')

// console.log('Models already exist. Skipping download.')
Expand All @@ -49,6 +52,7 @@ export default async function autoDownloadModel(autoDownloadModelName?: string)
} catch (error) {
console.log('[Nodejs-whisper] Error Caught in downloadModel\n')
console.log(error)
shell.cd(projectDir)
return error
}
}
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { WhisperOptions } from './types'
import { executeCppCommand } from './whisper'
// import downloadModel from './downloadModel'

import { constructCommand } from './WhisperHelper'
import { checkIfFileExists, convertToWavType } from './utils'
Expand All @@ -19,8 +18,6 @@ export async function nodewhisper(filePath: string, options: IOptions) {
}

checkIfFileExists(filePath)
console.log(`[Nodejs-whisper] Transcribing file: ${filePath}\n`)
// await downloadModel()

const outputFilePath = await convertToWavType(filePath)

Expand All @@ -32,5 +29,9 @@ export async function nodewhisper(filePath: string, options: IOptions) {

const transcript = await executeCppCommand(command)

if (transcript.length === 0) {
throw new Error('Something went wrong while executing the command.')
}

return transcript
}
9 changes: 8 additions & 1 deletion src/whisper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ const defaultShellOptions = {
async: true,
}

const projectDir = process.cwd()

export async function whisperShell(command: string, options: IShellOptions = defaultShellOptions): Promise<any> {
return new Promise(async (resolve, reject) => {
try {
// docs: https://github.com/shelljs/shelljs#execcommand--options--callback
shell.exec(command, options, (code: number, stdout: string, stderr: string) => {
if (code === 0) {
if (stdout.match(/^error:/gm)) {
shell.cd(projectDir)
throw new Error('whisper.cpp error:\n' + stdout)
}
console.log('[Nodejs-whisper] Transcribing Done!')

shell.cd(projectDir)
resolve(stdout)
} else {
shell.cd(projectDir)
reject(stderr)
}
})
Expand All @@ -51,15 +55,18 @@ export const executeCppCommand = async (command: string) => {
" [Nodejs-whisper] 'make' command failed. Please run 'make' command in /whisper.cpp directory. Current shelljs directory: ",
__dirname
)
shell.cd(projectDir)
process.exit(1)
} else {
console.log("[Nodejs-whisper] 'make' command successful. Current directory: ", __dirname)

return await whisperShell(command, defaultShellOptions)
}
} else {
return await whisperShell(command, defaultShellOptions)
}
} catch (error) {
shell.cd(projectDir)
console.log('[Nodejs-whisper] Error in whisper.ts catch block.')
throw error
}
Expand Down

0 comments on commit fec5527

Please sign in to comment.