Skip to content

Commit 188b21f

Browse files
Add transcription callback to Client for handling transcription results
1 parent d9d8d51 commit 188b21f

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

whisper_live/client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def __init__(
3737
no_speech_thresh=0.45,
3838
clip_audio=False,
3939
same_output_threshold=10,
40+
transcription_callback=None,
4041
):
4142
"""
4243
Initializes a Client instance for audio recording and streaming to a server.
@@ -60,6 +61,7 @@ def __init__(
6061
no_speech_thresh (float, optional): Segments with no speech probability above this threshold will be discarded. Defaults to 0.45.
6162
clip_audio (bool, optional): Whether to clip audio with no valid segments. Defaults to False.
6263
same_output_threshold (int, optional): Number of repeated outputs before considering it as a valid segment. Defaults to 10.
64+
transcription_callback (callable, optional): A callback function to handle transcription results. Default is None.
6365
"""
6466
self.recording = False
6567
self.task = "transcribe"
@@ -81,6 +83,7 @@ def __init__(
8183
self.no_speech_thresh = no_speech_thresh
8284
self.clip_audio = clip_audio
8385
self.same_output_threshold = same_output_threshold
86+
self.transcription_callback = transcription_callback
8487

8588
if translate:
8689
self.task = "translate"
@@ -141,6 +144,14 @@ def process_segments(self, segments):
141144
self.last_response_received = time.time()
142145
self.last_received_segment = segments[-1]["text"]
143146

147+
# call the transcription callback if provided
148+
if self.transcription_callback and callable(self.transcription_callback):
149+
try:
150+
self.transcription_callback(" ".join(text), segments) # string, list
151+
except Exception as e:
152+
print(f"[WARN] transcription_callback raised: {e}")
153+
return
154+
144155
if self.log_transcription:
145156
# Truncate to last 3 entries for brevity.
146157
text = text[-3:]

0 commit comments

Comments
 (0)