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