Add transcription callback to Client for handling transcription results

This commit is contained in:
Erik
2025-05-12 20:56:04 +02:00
parent d9d8d511c7
commit 188b21f1d0
+11
View File
@@ -37,6 +37,7 @@ class Client:
no_speech_thresh=0.45, no_speech_thresh=0.45,
clip_audio=False, clip_audio=False,
same_output_threshold=10, same_output_threshold=10,
transcription_callback=None,
): ):
""" """
Initializes a Client instance for audio recording and streaming to a server. Initializes a Client instance for audio recording and streaming to a server.
@@ -60,6 +61,7 @@ class Client:
no_speech_thresh (float, optional): Segments with no speech probability above this threshold will be discarded. Defaults to 0.45. no_speech_thresh (float, optional): Segments with no speech probability above this threshold will be discarded. Defaults to 0.45.
clip_audio (bool, optional): Whether to clip audio with no valid segments. Defaults to False. clip_audio (bool, optional): Whether to clip audio with no valid segments. Defaults to False.
same_output_threshold (int, optional): Number of repeated outputs before considering it as a valid segment. Defaults to 10. same_output_threshold (int, optional): Number of repeated outputs before considering it as a valid segment. Defaults to 10.
transcription_callback (callable, optional): A callback function to handle transcription results. Default is None.
""" """
self.recording = False self.recording = False
self.task = "transcribe" self.task = "transcribe"
@@ -81,6 +83,7 @@ class Client:
self.no_speech_thresh = no_speech_thresh self.no_speech_thresh = no_speech_thresh
self.clip_audio = clip_audio self.clip_audio = clip_audio
self.same_output_threshold = same_output_threshold self.same_output_threshold = same_output_threshold
self.transcription_callback = transcription_callback
if translate: if translate:
self.task = "translate" self.task = "translate"
@@ -141,6 +144,14 @@ class Client:
self.last_response_received = time.time() self.last_response_received = time.time()
self.last_received_segment = segments[-1]["text"] self.last_received_segment = segments[-1]["text"]
# call the transcription callback if provided
if self.transcription_callback and callable(self.transcription_callback):
try:
self.transcription_callback(" ".join(text), segments) # string, list
except Exception as e:
print(f"[WARN] transcription_callback raised: {e}")
return
if self.log_transcription: if self.log_transcription:
# Truncate to last 3 entries for brevity. # Truncate to last 3 entries for brevity.
text = text[-3:] text = text[-3:]