Merge pull request #391 from makaveli10/integrate_live_translation

Integrate live translation
This commit is contained in:
Vineet Suryan
2025-07-24 12:07:03 +05:30
committed by GitHub
11 changed files with 746 additions and 30 deletions
+5 -1
View File
@@ -111,6 +111,8 @@ If you don't want this, set `--no_single_model`.
- `save_output_recording`: Set to True to save the microphone input as a `.wav` file during live transcription. This option is helpful for recording sessions for later playback or analysis. Defaults to `False`. - `save_output_recording`: Set to True to save the microphone input as a `.wav` file during live transcription. This option is helpful for recording sessions for later playback or analysis. Defaults to `False`.
- `output_recording_filename`: Specifies the `.wav` file path where the microphone input will be saved if `save_output_recording` is set to `True`. - `output_recording_filename`: Specifies the `.wav` file path where the microphone input will be saved if `save_output_recording` is set to `True`.
- `mute_audio_playback`: Whether to mute audio playback when transcribing an audio file. Defaults to False. - `mute_audio_playback`: Whether to mute audio playback when transcribing an audio file. Defaults to False.
- `enable_translation`: Start translation thread on the server (from any to any).
- `target_language`: Server translation thread's target translation language.
```python ```python
from whisper_live.client import TranscriptionClient from whisper_live.client import TranscriptionClient
@@ -124,6 +126,8 @@ client = TranscriptionClient(
save_output_recording=True, # Only used for microphone input, False by Default save_output_recording=True, # Only used for microphone input, False by Default
output_recording_filename="./output_recording.wav", # Only used for microphone input output_recording_filename="./output_recording.wav", # Only used for microphone input
mute_audio_playback=False, # Only used for file input, False by Default mute_audio_playback=False, # Only used for file input, False by Default
enable_translation=True,
target_language="hi",
) )
``` ```
It connects to the server running on localhost at port 9090. Using a multilingual model, language for the transcription will be automatically detected. You can also use the language option to specify the target language for the transcription, in this case, English ("en"). The translate option should be set to `True` if we want to translate from the source language to English and `False` if we want to transcribe in the source language. It connects to the server running on localhost at port 9090. Using a multilingual model, language for the transcription will be automatically detected. You can also use the language option to specify the target language for the transcription, in this case, English ("en"). The translate option should be set to `True` if we want to translate from the source language to English and `False` if we want to transcribe in the source language.
@@ -195,7 +199,7 @@ Refer to [`ios-client`](https://github.com/collabora/WhisperLive/tree/main/Audio
``` ```
## Future Work ## Future Work
- [ ] Add translation to other languages on top of transcription. - [x] Add translation to other languages on top of transcription.
## Blog Posts ## Blog Posts
- [Transforming speech technology with WhisperLive](https://www.collabora.com/news-and-blog/blog/2024/05/28/transforming-speech-technology-with-whisperlive/) - [Transforming speech technology with WhisperLive](https://www.collabora.com/news-and-blog/blog/2024/05/28/transforming-speech-technology-with-whisperlive/)
+1
View File
@@ -12,6 +12,7 @@ numpy<2
openai-whisper==20240930 openai-whisper==20240930
tokenizers==0.20.3 tokenizers==0.20.3
transformers[torch] transformers[torch]
sentencepiece
# openvino # openvino
librosa librosa
+11 -1
View File
@@ -39,7 +39,15 @@ if __name__ == '__main__':
help='Mute audio playback during transcription.') help='Mute audio playback during transcription.')
parser.add_argument('--save_output_recording', '-r', parser.add_argument('--save_output_recording', '-r',
action='store_true', action='store_true',
help='Save the output recording, only used for microphone input.') help='Save the output recording, only used for microphone input.')
parser.add_argument('--enable_translation',
action='store_true',
help='Enable translation of the transcription output.')
parser.add_argument('--target_language', '-tl',
type=str,
default='fr',
help='Target language for translation, e.g., "fr" for French.')
args = parser.parse_args() args = parser.parse_args()
# Validate audio files # Validate audio files
@@ -70,5 +78,7 @@ if __name__ == '__main__':
save_output_recording=args.save_output_recording, # Only used for microphone input, False by Default save_output_recording=args.save_output_recording, # Only used for microphone input, False by Default
output_recording_filename=args.output_file, # Only used for microphone input output_recording_filename=args.output_file, # Only used for microphone input
mute_audio_playback=args.mute_audio_playback, # Only used for file input, False by Default mute_audio_playback=args.mute_audio_playback, # Only used for file input, False by Default
enable_translation=args.enable_translation, # Enable translation of the transcription output
target_language=args.target_language, # Target language for translation, e.g., "fr
) )
client(f) client(f)
+2
View File
@@ -53,6 +53,8 @@ class TestClientCallbacks(BaseTestCase):
"no_speech_thresh": 0.45, "no_speech_thresh": 0.45,
"clip_audio": False, "clip_audio": False,
"same_output_threshold": 10, "same_output_threshold": 10,
"enable_translation": False,
"target_language": "fr",
}) })
self.client.on_open(self.mock_ws_app) self.client.on_open(self.mock_ws_app)
self.mock_ws_app.send.assert_called_with(expected_message) self.mock_ws_app.send.assert_called_with(expected_message)
+21 -3
View File
@@ -2,6 +2,7 @@ import json
import logging import logging
import threading import threading
import time import time
import queue
import numpy as np import numpy as np
@@ -31,6 +32,7 @@ class ServeClientBase(object):
no_speech_thresh=0.45, no_speech_thresh=0.45,
clip_audio=False, clip_audio=False,
same_output_threshold=10, same_output_threshold=10,
translation_queue=None,
): ):
self.client_uid = client_uid self.client_uid = client_uid
self.websocket = websocket self.websocket = websocket
@@ -50,6 +52,7 @@ class ServeClientBase(object):
self.same_output_count = 0 self.same_output_count = 0
self.transcript = [] self.transcript = []
self.end_time_for_same_output = None self.end_time_for_same_output = None
self.translation_queue = translation_queue
# threading # threading
self.lock = threading.Lock() self.lock = threading.Lock()
@@ -307,7 +310,14 @@ class ServeClientBase(object):
continue continue
if self.get_segment_no_speech_prob(s) > self.no_speech_thresh: if self.get_segment_no_speech_prob(s) > self.no_speech_thresh:
continue continue
self.transcript.append(self.format_segment(start, end, text_, completed=True)) completed_segment = self.format_segment(start, end, text_, completed=True)
self.transcript.append(completed_segment)
if self.translation_queue:
try:
self.translation_queue.put(completed_segment.copy(), timeout=0.1)
except queue.Full:
logging.warning("Translation queue is full, skipping segment")
offset = min(duration, self.get_segment_end(s)) offset = min(duration, self.get_segment_end(s))
# Process the last segment if its no_speech_prob is acceptable. # Process the last segment if its no_speech_prob is acceptable.
@@ -340,12 +350,20 @@ class ServeClientBase(object):
if not self.text or self.text[-1].strip().lower() != self.current_out.strip().lower(): if not 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)
with self.lock: with self.lock:
self.transcript.append(self.format_segment( completed_segment = self.format_segment(
self.timestamp_offset, self.timestamp_offset,
self.timestamp_offset + min(duration, self.end_time_for_same_output), self.timestamp_offset + min(duration, self.end_time_for_same_output),
self.current_out, self.current_out,
completed=True completed=True
)) )
self.transcript.append(completed_segment)
if self.translation_queue:
try:
self.translation_queue.put(completed_segment.copy(), timeout=0.1)
except queue.Full:
logging.warning("Translation queue is full, skipping segment")
self.current_out = '' self.current_out = ''
offset = min(duration, self.end_time_for_same_output) offset = min(duration, self.end_time_for_same_output)
self.same_output_count = 0 self.same_output_count = 0
@@ -30,8 +30,9 @@ class ServeClientFasterWhisper(ServeClientBase):
send_last_n_segments=10, send_last_n_segments=10,
no_speech_thresh=0.45, no_speech_thresh=0.45,
clip_audio=False, clip_audio=False,
same_output_threshold=10, same_output_threshold=7,
cache_path="~/.cache/whisper-live/" cache_path="~/.cache/whisper-live/",
translation_queue=None,
): ):
""" """
Initialize a ServeClient instance. Initialize a ServeClient instance.
@@ -61,6 +62,7 @@ class ServeClientFasterWhisper(ServeClientBase):
no_speech_thresh, no_speech_thresh,
clip_audio, clip_audio,
same_output_threshold, same_output_threshold,
translation_queue
) )
self.cache_path = cache_path self.cache_path = cache_path
self.model_sizes = [ self.model_sizes = [
@@ -0,0 +1,365 @@
# Copyright (c) 2022 Idiap Research Institute, http://www.idiap.ch/
# Written by Alireza Mohammadshahi <alireza.mohammadshahi@idiap.ch>
# This is a modified version of https://github.com/huggingface/transformers/blob/main/src/transformers/models/m2m_100/tokenization_m2m_100.py
# which owns by Fariseq Authors and The HuggingFace Inc. team.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tokenization classes for SMALL100."""
import json
import os
from pathlib import Path
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple, Union
import sentencepiece
from transformers.tokenization_utils import BatchEncoding, PreTrainedTokenizer
from transformers.utils import logging
logger = logging.get_logger(__name__)
SPIECE_UNDERLINE = ""
VOCAB_FILES_NAMES = {
"vocab_file": "vocab.json",
"spm_file": "sentencepiece.bpe.model",
"tokenizer_config_file": "tokenizer_config.json",
}
PRETRAINED_VOCAB_FILES_MAP = {
"vocab_file": {
"alirezamsh/small100": "https://huggingface.co/alirezamsh/small100/resolve/main/vocab.json",
},
"spm_file": {
"alirezamsh/small100": "https://huggingface.co/alirezamsh/small100/resolve/main/sentencepiece.bpe.model",
},
"tokenizer_config_file": {
"alirezamsh/small100": "https://huggingface.co/alirezamsh/small100/resolve/main/tokenizer_config.json",
},
}
PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES = {
"alirezamsh/small100": 1024,
}
# fmt: off
FAIRSEQ_LANGUAGE_CODES = {
"m2m100": ["af", "am", "ar", "ast", "az", "ba", "be", "bg", "bn", "br", "bs", "ca", "ceb", "cs", "cy", "da", "de", "el", "en", "es", "et", "fa", "ff", "fi", "fr", "fy", "ga", "gd", "gl", "gu", "ha", "he", "hi", "hr", "ht", "hu", "hy", "id", "ig", "ilo", "is", "it", "ja", "jv", "ka", "kk", "km", "kn", "ko", "lb", "lg", "ln", "lo", "lt", "lv", "mg", "mk", "ml", "mn", "mr", "ms", "my", "ne", "nl", "no", "ns", "oc", "or", "pa", "pl", "ps", "pt", "ro", "ru", "sd", "si", "sk", "sl", "so", "sq", "sr", "ss", "su", "sv", "sw", "ta", "th", "tl", "tn", "tr", "uk", "ur", "uz", "vi", "wo", "xh", "yi", "yo", "zh", "zu"]
}
# fmt: on
class SMALL100Tokenizer(PreTrainedTokenizer):
"""
Construct an SMALL100 tokenizer. Based on [SentencePiece](https://github.com/google/sentencepiece).
This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. Users should refer to
this superclass for more information regarding those methods.
Args:
vocab_file (`str`):
Path to the vocabulary file.
spm_file (`str`):
Path to [SentencePiece](https://github.com/google/sentencepiece) file (generally has a .spm extension) that
contains the vocabulary.
tgt_lang (`str`, *optional*):
A string representing the target language.
eos_token (`str`, *optional*, defaults to `"</s>"`):
The end of sequence token.
sep_token (`str`, *optional*, defaults to `"</s>"`):
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for
sequence classification or for a text and a question for question answering. It is also used as the last
token of a sequence built with special tokens.
unk_token (`str`, *optional*, defaults to `"<unk>"`):
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
token instead.
pad_token (`str`, *optional*, defaults to `"<pad>"`):
The token used for padding, for example when batching sequences of different lengths.
language_codes (`str`, *optional*):
What language codes to use. Should be `"m2m100"`.
sp_model_kwargs (`dict`, *optional*):
Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for
SentencePiece](https://github.com/google/sentencepiece/tree/master/python) can be used, among other things,
to set:
- `enable_sampling`: Enable subword regularization.
- `nbest_size`: Sampling parameters for unigram. Invalid for BPE-Dropout.
- `nbest_size = {0,1}`: No sampling is performed.
- `nbest_size > 1`: samples from the nbest_size results.
- `nbest_size < 0`: assuming that nbest_size is infinite and samples from the all hypothesis (lattice)
using forward-filtering-and-backward-sampling algorithm.
- `alpha`: Smoothing parameter for unigram sampling, and dropout probability of merge operations for
BPE-dropout.
Examples:
```python
>>> from tokenization_small100 import SMALL100Tokenizer
>>> tokenizer = SMALL100Tokenizer.from_pretrained("alirezamsh/small100", tgt_lang="ro")
>>> src_text = " UN Chief Says There Is No Military Solution in Syria"
>>> tgt_text = "Şeful ONU declară că nu există o soluţie militară în Siria"
>>> model_inputs = tokenizer(src_text, text_target=tgt_text, return_tensors="pt")
>>> model(**model_inputs) # should work
```"""
vocab_files_names = VOCAB_FILES_NAMES
max_model_input_sizes = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
model_input_names = ["input_ids", "attention_mask"]
prefix_tokens: List[int] = []
suffix_tokens: List[int] = []
def __init__(
self,
vocab_file,
spm_file,
tgt_lang=None,
bos_token="<s>",
eos_token="</s>",
sep_token="</s>",
pad_token="<pad>",
unk_token="<unk>",
language_codes="m2m100",
sp_model_kwargs: Optional[Dict[str, Any]] = None,
num_madeup_words=8,
**kwargs,
) -> None:
self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs
self.language_codes = language_codes
fairseq_language_code = FAIRSEQ_LANGUAGE_CODES[language_codes]
self.lang_code_to_token = {lang_code: f"__{lang_code}__" for lang_code in fairseq_language_code}
kwargs["additional_special_tokens"] = kwargs.get("additional_special_tokens", [])
kwargs["additional_special_tokens"] += [
self.get_lang_token(lang_code)
for lang_code in fairseq_language_code
if self.get_lang_token(lang_code) not in kwargs["additional_special_tokens"]
]
self.vocab_file = vocab_file
self.encoder = load_json(vocab_file)
self.decoder = {v: k for k, v in self.encoder.items()}
self.spm_file = spm_file
self.sp_model = load_spm(spm_file, self.sp_model_kwargs)
self.encoder_size = len(self.encoder)
self.lang_token_to_id = {
self.get_lang_token(lang_code): self.encoder_size + i for i, lang_code in enumerate(fairseq_language_code)
}
self.lang_code_to_id = {lang_code: self.encoder_size + i for i, lang_code in enumerate(fairseq_language_code)}
self.id_to_lang_token = {v: k for k, v in self.lang_token_to_id.items()}
self._tgt_lang = tgt_lang if tgt_lang is not None else "en"
self.cur_lang_id = self.get_lang_id(self._tgt_lang)
self.num_madeup_words = num_madeup_words
super().__init__(
tgt_lang=tgt_lang,
bos_token=bos_token,
eos_token=eos_token,
sep_token=sep_token,
unk_token=unk_token,
pad_token=pad_token,
language_codes=language_codes,
sp_model_kwargs=self.sp_model_kwargs,
num_madeup_words=num_madeup_words,
**kwargs,
)
self.set_lang_special_tokens(self._tgt_lang)
@property
def vocab_size(self) -> int:
return len(self.encoder) + len(self.lang_token_to_id) + self.num_madeup_words
@property
def tgt_lang(self) -> str:
return self._tgt_lang
@tgt_lang.setter
def tgt_lang(self, new_tgt_lang: str) -> None:
self._tgt_lang = new_tgt_lang
self.set_lang_special_tokens(self._tgt_lang)
def _tokenize(self, text: str) -> List[str]:
return self.sp_model.encode(text, out_type=str)
def _convert_token_to_id(self, token):
if token in self.lang_token_to_id:
return self.lang_token_to_id[token]
return self.encoder.get(token, self.encoder[self.unk_token])
def _convert_id_to_token(self, index: int) -> str:
"""Converts an index (integer) in a token (str) using the decoder."""
if index in self.id_to_lang_token:
return self.id_to_lang_token[index]
return self.decoder.get(index, self.unk_token)
def convert_tokens_to_string(self, tokens: List[str]) -> str:
"""Converts a sequence of tokens (strings for sub-words) in a single string."""
return self.sp_model.decode(tokens)
def get_special_tokens_mask(
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
) -> List[int]:
"""
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
special tokens using the tokenizer `prepare_for_model` method.
Args:
token_ids_0 (`List[int]`):
List of IDs.
token_ids_1 (`List[int]`, *optional*):
Optional second list of IDs for sequence pairs.
already_has_special_tokens (`bool`, *optional*, defaults to `False`):
Whether or not the token list is already formatted with special tokens for the model.
Returns:
`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
"""
if already_has_special_tokens:
return super().get_special_tokens_mask(
token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True
)
prefix_ones = [1] * len(self.prefix_tokens)
suffix_ones = [1] * len(self.suffix_tokens)
if token_ids_1 is None:
return prefix_ones + ([0] * len(token_ids_0)) + suffix_ones
return prefix_ones + ([0] * len(token_ids_0)) + ([0] * len(token_ids_1)) + suffix_ones
def build_inputs_with_special_tokens(
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
) -> List[int]:
"""
Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
adding special tokens. An MBART sequence has the following format, where `X` represents the sequence:
- `input_ids` (for encoder) `X [eos, src_lang_code]`
- `decoder_input_ids`: (for decoder) `X [eos, tgt_lang_code]`
BOS is never used. Pairs of sequences are not the expected use case, but they will be handled without a
separator.
Args:
token_ids_0 (`List[int]`):
List of IDs to which the special tokens will be added.
token_ids_1 (`List[int]`, *optional*):
Optional second list of IDs for sequence pairs.
Returns:
`List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.
"""
if token_ids_1 is None:
if self.prefix_tokens is None:
return token_ids_0 + self.suffix_tokens
else:
return self.prefix_tokens + token_ids_0 + self.suffix_tokens
# We don't expect to process pairs, but leave the pair logic for API consistency
if self.prefix_tokens is None:
return token_ids_0 + token_ids_1 + self.suffix_tokens
else:
return self.prefix_tokens + token_ids_0 + token_ids_1 + self.suffix_tokens
def get_vocab(self) -> Dict:
vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
vocab.update(self.added_tokens_encoder)
return vocab
def __getstate__(self) -> Dict:
state = self.__dict__.copy()
state["sp_model"] = None
return state
def __setstate__(self, d: Dict) -> None:
self.__dict__ = d
# for backward compatibility
if not hasattr(self, "sp_model_kwargs"):
self.sp_model_kwargs = {}
self.sp_model = load_spm(self.spm_file, self.sp_model_kwargs)
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
save_dir = Path(save_directory)
if not save_dir.is_dir():
raise OSError(f"{save_directory} should be a directory")
vocab_save_path = save_dir / (
(filename_prefix + "-" if filename_prefix else "") + self.vocab_files_names["vocab_file"]
)
spm_save_path = save_dir / (
(filename_prefix + "-" if filename_prefix else "") + self.vocab_files_names["spm_file"]
)
save_json(self.encoder, vocab_save_path)
if os.path.abspath(self.spm_file) != os.path.abspath(spm_save_path) and os.path.isfile(self.spm_file):
copyfile(self.spm_file, spm_save_path)
elif not os.path.isfile(self.spm_file):
with open(spm_save_path, "wb") as fi:
content_spiece_model = self.sp_model.serialized_model_proto()
fi.write(content_spiece_model)
return (str(vocab_save_path), str(spm_save_path))
def prepare_seq2seq_batch(
self,
src_texts: List[str],
tgt_texts: Optional[List[str]] = None,
tgt_lang: str = "ro",
**kwargs,
) -> BatchEncoding:
self.tgt_lang = tgt_lang
self.set_lang_special_tokens(self.tgt_lang)
return super().prepare_seq2seq_batch(src_texts, tgt_texts, **kwargs)
def _build_translation_inputs(self, raw_inputs, tgt_lang: Optional[str], **extra_kwargs):
"""Used by translation pipeline, to prepare inputs for the generate function"""
if tgt_lang is None:
raise ValueError("Translation requires a `tgt_lang` for this model")
self.tgt_lang = tgt_lang
inputs = self(raw_inputs, add_special_tokens=True, **extra_kwargs)
return inputs
def _switch_to_input_mode(self):
self.set_lang_special_tokens(self.tgt_lang)
def _switch_to_target_mode(self):
self.prefix_tokens = None
self.suffix_tokens = [self.eos_token_id]
def set_lang_special_tokens(self, src_lang: str) -> None:
"""Reset the special tokens to the tgt lang setting. No prefix and suffix=[eos, tgt_lang_code]."""
lang_token = self.get_lang_token(src_lang)
self.cur_lang_id = self.lang_token_to_id[lang_token]
self.prefix_tokens = [self.cur_lang_id]
self.suffix_tokens = [self.eos_token_id]
def get_lang_token(self, lang: str) -> str:
return self.lang_code_to_token[lang]
def get_lang_id(self, lang: str) -> int:
lang_token = self.get_lang_token(lang)
return self.lang_token_to_id[lang_token]
def load_spm(path: str, sp_model_kwargs: Dict[str, Any]) -> sentencepiece.SentencePieceProcessor:
spm = sentencepiece.SentencePieceProcessor(**sp_model_kwargs)
spm.Load(str(path))
return spm
def load_json(path: str) -> Union[Dict, List]:
with open(path, "r") as f:
return json.load(f)
def save_json(data, path: str) -> None:
with open(path, "w") as f:
json.dump(data, f, indent=2)
+218
View File
@@ -0,0 +1,218 @@
import json
import logging
import threading
import time
import queue
from typing import Dict, Any, Optional
import torch
import threading
from transformers import M2M100ForConditionalGeneration
from whisper_live.backend.tokenization_small100 import SMALL100Tokenizer
from whisper_live.backend.base import ServeClientBase
class ServeClientTranslation(ServeClientBase):
"""
Handles translation of completed transcription segments in a separate thread.
Reads from a queue populated by the transcription backend and sends translated
segments back to the client via WebSocket.
"""
def __init__(
self,
client_uid,
websocket,
translation_queue,
target_language="fr",
send_last_n_segments=10,
model_name="alirezamsh/small100"
):
"""
Initialize the translation client.
Args:
client_uid (str): Unique identifier for the client
websocket: WebSocket connection to the client
translation_queue (queue.Queue): Queue containing completed segments to translate
target_language (str): Target language code (default: "fr" for French)
send_last_n_segments (int): Number of recent translated segments to send
model_name (str): Translation model name to use
"""
super().__init__(client_uid, websocket, send_last_n_segments)
self.translation_queue = translation_queue
self.target_language = target_language
self.model_name = model_name
self.translated_segments = []
self.translation_model = None
self.tokenizer = None
self.device = None
self.model_loaded = False
self.load_translation_model()
def load_translation_model(self):
"""Load the translation model and tokenizer."""
try:
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
logging.info(f"Loading translation model on device: {self.device}")
self.translation_model = M2M100ForConditionalGeneration.from_pretrained(
self.model_name
).to(self.device)
self.tokenizer = SMALL100Tokenizer.from_pretrained(self.model_name)
self.tokenizer.tgt_lang = self.target_language
self.model_loaded = True
logging.info(f"Translation model loaded successfully. Target language: {self.target_language}")
except Exception as e:
logging.error(f"Failed to load translation model: {e}")
self.translation_model = None
self.tokenizer = None
self.model_loaded = False
def translate_text(self, text: str) -> str:
"""
Translate a single text segment.
Args:
text (str): Text to translate
Returns:
str: Translated text or original text if translation fails
"""
if not self.model_loaded or not text.strip():
return text
try:
# Encode input and move to device
encoded_input = self.tokenizer(text, return_tensors="pt").to(self.device)
# Generate translation
with torch.no_grad():
generated_tokens = self.translation_model.generate(**encoded_input)
# Decode output
output = self.tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
return output[0] if output else text
except Exception as e:
logging.error(f"Translation failed for text '{text}': {e}")
return text
def process_translation_queue(self):
"""
Process segments from the translation queue.
Continuously reads from the queue until None is received (exit signal).
"""
logging.info(f"Starting translation processing for client {self.client_uid}")
while not self.exit:
try:
# Get segment from queue with timeout
segment = self.translation_queue.get(timeout=1.0)
# Check for exit signal
if segment is None:
logging.info(f"Received exit signal for translation client {self.client_uid}")
break
# Only translate completed segments
if not segment.get("completed", False):
self.translation_queue.task_done()
continue
# Translate the segment
original_text = segment.get("text", "")
translated_text = self.translate_text(original_text)
# Create translated segment
translated_segment = {
"start": segment["start"],
"end": segment["end"],
"text": translated_text,
"completed": segment.get("completed", False),
"target_language": self.target_language
}
self.translated_segments.append(translated_segment)
segments_to_send = self.prepare_translated_segments()
self.send_translation_to_client(segments_to_send)
self.translation_queue.task_done()
except queue.Empty:
continue
except Exception as e:
logging.error(f"Error processing translation queue: {e}")
continue
logging.info(f"Translation processing ended for client {self.client_uid}")
def prepare_translated_segments(self):
"""
Prepare the last n translated segments to send to client.
Returns:
list: List of recent translated segments
"""
if len(self.translated_segments) >= self.send_last_n_segments:
return self.translated_segments[-self.send_last_n_segments:]
return self.translated_segments[:]
def send_translation_to_client(self, translated_segments):
"""
Send translated segments to the client via WebSocket.
Args:
translated_segments (list): List of translated segments to send
"""
try:
self.websocket.send(
json.dumps({
"uid": self.client_uid,
"translated_segments": translated_segments,
})
)
except Exception as e:
logging.error(f"[ERROR]: Sending translation data to client: {e}")
def speech_to_text(self):
"""
Override parent method to handle translation processing.
This method will be called when the translation thread starts.
"""
self.process_translation_queue()
def set_target_language(self, language: str):
"""
Change the target language for translation.
Args:
language (str): New target language code
"""
self.target_language = language
if self.tokenizer:
self.tokenizer.tgt_lang = language
logging.info(f"Target language changed to: {language}")
def cleanup(self):
"""Clean up translation resources."""
logging.info(f"Cleaning up translation resources for client {self.client_uid}")
self.exit = True
try:
self.translation_queue.put(None, timeout=1.0)
except:
pass
self.translated_segments.clear()
if self.translation_model:
del self.translation_model
self.translation_model = None
if self.tokenizer:
del self.tokenizer
self.tokenizer = None
if self.device and self.device.type == 'cuda':
torch.cuda.empty_cache()
+73 -19
View File
@@ -37,6 +37,10 @@ class Client:
clip_audio=False, clip_audio=False,
same_output_threshold=10, same_output_threshold=10,
transcription_callback=None, transcription_callback=None,
enable_translation=False,
target_language="fr",
translation_callback=None,
translation_srt_file_path="output_translated.srt",
): ):
""" """
Initializes a Client instance for audio recording and streaming to a server. Initializes a Client instance for audio recording and streaming to a server.
@@ -59,6 +63,10 @@ class Client:
clip_audio (bool, optional): Whether to clip audio with no valid segments. Defaults to False. clip_audio (bool, optional): Whether to clip audio with no valid segments. Defaults to False.
same_output_threshold (int, optional): Number of repeated outputs before considering it as a valid segment. Defaults to 10. same_output_threshold (int, optional): Number of repeated outputs before considering it as a valid segment. Defaults to 10.
transcription_callback (callable, optional): A callback function to handle transcription results. Default is None. transcription_callback (callable, optional): A callback function to handle transcription results. Default is None.
enable_translation (float, optional): Whether to enable translation from any to any language. Defaults to False.
target_language (str, optional): Target language for translation. Defaults to 'fr'.
translation_callback (callable, optional): A callback function to handle translation results. Default is None.
translation_srt_file_path (str, optional): The file path to save the translated output SRT file. Default is "output_translated.srt".
""" """
self.recording = False self.recording = False
self.task = "transcribe" self.task = "transcribe"
@@ -81,6 +89,12 @@ class Client:
self.same_output_threshold = same_output_threshold self.same_output_threshold = same_output_threshold
self.transcription_callback = transcription_callback self.transcription_callback = transcription_callback
# Translation-specific attributes
self.enable_translation = enable_translation
self.target_language = target_language
self.translation_callback = translation_callback
self.translation_srt_file_path = translation_srt_file_path
self.last_translated_segment = None
if translate: if translate:
self.task = "translate" self.task = "translate"
@@ -110,6 +124,7 @@ class Client:
self.ws_thread.start() self.ws_thread.start()
self.transcript = [] self.transcript = []
self.translated_transcript = []
print("[INFO]: * recording") print("[INFO]: * recording")
def handle_status_messages(self, message_data): def handle_status_messages(self, message_data):
@@ -124,36 +139,53 @@ class Client:
elif status == "WARNING": elif status == "WARNING":
print(f"Message from Server: {message_data['message']}") print(f"Message from Server: {message_data['message']}")
def process_segments(self, segments): def process_segments(self, segments, translated=False):
"""Processes transcript segments.""" """Processes transcript segments."""
text = [] text = []
for i, seg in enumerate(segments): for i, seg in enumerate(segments):
if not text or text[-1] != seg["text"]: if not text or text[-1] != seg["text"]:
text.append(seg["text"]) text.append(seg["text"].strip())
if i == len(segments) - 1 and not seg.get("completed", False): if i == len(segments) - 1 and not seg.get("completed", False):
self.last_segment = seg self.last_segment = seg
elif (self.server_backend == "faster_whisper" and seg.get("completed", False) and elif self.server_backend == "faster_whisper" and seg.get("completed", False):
(not self.transcript or if translated:
float(seg['start']) >= float(self.transcript[-1]['end']))): if (not self.translated_transcript or float(seg['start']) >= float(self.translated_transcript[-1]['end'])):
self.transcript.append(seg) self.translated_transcript.append(seg)
else:
if (not self.transcript or float(seg['start']) >= float(self.transcript[-1]['end'])):
self.transcript.append(seg)
# update last received segment and last valid response time # update last received segment and last valid response time
if self.last_received_segment is None or self.last_received_segment != segments[-1]["text"]: if not translated:
self.last_response_received = time.time() if self.last_received_segment is None or self.last_received_segment != segments[-1]["text"]:
self.last_received_segment = segments[-1]["text"] self.last_response_received = time.time()
self.last_received_segment = segments[-1]["text"]
# call the transcription callback if provided # call the transcription callback if provided
if self.transcription_callback and callable(self.transcription_callback): if translated:
try: if self.translation_callback and callable(self.translation_callback):
self.transcription_callback(" ".join(text), segments) # string, list try:
except Exception as e: self.translation_callback(" ".join(text), segments) # string, list
print(f"[WARN] transcription_callback raised: {e}") except Exception as e:
return print(f"[WARN] translation_callback raised: {e}")
return
else:
if self.transcription_callback and callable(self.transcription_callback):
try:
self.transcription_callback(" ".join(text), segments) # string, list
except Exception as e:
print(f"[WARN] transcription_callback raised: {e}")
return
if self.log_transcription: if self.log_transcription:
# Truncate to last 3 entries for brevity. original_text = [seg["text"] for seg in self.transcript[-4:]]
text = text[-3:] if self.last_segment is not None and self.last_segment["text"] not in original_text:
original_text.append(self.last_segment["text"])
utils.clear_screen() utils.clear_screen()
utils.print_transcript(text) utils.print_transcript(original_text)
if self.enable_translation:
print(f"\n\nTRANSLATION to {self.target_language}:")
utils.print_transcript([seg["text"] for seg in self.translated_transcript[-4:]], translated=True)
def on_message(self, ws, message): def on_message(self, ws, message):
""" """
@@ -199,6 +231,9 @@ class Client:
if "segments" in message.keys(): if "segments" in message.keys():
self.process_segments(message["segments"]) self.process_segments(message["segments"])
if "translated_segments" in message.keys():
self.process_segments(message["translated_segments"], translated=True)
def on_error(self, ws, error): def on_error(self, ws, error):
print(f"[ERROR] WebSocket Error: {error}") print(f"[ERROR] WebSocket Error: {error}")
@@ -234,6 +269,8 @@ class Client:
"no_speech_thresh": self.no_speech_thresh, "no_speech_thresh": self.no_speech_thresh,
"clip_audio": self.clip_audio, "clip_audio": self.clip_audio,
"same_output_threshold": self.same_output_threshold, "same_output_threshold": self.same_output_threshold,
"enable_translation": self.enable_translation,
"target_language": self.target_language,
} }
) )
) )
@@ -293,6 +330,9 @@ class Client:
self.transcript.append(self.last_segment) self.transcript.append(self.last_segment)
utils.create_srt_file(self.transcript, output_path) utils.create_srt_file(self.transcript, output_path)
if self.enable_translation:
utils.create_srt_file(self.translated_transcript, self.translation_srt_file_path)
def wait_before_disconnect(self): def wait_before_disconnect(self):
"""Waits a bit before disconnecting in order to process pending responses.""" """Waits a bit before disconnecting in order to process pending responses."""
assert self.last_response_received assert self.last_response_received
@@ -692,7 +732,7 @@ class TranscriptionClient(TranscriptionTeeClient):
""" """
Client for handling audio transcription tasks via a single WebSocket connection. Client for handling audio transcription tasks via a single WebSocket connection.
Acts as a high-level client for audio transcription tasks using a WebSocket connection. It can be used Acts as a high-level client for audio transcription tasksoutput_transcription_path using a WebSocket connection. It can be used
to send audio data for transcription to a server and receive transcribed text segments. to send audio data for transcription to a server and receive transcribed text segments.
Args: Args:
@@ -712,6 +752,10 @@ class TranscriptionClient(TranscriptionTeeClient):
clip_audio (bool, optional): Whether to clip audio with no valid segments. Defaults to False. clip_audio (bool, optional): Whether to clip audio with no valid segments. Defaults to False.
same_output_threshold (int, optional): Number of repeated outputs before considering it as a valid segment. Defaults to 10. same_output_threshold (int, optional): Number of repeated outputs before considering it as a valid segment. Defaults to 10.
transcription_callback (callable, optional): A callback function to handle transcription results. Default is None. transcription_callback (callable, optional): A callback function to handle transcription results. Default is None.
enable_translation (float, optional): Whether to enable translation from any to any language. Defaults to False.
target_language (str, optional): Target language for translation. Defaults to 'fr'.
translation_callback (callable, optional): A callback function to handle translation results. Default is None.
translation_srt_file_path (str, optional): The file path to save the translated output SRT file. Default is "output_translated.srt".
Attributes: Attributes:
client (Client): An instance of the underlying Client class responsible for handling the WebSocket connection. client (Client): An instance of the underlying Client class responsible for handling the WebSocket connection.
@@ -742,6 +786,10 @@ class TranscriptionClient(TranscriptionTeeClient):
clip_audio=False, clip_audio=False,
same_output_threshold=10, same_output_threshold=10,
transcription_callback=None, transcription_callback=None,
enable_translation=False,
target_language="fr",
translation_callback=None,
translation_srt_file_path="./output_translated.srt",
): ):
self.client = Client( self.client = Client(
host, host,
@@ -758,12 +806,18 @@ class TranscriptionClient(TranscriptionTeeClient):
clip_audio=clip_audio, clip_audio=clip_audio,
same_output_threshold=same_output_threshold, same_output_threshold=same_output_threshold,
transcription_callback=transcription_callback, transcription_callback=transcription_callback,
enable_translation=enable_translation,
target_language=target_language,
translation_callback=translation_callback,
translation_srt_file_path=translation_srt_file_path,
) )
if save_output_recording and not output_recording_filename.endswith(".wav"): if save_output_recording and not output_recording_filename.endswith(".wav"):
raise ValueError(f"Please provide a valid `output_recording_filename`: {output_recording_filename}") raise ValueError(f"Please provide a valid `output_recording_filename`: {output_recording_filename}")
if not output_transcription_path.endswith(".srt"): if not output_transcription_path.endswith(".srt"):
raise ValueError(f"Please provide a valid `output_transcription_path`: {output_transcription_path}. The file extension should be `.srt`.") raise ValueError(f"Please provide a valid `output_transcription_path`: {output_transcription_path}. The file extension should be `.srt`.")
if not translation_srt_file_path.endswith(".srt"):
raise ValueError(f"Please provide a valid `translation_srt_file_path`: {translation_srt_file_path}. The file extension should be `.srt`.")
TranscriptionTeeClient.__init__( TranscriptionTeeClient.__init__(
self, self,
[self.client], [self.client],
+43 -2
View File
@@ -1,6 +1,7 @@
import os import os
import time import time
import threading import threading
import queue
import json import json
import functools import functools
import logging import logging
@@ -15,7 +16,6 @@ from whisper_live.backend.base import ServeClientBase
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
class ClientManager: class ClientManager:
def __init__(self, max_clients=4, max_connection_time=600): def __init__(self, max_clients=4, max_connection_time=600):
""" """
@@ -157,6 +157,35 @@ class TranscriptionServer:
): ):
client: Optional[ServeClientBase] = None client: Optional[ServeClientBase] = None
# Check if client wants translation
enable_translation = options.get("enable_translation", False)
# Create translation queue if translation is enabled
translation_queue = None
translation_client = None
translation_thread = None
if enable_translation:
target_language = options.get("target_language", "fr")
translation_queue = queue.Queue()
from whisper_live.backend.translation_backend import ServeClientTranslation
translation_client = ServeClientTranslation(
client_uid=options["uid"],
websocket=websocket,
translation_queue=translation_queue,
target_language=target_language,
send_last_n_segments=options.get("send_last_n_segments", 10)
)
# Start translation thread
translation_thread = threading.Thread(
target=translation_client.speech_to_text,
daemon=True
)
translation_thread.start()
logging.info(f"Translation enabled for client {options['uid']} with target language: {target_language}")
if self.backend.is_tensorrt(): if self.backend.is_tensorrt():
try: try:
from whisper_live.backend.trt_backend import ServeClientTensorRT from whisper_live.backend.trt_backend import ServeClientTensorRT
@@ -235,6 +264,7 @@ class TranscriptionServer:
clip_audio=options.get("clip_audio", False), clip_audio=options.get("clip_audio", False),
same_output_threshold=options.get("same_output_threshold", 10), same_output_threshold=options.get("same_output_threshold", 10),
cache_path=self.cache_path, cache_path=self.cache_path,
translation_queue=translation_queue
) )
logging.info("Running faster_whisper backend.") logging.info("Running faster_whisper backend.")
@@ -245,6 +275,10 @@ class TranscriptionServer:
if client is None: if client is None:
raise ValueError(f"Backend type {self.backend.value} not recognised or not handled.") raise ValueError(f"Backend type {self.backend.value} not recognised or not handled.")
if translation_client:
client.translation_client = translation_client
client.translation_thread = translation_thread
self.client_manager.add_client(websocket, client) self.client_manager.add_client(websocket, client)
def get_audio_from_websocket(self, websocket): def get_audio_from_websocket(self, websocket):
@@ -443,6 +477,13 @@ class TranscriptionServer:
Args: Args:
websocket: The websocket associated with the client to be cleaned up. websocket: The websocket associated with the client to be cleaned up.
""" """
if self.client_manager.get_client(websocket): client = self.client_manager.get_client(websocket)
if client:
if hasattr(client, 'translation_client') and client.translation_client:
client.translation_client.cleanup()
# Wait for translation thread to finish
if hasattr(client, 'translation_thread') and client.translation_thread:
client.translation_thread.join(timeout=2.0)
self.client_manager.remove_client(websocket) self.client_manager.remove_client(websocket)
+3 -2
View File
@@ -11,10 +11,11 @@ def clear_screen():
os.system("cls" if os.name == "nt" else "clear") os.system("cls" if os.name == "nt" else "clear")
def print_transcript(text): def print_transcript(text, translated=False):
"""Prints formatted transcript text.""" """Prints formatted transcript text."""
wrapper = textwrap.TextWrapper(width=60) wrapper = textwrap.TextWrapper(width=60)
for line in wrapper.wrap(text="".join(text)): text=" ".join(text) if translated else "".join(text)
for line in wrapper.wrap(text=text):
print(line) print(line)