add model size option from server

This commit is contained in:
makaveli10
2023-12-14 22:08:12 +08:00
parent da72d03073
commit 09b18e8ab8
+51 -33
View File
@@ -101,9 +101,10 @@ class TranscriptionServer:
multilingual=options["multilingual"], multilingual=options["multilingual"],
language=options["language"], language=options["language"],
task=options["task"], task=options["task"],
client_uid=options["uid"] client_uid=options["uid"],
model_size=options["model_size"]
) )
self.clients[websocket] = client self.clients[websocket] = client
self.clients_start_time[websocket] = time.time() self.clients_start_time[websocket] = time.time()
@@ -127,7 +128,8 @@ class TranscriptionServer:
except Exception as e: except Exception as e:
logging.error(e) logging.error(e)
self.clients[websocket].cleanup() if self.clients[websocket].model_size is not None:
self.clients[websocket].cleanup()
self.clients.pop(websocket) self.clients.pop(websocket)
self.clients_start_time.pop(websocket) self.clients_start_time.pop(websocket)
logging.info("Connection Closed.") logging.info("Connection Closed.")
@@ -180,7 +182,16 @@ class ServeClient:
SERVER_READY = "SERVER_READY" SERVER_READY = "SERVER_READY"
DISCONNECT = "DISCONNECT" DISCONNECT = "DISCONNECT"
def __init__(self, websocket, task="transcribe", device=None, multilingual=False, language=None, client_uid=None): def __init__(
self,
websocket,
task="transcribe",
device=None,
multilingual=False,
language=None,
client_uid=None,
model_size="small"
):
""" """
Initialize a ServeClient instance. Initialize a ServeClient instance.
The Whisper model is initialized based on the client's language and device availability. The Whisper model is initialized based on the client's language and device availability.
@@ -199,11 +210,23 @@ class ServeClient:
self.client_uid = client_uid self.client_uid = client_uid
self.data = b"" self.data = b""
self.frames = b"" self.frames = b""
self.language = language if multilingual else "en" self.model_sizes = [
"tiny", "base", "small", "medium", "large-v2"
]
self.multilingual = multilingual
self.model_size = self.get_model_size(model_size)
self.language = language if self.multilingual else "en"
self.task = task self.task = task
self.websocket = websocket
device = "cuda" if torch.cuda.is_available() else "cpu" device = "cuda" if torch.cuda.is_available() else "cpu"
if self.model_size == None:
return
self.transcriber = WhisperModel( self.transcriber = WhisperModel(
"small" if multilingual else "small.en", self.model_size,
device=device, device=device,
compute_type="int8" if device=="cpu" else "float16", compute_type="int8" if device=="cpu" else "float16",
local_files_only=False, local_files_only=False,
@@ -228,7 +251,6 @@ class ServeClient:
self.pick_previous_segments = 2 self.pick_previous_segments = 2
# threading # threading
self.websocket = websocket
self.trans_thread = threading.Thread(target=self.speech_to_text) self.trans_thread = threading.Thread(target=self.speech_to_text)
self.trans_thread.start() self.trans_thread.start()
self.websocket.send( self.websocket.send(
@@ -240,34 +262,30 @@ class ServeClient:
) )
) )
def fill_output(self, output): def get_model_size(self, model_size):
""" """
Format the current incomplete transcription output by combining it with previous complete segments. Returns the whisper model size based on multilingual.
The resulting transcription is wrapped into two lines, each containing a maximum of 50 characters. """
if model_size not in self.model_sizes:
It ensures that the combined transcription fits within two lines, with a maximum of 50 characters per line. self.websocket.send(
Segments are concatenated in the order they exist in the list of previous segments, with the most json.dumps(
recent complete segment first and older segments prepended as needed to maintain the character limit. {
If a 3-second pause is detected in the previous segments, any text preceding it is discarded to ensure "uid": self.client_uid,
the transcription starts with the most recent complete content. The resulting transcription is returned "status": "ERROR",
as a single string. "message": f"Invalid model size {model_size}. Available choices: {self.model_sizes}"
}
Args: )
output(str): The current incomplete transcription segment. )
return None
Returns: if model_size == "large-v2":
str: A formatted transcription wrapped in two lines. self.multilingual = True
""" return model_size
text = ''
pick_prev = min(len(self.text), self.pick_previous_segments) if not self.multilingual:
for seg in self.text[-pick_prev:]: model_size = model_size + ".en"
# discard everything before a 3 second pause
if seg == '': return model_size
text = ''
else:
text += seg
wrapped = "".join(text + output)
return wrapped
def add_frames(self, frame_np): def add_frames(self, frame_np):
""" """