Skip to content

Commit f3528aa

Browse files
committed
feat: add validation and some default values
1 parent 04b558f commit f3528aa

5 files changed

Lines changed: 37 additions & 22 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name": "nodejs-whisper",
3-
"version": "0.0.3",
3+
"version": "0.0.6",
44
"description": "Node bindings for OpenAI's Whisper. Optimized for CPU.",
55
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
67
"publishConfig": {
78
"access": "public"
89
},

src/WhisperHelper.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ import fs from 'fs'
44
import { MODELS, MODELS_LIST, MODEL_OBJECT } from './constants'
55

66
export const constructCommand = (filePath: string, args: IOptions) => {
7+
if (args?.modelName == undefined) {
8+
throw new Error('[Nodejs-whisper] Error: Provide model name')
9+
}
10+
711
let anyModelExist = []
812

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

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

23-
if (MODELS_LIST.indexOf(args.modelName) === -1) {
27+
if (MODELS_LIST.indexOf(args?.modelName) === -1) {
2428
throw new Error('[Nodejs-whisper] Error: Model not found')
2529
}
26-
let command = `./main ${constructOptionsFlags(args)} -l auto -m ./models/${modelName} -f ${filePath} `
30+
31+
const defaultOutput =
32+
!args?.whisperOptions?.outputInCsv &&
33+
!args?.whisperOptions?.outputInSrt &&
34+
!args?.whisperOptions?.outputInText &&
35+
!args?.whisperOptions?.outputInVtt
36+
37+
let command = `./main ${constructOptionsFlags(args)} ${
38+
defaultOutput && '-osrt'
39+
} -l auto -m ./models/${modelName} -f ${filePath} `
2740

2841
return command
2942
}
@@ -51,7 +64,7 @@ const constructOptionsFlags = (args: IOptions) => {
5164
if (args?.whisperOptions?.timestamps_length) {
5265
flag += `-ml ${args.whisperOptions.timestamps_length} `
5366
}
54-
if (args.whisperOptions.splitOnWord) {
67+
if (args?.whisperOptions?.splitOnWord) {
5568
flag += `-sow true `
5669
}
5770
return flag

src/index.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export interface WhisperOptions {
2+
outputInText?: boolean
3+
outputInVtt?: boolean
4+
outputInSrt?: boolean
5+
outputInCsv?: boolean
6+
translateToEnglish?: boolean
7+
timestamps_length?: number
8+
wordTimestamps?: boolean
9+
splitOnWord?: boolean
10+
}
11+
12+
export interface IOptions {
13+
modelName: string
14+
whisperOptions?: WhisperOptions
15+
}
16+
17+
export declare function nodewhisper(filePath: string, options: IOptions): Promise<string>

src/index.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import downloadModel from './downloadModel'
44

55
import { constructCommand } from './WhisperHelper'
66
import { checkIfFileExists, convertToWavType } from './utils'
7-
import path from 'path'
87

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

2827
return transcript
2928
}
30-
31-
// const filePath = path.resolve(__dirname, '..', 'test.wav')
32-
33-
// nodewhisper(filePath, {
34-
// modelName: 'base.en',
35-
// whisperOptions: {
36-
// outputInText: false,
37-
// outputInVtt: false,
38-
// outputInSrt: true,
39-
// outputInCsv: false,
40-
// translateToEnglish: false,
41-
// wordTimestamps: false,
42-
// timestamps_length: 20,
43-
// splitOnWord: true,
44-
// },
45-
// })

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"esModuleInterop": true,
55
"target": "es6",
66
"moduleResolution": "node",
7+
"declaration": true,
78
"sourceMap": true,
89
"outDir": "dist"
910
},

0 commit comments

Comments
 (0)