fix: Improve Whisper server startup with async polling and reduce logs
- Make server startup async to avoid Bun's 10s timeout - Add frontend polling to detect when server is ready - Use PowerShell Get-NetTCPConnection for reliable port detection - Add starting state to prevent multiple simultaneous starts - Reduce verbose logging, keep only essential info - Add dev-dist and nul to gitignore
This commit is contained in:
@@ -12,6 +12,7 @@ const WHISPER_SCRIPT = join(import.meta.dir, '..', 'whisper_server.py')
|
||||
interface WhisperState {
|
||||
enabled: boolean
|
||||
running: boolean
|
||||
starting: boolean // Prevents multiple simultaneous start attempts
|
||||
process: Subprocess | null
|
||||
model: string
|
||||
device: string
|
||||
@@ -20,8 +21,9 @@ interface WhisperState {
|
||||
const state: WhisperState = {
|
||||
enabled: false,
|
||||
running: false,
|
||||
starting: false,
|
||||
process: null,
|
||||
model: 'medium',
|
||||
model: 'large-v3',
|
||||
device: 'cuda'
|
||||
}
|
||||
|
||||
@@ -46,89 +48,104 @@ async function killProcessOnPort(port: number): Promise<void> {
|
||||
* Start the Whisper Python server
|
||||
*/
|
||||
export async function startWhisperServer(): Promise<boolean> {
|
||||
// Prevent multiple simultaneous start attempts
|
||||
if (state.starting) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (state.running && state.process) {
|
||||
console.log('[Whisper] Server already running')
|
||||
return true
|
||||
}
|
||||
|
||||
console.log('[Whisper] ====== STARTING (v3) ======')
|
||||
console.log('[Whisper] Script:', WHISPER_SCRIPT)
|
||||
state.starting = true
|
||||
console.log(`[Whisper] Starting (${state.model})...`)
|
||||
|
||||
// Kill any existing process on the port
|
||||
console.log('[Whisper] Cleaning up port', WHISPER_PORT)
|
||||
await killProcessOnPort(WHISPER_PORT)
|
||||
|
||||
try {
|
||||
// Use Bun.spawn with inherit to show logs directly in console
|
||||
const proc = Bun.spawn(['python', WHISPER_SCRIPT], {
|
||||
// -u flag disables Python output buffering for real-time logs
|
||||
const proc = Bun.spawn(['python', '-u', WHISPER_SCRIPT], {
|
||||
cwd: join(import.meta.dir, '..'),
|
||||
stdout: 'inherit',
|
||||
stderr: 'inherit',
|
||||
env: { ...process.env }
|
||||
env: { ...process.env, PYTHONUNBUFFERED: '1' }
|
||||
})
|
||||
|
||||
state.process = proc
|
||||
|
||||
// Wait a bit for the server to start, then check if port is listening
|
||||
await new Promise(resolve => setTimeout(resolve, 3000))
|
||||
// Wait a bit for the server to start
|
||||
await new Promise(resolve => setTimeout(resolve, 2000))
|
||||
|
||||
// Check if process is still running
|
||||
if (proc.exitCode !== null) {
|
||||
console.error('[Whisper] Process exited with code:', proc.exitCode)
|
||||
state.process = null
|
||||
state.starting = false
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if port is listening (simple TCP check)
|
||||
// Check if WebSocket is ready
|
||||
const isListening = await checkPort(WHISPER_PORT)
|
||||
|
||||
if (isListening) {
|
||||
console.log('[Whisper] Server started successfully on port', WHISPER_PORT)
|
||||
console.log('[Whisper] Ready')
|
||||
state.running = true
|
||||
state.enabled = true
|
||||
state.starting = false
|
||||
return true
|
||||
}
|
||||
|
||||
// Wait more if model is still loading (up to 90 seconds total)
|
||||
console.log('[Whisper] Waiting for model to load...')
|
||||
for (let i = 0; i < 30; i++) {
|
||||
// Wait more if model is still loading (up to 120 seconds total for large models)
|
||||
for (let i = 0; i < 40; i++) {
|
||||
await new Promise(resolve => setTimeout(resolve, 3000))
|
||||
|
||||
if (proc.exitCode !== null) {
|
||||
console.error('[Whisper] Process died while loading')
|
||||
console.error('[Whisper] Process died')
|
||||
state.process = null
|
||||
state.starting = false
|
||||
return false
|
||||
}
|
||||
|
||||
if (await checkPort(WHISPER_PORT)) {
|
||||
console.log('[Whisper] Server ready!')
|
||||
const ready = await checkPort(WHISPER_PORT)
|
||||
if (ready) {
|
||||
console.log('[Whisper] Ready')
|
||||
state.running = true
|
||||
state.enabled = true
|
||||
state.starting = false
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
console.log('[Whisper] Timeout waiting for server')
|
||||
console.error('[Whisper] Timeout (120s)')
|
||||
state.starting = false
|
||||
return false
|
||||
|
||||
} catch (err: any) {
|
||||
console.error('[Whisper] Failed to start:', err.message)
|
||||
console.error('[Whisper] Error:', err.message)
|
||||
state.process = null
|
||||
state.starting = false
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a port is listening using PowerShell
|
||||
* Check if Whisper WebSocket is ready using PowerShell
|
||||
*/
|
||||
async function checkPort(port: number): Promise<boolean> {
|
||||
try {
|
||||
const proc = Bun.spawn(['powershell', '-Command',
|
||||
`if (Get-NetTCPConnection -LocalPort ${port} -State Listen -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }`
|
||||
], { stdout: 'ignore', stderr: 'ignore' })
|
||||
const proc = Bun.spawn(['powershell', '-NoProfile', '-Command',
|
||||
`$c = Get-NetTCPConnection -LocalPort ${port} -State Listen -ErrorAction SilentlyContinue; if ($c) { Write-Output 'LISTENING' }`
|
||||
], {
|
||||
stdout: 'pipe',
|
||||
stderr: 'ignore'
|
||||
})
|
||||
|
||||
const exitCode = await proc.exited
|
||||
return exitCode === 0
|
||||
const output = await new Response(proc.stdout).text()
|
||||
await proc.exited
|
||||
|
||||
return output.trim() === 'LISTENING'
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
@@ -139,35 +156,43 @@ async function checkPort(port: number): Promise<boolean> {
|
||||
*/
|
||||
export function stopWhisperServer(): boolean {
|
||||
if (!state.process) {
|
||||
console.log('[Whisper] No server running')
|
||||
return true
|
||||
}
|
||||
|
||||
console.log('[Whisper] Stopping server...')
|
||||
|
||||
try {
|
||||
state.process.kill()
|
||||
state.process = null
|
||||
state.running = false
|
||||
state.enabled = false
|
||||
console.log('[Whisper] Server stopped')
|
||||
console.log('[Whisper] Stopped')
|
||||
return true
|
||||
} catch (err) {
|
||||
console.error('[Whisper] Error stopping server:', err)
|
||||
console.error('[Whisper] Stop error:', err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle Whisper server on/off
|
||||
* Toggle Whisper server on/off (async - returns immediately when starting)
|
||||
*/
|
||||
export async function toggleWhisperServer(): Promise<{ enabled: boolean; success: boolean }> {
|
||||
export async function toggleWhisperServer(): Promise<{ enabled: boolean; success: boolean; starting: boolean }> {
|
||||
// Prevent toggle while starting
|
||||
if (state.starting) {
|
||||
return { enabled: false, success: false, starting: true }
|
||||
}
|
||||
|
||||
if (state.enabled && state.running) {
|
||||
const success = stopWhisperServer()
|
||||
return { enabled: false, success }
|
||||
return { enabled: false, success, starting: false }
|
||||
} else {
|
||||
const success = await startWhisperServer()
|
||||
return { enabled: success, success }
|
||||
// Start server in background - don't await
|
||||
startWhisperServer().catch(err => {
|
||||
console.error('[Whisper] Start error:', err)
|
||||
state.starting = false
|
||||
})
|
||||
|
||||
// Return immediately - frontend will poll for status
|
||||
return { enabled: false, success: true, starting: true }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,26 +202,30 @@ export async function toggleWhisperServer(): Promise<{ enabled: boolean; success
|
||||
export async function getWhisperState(): Promise<{
|
||||
enabled: boolean
|
||||
running: boolean
|
||||
starting: boolean
|
||||
port: number
|
||||
model: string
|
||||
device: string
|
||||
}> {
|
||||
// Check if port is actually listening
|
||||
const isListening = await checkPort(WHISPER_PORT)
|
||||
// Check if port is actually listening (skip if starting to avoid interference)
|
||||
if (!state.starting) {
|
||||
const isListening = await checkPort(WHISPER_PORT)
|
||||
|
||||
// Sync state with reality
|
||||
if (isListening && !state.running) {
|
||||
state.running = true
|
||||
state.enabled = true
|
||||
} else if (!isListening && state.running) {
|
||||
state.running = false
|
||||
state.enabled = false
|
||||
state.process = null
|
||||
// Sync state with reality
|
||||
if (isListening && !state.running) {
|
||||
state.running = true
|
||||
state.enabled = true
|
||||
} else if (!isListening && state.running) {
|
||||
state.running = false
|
||||
state.enabled = false
|
||||
state.process = null
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
enabled: state.enabled,
|
||||
running: state.running,
|
||||
starting: state.starting,
|
||||
port: WHISPER_PORT,
|
||||
model: state.model,
|
||||
device: state.device
|
||||
|
||||
@@ -79,10 +79,13 @@ def convert_audio_to_wav(input_data: bytes, input_format: str = "webm") -> bytes
|
||||
# Configuration
|
||||
HOST = "localhost"
|
||||
PORT = 4104
|
||||
MODEL_SIZE = "large-v3" # tiny, base, small, medium, large-v2, large-v3
|
||||
MODEL_SIZE = "large-v3" # Best standard model for Spanish
|
||||
DEVICE = "cuda" # cuda or cpu
|
||||
COMPUTE_TYPE = "float16" # float16 for GPU, int8 for CPU
|
||||
|
||||
# Model display name (extract from path if needed)
|
||||
MODEL_NAME = MODEL_SIZE.split("/")[-1] if "/" in MODEL_SIZE else MODEL_SIZE
|
||||
|
||||
# Spanish context prompt to improve accuracy (Honduras Spanish + tech context)
|
||||
INITIAL_PROMPT = """Transcripción en español hondureño de un desarrollador de software.
|
||||
Contexto: programación, TypeScript, Vue, Python, comandos de terminal, código.
|
||||
@@ -109,7 +112,7 @@ async def load_model():
|
||||
return model
|
||||
|
||||
model_loading = True
|
||||
print(f"[Whisper] Loading model '{MODEL_SIZE}' on {DEVICE}...")
|
||||
print(f"[Whisper] Loading model '{MODEL_NAME}' on {DEVICE}...")
|
||||
|
||||
try:
|
||||
# Load model - this downloads on first run
|
||||
@@ -140,15 +143,11 @@ def transcribe_audio(audio_data: bytes, language: str = "es", is_webm: bool = Tr
|
||||
if model is None:
|
||||
return {"error": "Model not loaded"}
|
||||
|
||||
print(f"[Whisper] Received {len(audio_data)} bytes of audio data")
|
||||
|
||||
# Convert WebM to WAV if needed
|
||||
if is_webm:
|
||||
print("[Whisper] Converting WebM to WAV...")
|
||||
wav_data = convert_audio_to_wav(audio_data, "webm")
|
||||
if wav_data is None:
|
||||
return {"error": "Failed to convert audio format. Ensure ffmpeg is installed."}
|
||||
print(f"[Whisper] Converted to {len(wav_data)} bytes WAV")
|
||||
else:
|
||||
wav_data = audio_data
|
||||
|
||||
@@ -159,7 +158,6 @@ def transcribe_audio(audio_data: bytes, language: str = "es", is_webm: bool = Tr
|
||||
|
||||
try:
|
||||
# Transcribe with optimized parameters
|
||||
print(f"[Whisper] Transcribing {temp_path}...")
|
||||
segments, info = model.transcribe(
|
||||
temp_path,
|
||||
language=language,
|
||||
@@ -191,7 +189,6 @@ def transcribe_audio(audio_data: bytes, language: str = "es", is_webm: bool = Tr
|
||||
"text": segment.text
|
||||
})
|
||||
|
||||
print(f"[Whisper] Transcription result: '{text.strip()}'")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
@@ -201,7 +198,7 @@ def transcribe_audio(audio_data: bytes, language: str = "es", is_webm: bool = Tr
|
||||
"duration": info.duration,
|
||||
"segments": segments_list,
|
||||
"engine": "whisper-gpu",
|
||||
"model": MODEL_SIZE,
|
||||
"model": MODEL_NAME,
|
||||
"device": DEVICE
|
||||
}
|
||||
|
||||
@@ -218,7 +215,6 @@ def transcribe_audio(audio_data: bytes, language: str = "es", is_webm: bool = Tr
|
||||
|
||||
async def handle_client(websocket):
|
||||
"""Handle WebSocket client connection"""
|
||||
print(f"[Whisper] Client connected")
|
||||
|
||||
# Ensure model is loaded
|
||||
await load_model()
|
||||
@@ -226,7 +222,7 @@ async def handle_client(websocket):
|
||||
# Send ready message
|
||||
await websocket.send(json.dumps({
|
||||
"type": "ready",
|
||||
"model": MODEL_SIZE,
|
||||
"model": MODEL_NAME,
|
||||
"device": DEVICE
|
||||
}))
|
||||
|
||||
@@ -234,8 +230,6 @@ async def handle_client(websocket):
|
||||
async for message in websocket:
|
||||
if isinstance(message, bytes):
|
||||
# Binary audio data (likely WebM format from browser)
|
||||
print(f"[Whisper] Received {len(message)} bytes of binary audio")
|
||||
|
||||
# Transcribe in thread pool to not block
|
||||
loop = asyncio.get_event_loop()
|
||||
result = await loop.run_in_executor(
|
||||
@@ -260,8 +254,6 @@ async def handle_client(websocket):
|
||||
language = cmd.get("language", "es")
|
||||
is_partial = cmd.get("partial", False)
|
||||
|
||||
print(f"[Whisper] Transcribe request: {len(audio_data)} bytes, lang={language}, partial={is_partial}")
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
result = await loop.run_in_executor(
|
||||
None,
|
||||
@@ -283,7 +275,7 @@ async def handle_client(websocket):
|
||||
elif cmd.get("type") == "status":
|
||||
await websocket.send(json.dumps({
|
||||
"type": "status",
|
||||
"model": MODEL_SIZE,
|
||||
"model": MODEL_NAME,
|
||||
"device": DEVICE,
|
||||
"ready": model is not None
|
||||
}))
|
||||
@@ -295,21 +287,19 @@ async def handle_client(websocket):
|
||||
}))
|
||||
|
||||
except websockets.exceptions.ConnectionClosed:
|
||||
print("[Whisper] Client disconnected")
|
||||
pass
|
||||
except Exception as e:
|
||||
print(f"[Whisper] Error: {e}")
|
||||
|
||||
async def main():
|
||||
"""Start WebSocket server"""
|
||||
print(f"[Whisper] Starting server on ws://{HOST}:{PORT}")
|
||||
print(f"[Whisper] Model: {MODEL_SIZE}, Device: {DEVICE}")
|
||||
print(f"[Whisper] Model: {MODEL_NAME} | Device: {DEVICE} | Port: {PORT}")
|
||||
|
||||
# Pre-load model
|
||||
print("[Whisper] Pre-loading model...")
|
||||
await load_model()
|
||||
|
||||
async with websockets.serve(handle_client, HOST, PORT):
|
||||
print(f"[Whisper] Server ready! Listening on ws://{HOST}:{PORT}")
|
||||
print(f"[Whisper] Ready")
|
||||
await asyncio.Future() # Run forever
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user