Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update faster whisper #64

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements/server.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PyAudio
faster-whisper==0.6.0
faster-whisper==0.9.0
--extra-index-url https://download.pytorch.org/whl/cu111
torch==1.10.1
torchaudio==0.10.1
Expand Down
2 changes: 1 addition & 1 deletion whisper_live/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__="0.0.7"
__version__="0.0.8"
87 changes: 27 additions & 60 deletions whisper_live/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import numpy as np
import time
from whisper_live.transcriber import WhisperModel
from whisper_live.vad import VoiceActivityDetection


class TranscriptionServer:
Expand All @@ -35,8 +34,6 @@ class TranscriptionServer:

def __init__(self):
# voice activity detection model
self.vad_model = VoiceActivityDetection()
self.vad_threshold = 0.4

self.clients = {}
self.websockets = {}
Expand Down Expand Up @@ -115,15 +112,6 @@ def recv_audio(self, websocket):
frame_data = websocket.recv()
frame_np = np.frombuffer(frame_data, dtype=np.float32)

try:
speech_prob = self.vad_model(torch.from_numpy(frame_np.copy()), self.RATE).item()
if speech_prob < self.vad_threshold:
continue

except Exception as e:
logging.error(e)
return

self.clients[websocket].add_frames(frame_np)

elapsed_time = time.time() - self.clients_start_time[websocket]
Expand Down Expand Up @@ -322,25 +310,6 @@ def speech_to_text(self):
Exception: If there is an issue with audio processing or WebSocket communication.

"""
# detect language
if self.language is None:
# wait for 30s of audio
while self.frames_np is None or self.frames_np.shape[0] < 30*self.RATE:
time.sleep(1)
input_bytes = self.frames_np[-30*self.RATE:].copy()
self.frames_np = None
duration = input_bytes.shape[0] / self.RATE

self.language, lang_prob = self.transcriber.transcribe(
input_bytes,
initial_prompt=None,
language=self.language,
task=self.task
)
logging.info(f"Detected language {self.language} with probability {lang_prob}")
self.websocket.send(json.dumps(
{"uid": self.client_uid, "language": self.language, "language_prob": lang_prob}))

while True:
if self.exit:
logging.info("Exiting speech to text thread")
Expand All @@ -358,24 +327,31 @@ def speech_to_text(self):
samples_take = max(0, (self.timestamp_offset - self.frames_offset)*self.RATE)
input_bytes = self.frames_np[int(samples_take):].copy()
duration = input_bytes.shape[0] / self.RATE
if duration<1.0:
if duration<1.0:
continue
try:
input_sample = input_bytes.copy()
# set previous complete segment as initial prompt
if len(self.text) and self.text[-1] != '':
initial_prompt = self.text[-1]
else:
initial_prompt = None

# whisper transcribe with prompt
result = self.transcriber.transcribe(
result, info = self.transcriber.transcribe(
input_sample,
initial_prompt=initial_prompt,
initial_prompt=None,
language=self.language,
task=self.task
task=self.task,
vad_filter=True,
vad_parameters={"threshold": 0.5}
)

if self.language is None:
if info.language_probability > 0.5:
self.language = info.language
logging.info(f"Detected language {self.language} with probability {info.language_probability}")
self.websocket.send(json.dumps(
{"uid": self.client_uid, "language": self.language, "language_prob": info.language_probability}))
else:
# detect language again
continue

if len(result):
self.t_start = None
last_segment = self.update_segments(result, duration)
Expand All @@ -384,17 +360,7 @@ def speech_to_text(self):
else:
segments = self.transcript[-self.send_last_n_segments:]
if last_segment is not None:
segments = segments + [last_segment]

try:
self.websocket.send(
json.dumps({
"uid": self.client_uid,
"segments": segments
})
)
except Exception as e:
logging.error(f"[ERROR]: {e}")
segments = segments + [last_segment]
else:
# show previous output if there is pause i.e. no output from whisper
segments = []
Expand All @@ -410,15 +376,16 @@ def speech_to_text(self):
if time.time() - self.t_start > self.add_pause_thresh:
self.text.append('')

try:
self.websocket.send(
json.dumps({
"uid": self.client_uid,
"segments": segments
})
)
except Exception as e:
logging.error(f"[ERROR]: {e}")
try:
self.websocket.send(
json.dumps({
"uid": self.client_uid,
"segments": segments
})
)
except Exception as e:
logging.error(f"[ERROR]: {e}")

except Exception as e:
logging.error(f"[ERROR]: {e}")
time.sleep(0.01)
Expand Down
Loading
Loading