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

Add options for handling multilingual input #200

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
17 changes: 13 additions & 4 deletions whisper_live/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ def initialize_client(
task=options["task"],
client_uid=options["uid"],
model=options["model"],
multilingual_input=options.get("multilingual_input"),
lang_filter=options.get("lang_filter"),
initial_prompt=options.get("initial_prompt"),
vad_parameters=options.get("vad_parameters"),
use_vad=self.use_vad,
Expand Down Expand Up @@ -681,7 +683,7 @@ def speech_to_text(self):


class ServeClientFasterWhisper(ServeClientBase):
def __init__(self, websocket, task="transcribe", device=None, language=None, client_uid=None, model="small.en",
def __init__(self, websocket, task="transcribe", device=None, language=None, lang_filter=None, client_uid=None, model="small.en", multilingual_input=False,
initial_prompt=None, vad_parameters=None, use_vad=True):
"""
Initialize a ServeClient instance.
Expand All @@ -694,6 +696,8 @@ def __init__(self, websocket, task="transcribe", device=None, language=None, cli
task (str, optional): The task type, e.g., "transcribe." Defaults to "transcribe".
device (str, optional): The device type for Whisper, "cuda" or "cpu". Defaults to None.
language (str, optional): The language for transcription. Defaults to None.
lang_filter (List[str], optional): List of candidate languages to listen for. Defaults to None, meaning listen for all known languages.
multilingual_input (bool, optional): True means the input stream may contain more than one language. Defaults to False.
client_uid (str, optional): A unique identifier for the client. Defaults to None.
model (str, optional): The whisper model size. Defaults to 'small.en'
initial_prompt (str, optional): Prompt for whisper inference. Defaults to None.
Expand All @@ -708,6 +712,8 @@ def __init__(self, websocket, task="transcribe", device=None, language=None, cli
else:
self.model_size_or_path = model
self.language = "en" if self.model_size_or_path.endswith("en") else language
self.lang_filter = lang_filter
self.multilingual_input = multilingual_input
self.task = task
self.initial_prompt = initial_prompt
self.vad_parameters = vad_parameters or {"threshold": 0.5}
Expand Down Expand Up @@ -797,13 +803,16 @@ def transcribe_audio(self, input_sample):
result, info = self.transcriber.transcribe(
input_sample,
initial_prompt=self.initial_prompt,
language=self.language,
language=self.language if not self.multilingual_input else None,
lang_filter=self.lang_filter,
task=self.task,
vad_filter=self.use_vad,
vad_parameters=self.vad_parameters if self.use_vad else None)

if self.language is None and info is not None:
self.set_language(info)
if info is not None:
if (self.language is None) or (self.language != info.language):
self.set_language(info)

return result

def get_previous_output(self):
Expand Down
7 changes: 6 additions & 1 deletion whisper_live/transcriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def transcribe( # noqa:
self,
audio: Union[str, BinaryIO, np.ndarray],
language: Optional[str] = None,
lang_filter : List[str] = None,
task: str = "transcribe",
beam_size: int = 5,
best_of: int = 5,
Expand Down Expand Up @@ -352,8 +353,12 @@ def transcribe( # noqa:
results = self.model.detect_language(encoder_output)[0]
# Parse language names to strip out markers
all_language_probs = [(token[2:-2], prob) for (token, prob) in results]
filtered_language_probs = all_language_probs
if lang_filter:
filtered_language_probs = [p for p in all_language_probs
if p[0] in lang_filter]
# Get top language token and probability
language, language_probability = all_language_probs[0]
language, language_probability = filtered_language_probs[0]

self.logger.info(
"Detected language '%s' with probability %.2f",
Expand Down
Loading