create new method for handling a new connection; expcetion handling
This commit is contained in:
+28
-16
@@ -7,7 +7,7 @@ import logging
|
|||||||
import torch
|
import torch
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from websockets.sync.server import serve
|
from websockets.sync.server import serve
|
||||||
|
from websockets.exceptions import ConnectionClosed
|
||||||
from whisper_live.vad import VoiceActivityDetector
|
from whisper_live.vad import VoiceActivityDetector
|
||||||
from whisper_live.transcriber import WhisperModel
|
from whisper_live.transcriber import WhisperModel
|
||||||
try:
|
try:
|
||||||
@@ -186,6 +186,23 @@ class TranscriptionServer:
|
|||||||
frame_data = websocket.recv()
|
frame_data = websocket.recv()
|
||||||
return np.frombuffer(frame_data, dtype=np.float32)
|
return np.frombuffer(frame_data, dtype=np.float32)
|
||||||
|
|
||||||
|
def handle_new_connection(self, websocket, backend, faster_whisper_custom_model_path,
|
||||||
|
whisper_tensorrt_path, trt_multilingual):
|
||||||
|
logging.info("New client connected")
|
||||||
|
options = websocket.recv()
|
||||||
|
options = json.loads(options)
|
||||||
|
|
||||||
|
if self.client_manager.is_server_full(websocket, options):
|
||||||
|
websocket.close()
|
||||||
|
return
|
||||||
|
|
||||||
|
self.backend = backend
|
||||||
|
if self.backend == "tensorrt":
|
||||||
|
self.vad_detector = VoiceActivityDetector(frame_rate=self.RATE)
|
||||||
|
|
||||||
|
self.initialize_client(
|
||||||
|
websocket, options, faster_whisper_custom_model_path, whisper_tensorrt_path, trt_multilingual)
|
||||||
|
|
||||||
def recv_audio(self,
|
def recv_audio(self,
|
||||||
websocket,
|
websocket,
|
||||||
backend="faster_whisper",
|
backend="faster_whisper",
|
||||||
@@ -216,20 +233,9 @@ class TranscriptionServer:
|
|||||||
Raises:
|
Raises:
|
||||||
Exception: If there is an error during the audio frame processing.
|
Exception: If there is an error during the audio frame processing.
|
||||||
"""
|
"""
|
||||||
logging.info("New client connected")
|
try:
|
||||||
options = websocket.recv()
|
self.handle_new_connection(websocket, backend, faster_whisper_custom_model_path,
|
||||||
options = json.loads(options)
|
whisper_tensorrt_path, trt_multilingual)
|
||||||
|
|
||||||
if self.client_manager.is_server_full(websocket, options):
|
|
||||||
websocket.close()
|
|
||||||
return
|
|
||||||
|
|
||||||
self.backend = backend
|
|
||||||
if self.backend == "tensorrt":
|
|
||||||
self.vad_detector = VoiceActivityDetector(frame_rate=self.RATE)
|
|
||||||
|
|
||||||
self.initialize_client(
|
|
||||||
websocket, options, faster_whisper_custom_model_path, whisper_tensorrt_path, trt_multilingual)
|
|
||||||
|
|
||||||
while not self.client_manager.is_client_timeout(websocket):
|
while not self.client_manager.is_client_timeout(websocket):
|
||||||
try:
|
try:
|
||||||
@@ -250,7 +256,13 @@ class TranscriptionServer:
|
|||||||
self.cleanup(websocket)
|
self.cleanup(websocket)
|
||||||
websocket.close()
|
websocket.close()
|
||||||
break
|
break
|
||||||
|
except ConnectionClosed:
|
||||||
|
logging.info(f"Connection closed by client with path: {websocket.path}")
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
logging.error("Failed to decode JSON from client")
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Unexpected error: {str(e)}")
|
||||||
|
finally:
|
||||||
if self.client_manager.get_client(websocket):
|
if self.client_manager.get_client(websocket):
|
||||||
self.cleanup(websocket)
|
self.cleanup(websocket)
|
||||||
websocket.close()
|
websocket.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user