@@ -37,6 +37,7 @@ def __init__(
37
37
no_speech_thresh = 0.45 ,
38
38
clip_audio = False ,
39
39
same_output_threshold = 10 ,
40
+ transcription_callback = None ,
40
41
):
41
42
"""
42
43
Initializes a Client instance for audio recording and streaming to a server.
@@ -60,6 +61,7 @@ def __init__(
60
61
no_speech_thresh (float, optional): Segments with no speech probability above this threshold will be discarded. Defaults to 0.45.
61
62
clip_audio (bool, optional): Whether to clip audio with no valid segments. Defaults to False.
62
63
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.
63
65
"""
64
66
self .recording = False
65
67
self .task = "transcribe"
@@ -81,6 +83,7 @@ def __init__(
81
83
self .no_speech_thresh = no_speech_thresh
82
84
self .clip_audio = clip_audio
83
85
self .same_output_threshold = same_output_threshold
86
+ self .transcription_callback = transcription_callback
84
87
85
88
if translate :
86
89
self .task = "translate"
@@ -141,6 +144,14 @@ def process_segments(self, segments):
141
144
self .last_response_received = time .time ()
142
145
self .last_received_segment = segments [- 1 ]["text" ]
143
146
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
+
144
155
if self .log_transcription :
145
156
# Truncate to last 3 entries for brevity.
146
157
text = text [- 3 :]
0 commit comments