Update speech_processor.py
parent
c0c3c7405d
commit
b68e390d4c
|
@ -2,120 +2,141 @@
|
||||||
import vosk
|
import vosk
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
|
import struct
|
||||||
|
import numpy as np
|
||||||
|
from queue import Queue
|
||||||
|
from threading import Thread
|
||||||
|
import soundfile as sf
|
||||||
import tempfile
|
import tempfile
|
||||||
import os
|
import os
|
||||||
import wave
|
|
||||||
import soundfile as sf
|
|
||||||
|
|
||||||
# Global model - load once
|
# Global recognizer
|
||||||
model = None
|
|
||||||
recognizer = None
|
recognizer = None
|
||||||
|
audio_queue = Queue()
|
||||||
|
result_queue = Queue()
|
||||||
|
|
||||||
def initialize_vosk():
|
def initialize_vosk():
|
||||||
"""Initialize Vosk model"""
|
global recognizer
|
||||||
global model, recognizer
|
model_path = "vosk-model" # Update this path
|
||||||
|
|
||||||
model_path = "/app/vosk-model"
|
|
||||||
if not os.path.exists(model_path):
|
if not os.path.exists(model_path):
|
||||||
return {"success": False, "error": "Vosk model not found at /app/vosk-model"}
|
return {"success": False, "error": "Model not found"}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
vosk.SetLogLevel(-1) # Reduce log verbosity
|
vosk.SetLogLevel(-1)
|
||||||
model = vosk.Model(model_path)
|
model = vosk.Model(model_path)
|
||||||
recognizer = vosk.KaldiRecognizer(model, 16000)
|
recognizer = vosk.KaldiRecognizer(model, 16000)
|
||||||
return {"success": True}
|
return {"success": True}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"success": False, "error": f"Failed to initialize Vosk: {str(e)}"}
|
return {"success": False, "error": str(e)}
|
||||||
|
|
||||||
def process_audio_chunk(audio_data):
|
def audio_worker():
|
||||||
"""Process audio data and return transcription"""
|
|
||||||
global recognizer
|
global recognizer
|
||||||
|
while True:
|
||||||
if not recognizer:
|
audio_data, request_id = audio_queue.get()
|
||||||
init_result = initialize_vosk()
|
|
||||||
if not init_result["success"]:
|
|
||||||
return init_result
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Write audio data to temporary file
|
# Write to temp file and read with soundfile
|
||||||
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as temp_file:
|
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
|
||||||
temp_file.write(audio_data)
|
f.write(audio_data)
|
||||||
temp_filename = temp_file.name
|
temp_path = f.name
|
||||||
|
|
||||||
# Read audio file with soundfile
|
|
||||||
try:
|
try:
|
||||||
audio_data, sample_rate = sf.read(temp_filename)
|
data, samplerate = sf.read(temp_path, dtype='float32')
|
||||||
|
|
||||||
# Convert to 16-bit PCM at 16kHz if needed
|
# Resample if needed
|
||||||
if sample_rate != 16000:
|
if samplerate != 16000:
|
||||||
# Simple resampling (for better quality, use librosa)
|
duration = len(data) / samplerate
|
||||||
import numpy as np
|
data = np.interp(
|
||||||
audio_data = np.interp(
|
np.linspace(0, len(data)-1, int(duration * 16000)),
|
||||||
np.linspace(0, len(audio_data), int(len(audio_data) * 16000 / sample_rate)),
|
np.arange(len(data)),
|
||||||
np.arange(len(audio_data)),
|
data
|
||||||
audio_data
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Convert to bytes
|
# Convert to 16-bit PCM
|
||||||
audio_bytes = (audio_data * 32767).astype('int16').tobytes()
|
data = (data * 32767).astype('int16')
|
||||||
|
|
||||||
# Process with Vosk
|
# Process with Vosk
|
||||||
if recognizer.AcceptWaveform(audio_bytes):
|
if recognizer.AcceptWaveform(data.tobytes()):
|
||||||
result = json.loads(recognizer.Result())
|
text = json.loads(recognizer.Result()).get('text', '')
|
||||||
text = result.get('text', '')
|
is_final = True
|
||||||
else:
|
else:
|
||||||
result = json.loads(recognizer.PartialResult())
|
text = json.loads(recognizer.PartialResult()).get('partial', '')
|
||||||
text = result.get('partial', '')
|
is_final = False
|
||||||
|
|
||||||
# Clean up
|
result_queue.put(({
|
||||||
os.unlink(temp_filename)
|
"success": True,
|
||||||
|
"text": text,
|
||||||
|
"is_final": is_final,
|
||||||
|
"requestId": request_id
|
||||||
|
}, request_id))
|
||||||
|
|
||||||
return {"success": True, "text": text}
|
finally:
|
||||||
|
os.unlink(temp_path)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
os.unlink(temp_filename)
|
result_queue.put(({
|
||||||
return {"success": False, "error": f"Audio processing error: {str(e)}"}
|
"success": False,
|
||||||
|
"error": str(e),
|
||||||
except Exception as e:
|
"requestId": request_id
|
||||||
return {"success": False, "error": f"General error: {str(e)}"}
|
}, request_id))
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Main loop to process audio chunks from stdin"""
|
# Initialize Vosk
|
||||||
# Initialize Vosk on startup
|
|
||||||
init_result = initialize_vosk()
|
init_result = initialize_vosk()
|
||||||
if not init_result["success"]:
|
if not init_result["success"]:
|
||||||
error_response = json.dumps(init_result).encode('utf-8')
|
error = json.dumps({
|
||||||
sys.stdout.buffer.write(len(error_response).to_bytes(4, byteorder='big'))
|
"success": False,
|
||||||
sys.stdout.buffer.write(error_response)
|
"error": init_result["error"],
|
||||||
|
"requestId": 0
|
||||||
|
}).encode()
|
||||||
|
sys.stdout.buffer.write(struct.pack('>I', len(error)))
|
||||||
|
sys.stdout.buffer.write(error)
|
||||||
sys.stdout.buffer.flush()
|
sys.stdout.buffer.flush()
|
||||||
sys.exit(1)
|
return
|
||||||
|
|
||||||
|
# Start worker thread
|
||||||
|
Thread(target=audio_worker, daemon=True).start()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
# Read length of incoming data
|
# Read message length (4 bytes)
|
||||||
length_data = sys.stdin.buffer.read(4)
|
length_bytes = sys.stdin.buffer.read(4)
|
||||||
if not length_data:
|
if not length_bytes:
|
||||||
break
|
break
|
||||||
|
length = struct.unpack('>I', length_bytes)[0]
|
||||||
|
|
||||||
length = int.from_bytes(length_data, byteorder='big')
|
# Read request ID (4 bytes)
|
||||||
|
id_bytes = sys.stdin.buffer.read(4)
|
||||||
|
if not id_bytes:
|
||||||
|
break
|
||||||
|
request_id = struct.unpack('>I', id_bytes)[0]
|
||||||
|
|
||||||
# Read audio data
|
# Read audio data
|
||||||
audio_data = sys.stdin.buffer.read(length)
|
audio_data = sys.stdin.buffer.read(length)
|
||||||
|
if len(audio_data) != length:
|
||||||
|
break
|
||||||
|
|
||||||
# Process audio
|
# Add to processing queue
|
||||||
result = process_audio_chunk(audio_data)
|
audio_queue.put((audio_data, request_id))
|
||||||
|
|
||||||
# Send result back
|
# Check for results
|
||||||
response = json.dumps(result).encode('utf-8')
|
while not result_queue.empty():
|
||||||
sys.stdout.buffer.write(len(response).to_bytes(4, byteorder='big'))
|
result, res_id = result_queue.get()
|
||||||
|
response = json.dumps(result).encode()
|
||||||
|
sys.stdout.buffer.write(struct.pack('>I', len(response)))
|
||||||
|
sys.stdout.buffer.write(struct.pack('>I', res_id)))
|
||||||
sys.stdout.buffer.write(response)
|
sys.stdout.buffer.write(response)
|
||||||
sys.stdout.buffer.flush()
|
sys.stdout.buffer.flush()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error_result = {"success": False, "error": str(e)}
|
error = json.dumps({
|
||||||
response = json.dumps(error_result).encode('utf-8')
|
"success": False,
|
||||||
sys.stdout.buffer.write(len(response).to_bytes(4, byteorder='big'))
|
"error": str(e),
|
||||||
sys.stdout.buffer.write(response)
|
"requestId": request_id if 'request_id' in locals() else 0
|
||||||
|
}).encode()
|
||||||
|
sys.stdout.buffer.write(struct.pack('>I', len(error)))
|
||||||
|
sys.stdout.buffer.write(error)
|
||||||
sys.stdout.buffer.flush()
|
sys.stdout.buffer.flush()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue