forked from saharmor/whisper-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request saharmor#40 from saharmor/fix/hallucinations
Fix hallucinations + bugfixes
- Loading branch information
Showing
13 changed files
with
212 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,71 @@ | ||
import logging | ||
from queue import Queue | ||
from silero_vad import silero_vad | ||
from diart.utils import decode_audio | ||
from utils import get_transcriber_information | ||
from transcription.whisper_transcriber import WhisperTranscriber | ||
from abc import abstractmethod | ||
from config import ClientState | ||
|
||
|
||
class Client: | ||
|
||
def __init__(self, sid, socket, transcriber, transcription_timeout): | ||
def __init__(self, sid, socket, config): | ||
self.sid = sid | ||
self.config = config | ||
self.diarization_pipeline = None | ||
self.transcriber = transcriber | ||
self.transcription_timeout = transcription_timeout | ||
self.transcriber = None | ||
self.transcription_timeout = None | ||
self.socket = socket | ||
self.audio_chunks = Queue() | ||
self.transcription_thread = None | ||
self.disconnected = False | ||
self.ending_stream = False | ||
self.cleanup_needed = False | ||
self.state = ClientState.NOT_INITIALIZED | ||
|
||
def initialize_client(self): | ||
whisper_model_size, language_code = get_transcriber_information(self.config) | ||
try: | ||
beam_size = int(self.config.get("beamSize", 1)) | ||
except TypeError: | ||
logging.warning(f"Invalid beam size {self.config.get('beamSize')}, defaulting to 1") | ||
beam_size = 1 | ||
self.transcriber = WhisperTranscriber(model_size=whisper_model_size, language_code=language_code, | ||
beam_size=beam_size) | ||
self.transcription_timeout = int(self.config.get("transcribeTimeout", 5)) | ||
self.state = ClientState.INITIALIZED | ||
|
||
@abstractmethod | ||
async def start_transcribing(self): | ||
pass | ||
if self.transcriber is None: | ||
raise ValueError("The transcriber must be defined before using this method") | ||
|
||
async def stop_transcribing(self): | ||
self.ending_stream = True | ||
self.state = ClientState.ENDING_STREAM | ||
self.transcription_thread.join() | ||
logging.info("Transcription thread closed due to completion (stream ended)") | ||
await self.socket.emit("whisperingStopped") | ||
logging.info("Stream end signaled to client") | ||
|
||
def handle_disconnection(self): | ||
logging.info("Starting disconnection process, no longer sending transcriptions to client") | ||
self.disconnected = True | ||
if not self.ending_stream: | ||
if self.state not in [ClientState.ENDING_STREAM, ClientState.NOT_INITIALIZED]: | ||
self.state = ClientState.DISCONNECTED | ||
self.transcription_thread.join() | ||
logging.info("Transcription thread closed due to disconnection") | ||
|
||
async def send_transcription(self, transcription): | ||
logging.info(f"Transcription generated: {transcription}") | ||
if not self.disconnected: | ||
if self.state != ClientState.DISCONNECTED: | ||
await self.socket.emit("transcriptionDataAvailable", transcription) | ||
logging.info("Transcription sent") | ||
else: | ||
logging.info("Transcription not sent, client disconnected") | ||
|
||
def handle_chunk(self, chunk): | ||
self.audio_chunks.put(chunk) | ||
speech_present, speech_confidence = silero_vad(decode_audio(chunk)) | ||
if speech_present: | ||
self.audio_chunks.put(chunk) | ||
logging.debug("Chunk added") | ||
|
||
def is_ending_stream(self): | ||
return self.ending_stream | ||
def get_state(self): | ||
return self.state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.