From 155aaca7a782676cc0c4d6d7cacd24e6d978ecb2 Mon Sep 17 00:00:00 2001 From: makaveli10 Date: Wed, 23 Aug 2023 19:35:57 +0530 Subject: [PATCH 1/3] close websocket after consuming file --- whisper_live/client.py | 1 + 1 file changed, 1 insertion(+) diff --git a/whisper_live/client.py b/whisper_live/client.py index e397c18..6c24b6d 100644 --- a/whisper_live/client.py +++ b/whisper_live/client.py @@ -192,6 +192,7 @@ class Client: self.wf.close() self.stream.close() + self.close_websocket() except KeyboardInterrupt: self.wf.close() From 82a1aa449d40e0af066f3bb5fcbb14a885cfa70e Mon Sep 17 00:00:00 2001 From: makaveli10 Date: Thu, 24 Aug 2023 10:07:44 +0530 Subject: [PATCH 2/3] wait to consume server responses --- whisper_live/client.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/whisper_live/client.py b/whisper_live/client.py index 6c24b6d..85c9f10 100644 --- a/whisper_live/client.py +++ b/whisper_live/client.py @@ -11,6 +11,7 @@ import textwrap import json import websocket import uuid +import time def resample(file: str, sr: int = 16000): @@ -54,6 +55,8 @@ class Client: task = "transcribe" uid = str(uuid.uuid4()) WAITING = False + LAST_RESPONSE_RECIEVED = None + DISCONNECT_IF_NO_RESPONSE_FOR = 15 def __init__(self, host=None, port=None, is_multilingual=False, lang=None, translate=False): Client.multilingual = is_multilingual @@ -92,6 +95,7 @@ class Client: @staticmethod def on_message(ws, message): + Client.LAST_RESPONSE_RECIEVED = time.time() message = json.loads(message) if message.get('uid')!=Client.uid: print("[ERROR]: invalid client uid") @@ -203,6 +207,8 @@ class Client: print("[INFO]: Keyboard interrupt.") def close_websocket(self): + while time.time() - self.LAST_RESPONSE_RECIEVED < self.DISCONNECT_IF_NO_RESPONSE_FOR: + continue try: self.client_socket.close() # Close the WebSocket connection except Exception as e: From 3860c67bef133bb5f4b3a60599de76a7bb3aa5de Mon Sep 17 00:00:00 2001 From: makaveli10 Date: Fri, 25 Aug 2023 00:05:34 +0530 Subject: [PATCH 3/3] check elapsed time before closing stream --- whisper_live/client.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/whisper_live/client.py b/whisper_live/client.py index 85c9f10..663f579 100644 --- a/whisper_live/client.py +++ b/whisper_live/client.py @@ -195,6 +195,9 @@ class Client: self.stream.write(data) self.wf.close() + elapsed_time = time.time() - self.LAST_RESPONSE_RECIEVED + while elapsed_time < self.DISCONNECT_IF_NO_RESPONSE_FOR: + continue self.stream.close() self.close_websocket() @@ -207,8 +210,6 @@ class Client: print("[INFO]: Keyboard interrupt.") def close_websocket(self): - while time.time() - self.LAST_RESPONSE_RECIEVED < self.DISCONNECT_IF_NO_RESPONSE_FOR: - continue try: self.client_socket.close() # Close the WebSocket connection except Exception as e: