add model size option from server

This commit is contained in:
makaveli10
2023-12-14 22:08:12 +08:00
parent da72d03073
commit 09b18e8ab8
+49 -31
View File
@@ -101,7 +101,8 @@ 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
@@ -127,6 +128,7 @@ class TranscriptionServer:
except Exception as e: except Exception as e:
logging.error(e) logging.error(e)
if self.clients[websocket].model_size is not None:
self.clients[websocket].cleanup() self.clients[websocket].cleanup()
self.clients.pop(websocket) self.clients.pop(websocket)
self.clients_start_time.pop(websocket) self.clients_start_time.pop(websocket)
@@ -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.
It ensures that the combined transcription fits within two lines, with a maximum of 50 characters per line.
Segments are concatenated in the order they exist in the list of previous segments, with the most
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
the transcription starts with the most recent complete content. The resulting transcription is returned
as a single string.
Args:
output(str): The current incomplete transcription segment.
Returns:
str: A formatted transcription wrapped in two lines.
""" """
text = '' if model_size not in self.model_sizes:
pick_prev = min(len(self.text), self.pick_previous_segments) self.websocket.send(
for seg in self.text[-pick_prev:]: json.dumps(
# discard everything before a 3 second pause {
if seg == '': "uid": self.client_uid,
text = '' "status": "ERROR",
else: "message": f"Invalid model size {model_size}. Available choices: {self.model_sizes}"
text += seg }
wrapped = "".join(text + output) )
return wrapped )
return None
if model_size == "large-v2":
self.multilingual = True
return model_size
if not self.multilingual:
model_size = model_size + ".en"
return model_size
def add_frames(self, frame_np): def add_frames(self, frame_np):
""" """