Skip to content

Commit 4ba576f

Browse files
authored
Merge pull request #374 from xXLosKrachosXx/main
Add transcription callback to Client for handling transcription results
2 parents a27ac16 + 188b21f commit 4ba576f

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
@@ -38,6 +38,7 @@ def __init__(
3838
no_speech_thresh=0.45,
3939
clip_audio=False,
4040
same_output_threshold=10,
41+
transcription_callback=None,
4142
):
4243
"""
4344
Initializes a Client instance for audio recording and streaming to a server.
@@ -61,6 +62,7 @@ def __init__(
6162
no_speech_thresh (float, optional): Segments with no speech probability above this threshold will be discarded. Defaults to 0.45.
6263
clip_audio (bool, optional): Whether to clip audio with no valid segments. Defaults to False.
6364
same_output_threshold (int, optional): Number of repeated outputs before considering it as a valid segment. Defaults to 10.
65+
transcription_callback (callable, optional): A callback function to handle transcription results. Default is None.
6466
"""
6567
self.recording = False
6668
self.task = "transcribe"
@@ -83,6 +85,7 @@ def __init__(
8385
self.no_speech_thresh = no_speech_thresh
8486
self.clip_audio = clip_audio
8587
self.same_output_threshold = same_output_threshold
88+
self.transcription_callback = transcription_callback
8689

8790
if translate:
8891
self.task = "translate"
@@ -144,6 +147,14 @@ def process_segments(self, segments):
144147
self.last_response_received = time.time()
145148
self.last_received_segment = segments[-1]["text"]
146149

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

0 commit comments

Comments
 (0)