Skip to content

Commit e162f3d

Browse files
committed
fix: async whisper transcription to prevent blocking vite server
1 parent 7cb9dd8 commit e162f3d

1 file changed

Lines changed: 31 additions & 7 deletions

File tree

vite.config.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ function comfyLauncher(): Plugin {
10141014

10151015
const chunks: Buffer[] = []
10161016
req.on('data', (chunk: Buffer) => chunks.push(chunk))
1017-
req.on('end', () => {
1017+
req.on('end', async () => {
10181018
try {
10191019
const audioBuffer = Buffer.concat(chunks)
10201020
if (audioBuffer.length === 0) {
@@ -1066,16 +1066,40 @@ function comfyLauncher(): Plugin {
10661066
"print('LANG:' + result.get('language', 'en'))\n"
10671067
)
10681068

1069+
// Use async spawn instead of execSync to avoid blocking the server
1070+
const runWhisper = (scriptPath: string, timeoutMs: number): Promise<string> => {
1071+
return new Promise((resolve, reject) => {
1072+
const proc = spawn(pythonBin, [scriptPath], {
1073+
shell: true,
1074+
stdio: ['ignore', 'pipe', 'pipe'],
1075+
})
1076+
let stdout = ''
1077+
let stderr = ''
1078+
const timer = setTimeout(() => {
1079+
try { proc.kill('SIGKILL') } catch {}
1080+
reject(new Error('Whisper timed out'))
1081+
}, timeoutMs)
1082+
proc.stdout?.on('data', (d: Buffer) => { stdout += d.toString() })
1083+
proc.stderr?.on('data', (d: Buffer) => { stderr += d.toString() })
1084+
proc.on('close', (code: number) => {
1085+
clearTimeout(timer)
1086+
if (code === 0) resolve(stdout.trim())
1087+
else reject(new Error(stderr || 'Exit code ' + code))
1088+
})
1089+
proc.on('error', (err: Error) => {
1090+
clearTimeout(timer)
1091+
reject(err)
1092+
})
1093+
})
1094+
}
1095+
10691096
let transcript = ''
10701097
let language = 'en'
10711098
let success = false
10721099

1073-
// Try faster-whisper
1100+
// Try faster-whisper (async)
10741101
try {
1075-
const output = execSync(`${pythonBin} ${fwScript}`, {
1076-
encoding: 'utf8',
1077-
timeout: 120000,
1078-
}).trim()
1102+
const output = await runWhisper(fwScript, 120000)
10791103
const lines = output.split('\n')
10801104
const langLine = lines.find((l: string) => l.startsWith('LANG:'))
10811105
if (langLine) {
@@ -1085,7 +1109,7 @@ function comfyLauncher(): Plugin {
10851109
transcript = lines.join(' ').trim()
10861110
success = true
10871111
} catch (fwErr) {
1088-
console.error('[Transcribe] faster-whisper failed:', (fwErr as any).stderr || (fwErr as any).message)
1112+
console.error('[Transcribe] faster-whisper failed:', (fwErr as any).message)
10891113
/* try fallback */
10901114
}
10911115

0 commit comments

Comments
 (0)