Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 076aebf3b6 | |||
| a7eedc5d84 | |||
| d91330d790 | |||
| 783d147316 | |||
| 058c93e55e | |||
| 2300eedc8b | |||
| cafcb04fbc | |||
| 72ead71eeb | |||
| 7b2f5cff72 | |||
| 32c6a565d7 | |||
| e30286c046 | |||
| e92ddd291a |
@@ -1 +1 @@
|
|||||||
__version__="0.0.10"
|
__version__="0.0.11"
|
||||||
|
|||||||
@@ -412,7 +412,7 @@ class Client:
|
|||||||
for _ in range(0, int(self.rate / self.chunk * self.record_seconds)):
|
for _ in range(0, int(self.rate / self.chunk * self.record_seconds)):
|
||||||
if not self.recording:
|
if not self.recording:
|
||||||
break
|
break
|
||||||
data = self.stream.read(self.chunk)
|
data = self.stream.read(self.chunk, exception_on_overflow = False)
|
||||||
self.frames += data
|
self.frames += data
|
||||||
|
|
||||||
audio_array = Client.bytes_to_float_array(data)
|
audio_array = Client.bytes_to_float_array(data)
|
||||||
|
|||||||
+33
-30
@@ -102,7 +102,9 @@ class TranscriptionServer:
|
|||||||
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"]
|
model_size=options["model_size"],
|
||||||
|
initial_prompt=options.get("initial_prompt"),
|
||||||
|
vad_parameters=options.get("vad_parameters")
|
||||||
)
|
)
|
||||||
|
|
||||||
self.clients[websocket] = client
|
self.clients[websocket] = client
|
||||||
@@ -118,7 +120,7 @@ class TranscriptionServer:
|
|||||||
elapsed_time = time.time() - self.clients_start_time[websocket]
|
elapsed_time = time.time() - self.clients_start_time[websocket]
|
||||||
if elapsed_time >= self.max_connection_time:
|
if elapsed_time >= self.max_connection_time:
|
||||||
self.clients[websocket].disconnect()
|
self.clients[websocket].disconnect()
|
||||||
logging.warning(f"{self.clients[websocket]} Client disconnected due to overtime.")
|
logging.warning(f"Client with uid '{self.clients[websocket].client_uid}' disconnected due to overtime.")
|
||||||
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)
|
||||||
@@ -127,13 +129,11 @@ class TranscriptionServer:
|
|||||||
break
|
break
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(e)
|
logging.info(f"[ERROR]: Client with uid '{self.clients[websocket].client_uid}' Disconnected.")
|
||||||
if self.clients[websocket].model_size is not None:
|
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)
|
||||||
logging.info("Connection Closed.")
|
|
||||||
logging.info(self.clients)
|
|
||||||
del websocket
|
del websocket
|
||||||
break
|
break
|
||||||
|
|
||||||
@@ -190,7 +190,9 @@ class ServeClient:
|
|||||||
multilingual=False,
|
multilingual=False,
|
||||||
language=None,
|
language=None,
|
||||||
client_uid=None,
|
client_uid=None,
|
||||||
model_size="small"
|
model_size="small",
|
||||||
|
initial_prompt=None,
|
||||||
|
vad_parameters=None
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Initialize a ServeClient instance.
|
Initialize a ServeClient instance.
|
||||||
@@ -218,6 +220,8 @@ class ServeClient:
|
|||||||
self.language = language if self.multilingual else "en"
|
self.language = language if self.multilingual else "en"
|
||||||
self.task = task
|
self.task = task
|
||||||
self.websocket = websocket
|
self.websocket = websocket
|
||||||
|
self.initial_prompt = initial_prompt
|
||||||
|
self.vad_parameters = vad_parameters or {"threshold": 0.5}
|
||||||
|
|
||||||
device = "cuda" if torch.cuda.is_available() else "cpu"
|
device = "cuda" if torch.cuda.is_available() else "cpu"
|
||||||
|
|
||||||
@@ -352,11 +356,11 @@ class ServeClient:
|
|||||||
# whisper transcribe with prompt
|
# whisper transcribe with prompt
|
||||||
result, info = self.transcriber.transcribe(
|
result, info = self.transcriber.transcribe(
|
||||||
input_sample,
|
input_sample,
|
||||||
initial_prompt=None,
|
initial_prompt=self.initial_prompt,
|
||||||
language=self.language,
|
language=self.language,
|
||||||
task=self.task,
|
task=self.task,
|
||||||
vad_filter=True,
|
vad_filter=True,
|
||||||
vad_parameters={"threshold": 0.5}
|
vad_parameters=self.vad_parameters
|
||||||
)
|
)
|
||||||
|
|
||||||
if self.language is None:
|
if self.language is None:
|
||||||
@@ -401,12 +405,20 @@ class ServeClient:
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"[ERROR]: {e}")
|
logging.error(f"[ERROR]: Failed to send message to client: {e}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"[ERROR]: {e}")
|
logging.error(f"[ERROR]: Failed to transcribe audio chunk: {e}")
|
||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
|
|
||||||
|
def format_segment(self, start, end, text):
|
||||||
|
"""Helper function to format a segment with string timestamps."""
|
||||||
|
return {
|
||||||
|
'start': "{:.3f}".format(start),
|
||||||
|
'end': "{:.3f}".format(end),
|
||||||
|
'text': text
|
||||||
|
}
|
||||||
|
|
||||||
def update_segments(self, segments, duration):
|
def update_segments(self, segments, duration):
|
||||||
"""
|
"""
|
||||||
Processes the segments from whisper. Appends all the segments to the list
|
Processes the segments from whisper. Appends all the segments to the list
|
||||||
@@ -437,22 +449,16 @@ class ServeClient:
|
|||||||
text_ = s.text
|
text_ = s.text
|
||||||
self.text.append(text_)
|
self.text.append(text_)
|
||||||
start, end = self.timestamp_offset + s.start, self.timestamp_offset + min(duration, s.end)
|
start, end = self.timestamp_offset + s.start, self.timestamp_offset + min(duration, s.end)
|
||||||
self.transcript.append(
|
self.transcript.append(self.format_segment(start, end, text_))
|
||||||
{
|
|
||||||
'start': start,
|
|
||||||
'end': end,
|
|
||||||
'text': text_
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
offset = min(duration, s.end)
|
offset = min(duration, s.end)
|
||||||
|
|
||||||
self.current_out += segments[-1].text
|
self.current_out += segments[-1].text
|
||||||
last_segment = {
|
last_segment = self.format_segment(
|
||||||
'start': self.timestamp_offset + segments[-1].start,
|
self.timestamp_offset + segments[-1].start,
|
||||||
'end': self.timestamp_offset + min(duration, segments[-1].end),
|
self.timestamp_offset + min(duration, segments[-1].end),
|
||||||
'text': self.current_out
|
self.current_out
|
||||||
}
|
)
|
||||||
|
|
||||||
# if same incomplete segment is seen multiple times then update the offset
|
# if same incomplete segment is seen multiple times then update the offset
|
||||||
# and append the segment to the list
|
# and append the segment to the list
|
||||||
@@ -464,13 +470,11 @@ class ServeClient:
|
|||||||
if self.same_output_threshold > 5:
|
if self.same_output_threshold > 5:
|
||||||
if not len(self.text) or self.text[-1].strip().lower()!=self.current_out.strip().lower():
|
if not len(self.text) or self.text[-1].strip().lower()!=self.current_out.strip().lower():
|
||||||
self.text.append(self.current_out)
|
self.text.append(self.current_out)
|
||||||
self.transcript.append(
|
self.transcript.append(self.format_segment(
|
||||||
{
|
self.timestamp_offset,
|
||||||
'start': self.timestamp_offset,
|
self.timestamp_offset + duration,
|
||||||
'end': self.timestamp_offset + duration,
|
self.current_out
|
||||||
'text': self.current_out
|
))
|
||||||
}
|
|
||||||
)
|
|
||||||
self.current_out = ''
|
self.current_out = ''
|
||||||
offset = duration
|
offset = duration
|
||||||
self.same_output_threshold = 0
|
self.same_output_threshold = 0
|
||||||
@@ -512,4 +516,3 @@ class ServeClient:
|
|||||||
"""
|
"""
|
||||||
logging.info("Cleaning up.")
|
logging.info("Cleaning up.")
|
||||||
self.exit = True
|
self.exit = True
|
||||||
self.transcriber.destroy()
|
|
||||||
|
|||||||
@@ -934,9 +934,6 @@ class WhisperModel:
|
|||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
def destroy(self):
|
|
||||||
del self.model
|
|
||||||
|
|
||||||
|
|
||||||
def restore_speech_timestamps(
|
def restore_speech_timestamps(
|
||||||
segments: Iterable[Segment],
|
segments: Iterable[Segment],
|
||||||
|
|||||||
Reference in New Issue
Block a user