Skip to content

Commit

Permalink
feat: add validation and some default values
Browse files Browse the repository at this point in the history
  • Loading branch information
ChetanXpro committed Aug 1, 2023
1 parent 04b558f commit f3528aa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "nodejs-whisper",
"version": "0.0.3",
"version": "0.0.6",
"description": "Node bindings for OpenAI's Whisper. Optimized for CPU.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"publishConfig": {
"access": "public"
},
Expand Down
21 changes: 17 additions & 4 deletions src/WhisperHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import fs from 'fs'
import { MODELS, MODELS_LIST, MODEL_OBJECT } from './constants'

export const constructCommand = (filePath: string, args: IOptions) => {
if (args?.modelName == undefined) {
throw new Error('[Nodejs-whisper] Error: Provide model name')
}

let anyModelExist = []

MODELS.forEach(model => {
if (!fs.existsSync(path.join(__dirname, '..', 'cpp', 'whisper.cpp', 'models', MODEL_OBJECT[args.modelName]))) {
if (!fs.existsSync(path.join(__dirname, '..', 'cpp', 'whisper.cpp', 'models', MODEL_OBJECT[args?.modelName]))) {
} else {
anyModelExist.push(model)
}
Expand All @@ -20,10 +24,19 @@ export const constructCommand = (filePath: string, args: IOptions) => {

const modelName = MODEL_OBJECT[args.modelName as keyof typeof MODEL_OBJECT]

if (MODELS_LIST.indexOf(args.modelName) === -1) {
if (MODELS_LIST.indexOf(args?.modelName) === -1) {
throw new Error('[Nodejs-whisper] Error: Model not found')
}
let command = `./main ${constructOptionsFlags(args)} -l auto -m ./models/${modelName} -f ${filePath} `

const defaultOutput =
!args?.whisperOptions?.outputInCsv &&
!args?.whisperOptions?.outputInSrt &&
!args?.whisperOptions?.outputInText &&
!args?.whisperOptions?.outputInVtt

let command = `./main ${constructOptionsFlags(args)} ${
defaultOutput && '-osrt'
} -l auto -m ./models/${modelName} -f ${filePath} `

return command
}
Expand Down Expand Up @@ -51,7 +64,7 @@ const constructOptionsFlags = (args: IOptions) => {
if (args?.whisperOptions?.timestamps_length) {
flag += `-ml ${args.whisperOptions.timestamps_length} `
}
if (args.whisperOptions.splitOnWord) {
if (args?.whisperOptions?.splitOnWord) {
flag += `-sow true `
}
return flag
Expand Down
17 changes: 17 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface WhisperOptions {
outputInText?: boolean
outputInVtt?: boolean
outputInSrt?: boolean
outputInCsv?: boolean
translateToEnglish?: boolean
timestamps_length?: number
wordTimestamps?: boolean
splitOnWord?: boolean
}

export interface IOptions {
modelName: string
whisperOptions?: WhisperOptions
}

export declare function nodewhisper(filePath: string, options: IOptions): Promise<string>
17 changes: 0 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import downloadModel from './downloadModel'

import { constructCommand } from './WhisperHelper'
import { checkIfFileExists, convertToWavType } from './utils'
import path from 'path'

export interface IOptions {
modelName: string
Expand All @@ -27,19 +26,3 @@ export async function nodewhisper(filePath: string, options: IOptions) {

return transcript
}

// const filePath = path.resolve(__dirname, '..', 'test.wav')

// nodewhisper(filePath, {
// modelName: 'base.en',
// whisperOptions: {
// outputInText: false,
// outputInVtt: false,
// outputInSrt: true,
// outputInCsv: false,
// translateToEnglish: false,
// wordTimestamps: false,
// timestamps_length: 20,
// splitOnWord: true,
// },
// })
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"esModuleInterop": true,
"target": "es6",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"outDir": "dist"
},
Expand Down

0 comments on commit f3528aa

Please sign in to comment.