-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathaudio_client.py
More file actions
197 lines (151 loc) · 6.9 KB
/
audio_client.py
File metadata and controls
197 lines (151 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------
from __future__ import annotations
import json
import logging
import queue
import threading
from dataclasses import dataclass
from typing import Generator, List, Optional
from ..detail.core_interop import CoreInterop, InteropRequest
from ..exception import FoundryLocalException
from .live_audio_transcription_client import LiveAudioTranscriptionSession
logger = logging.getLogger(__name__)
class AudioSettings:
"""Settings supported by Foundry Local for audio transcription.
Attributes:
language: Language of the audio (e.g. ``"en"``).
temperature: Sampling temperature (0.0 for deterministic results).
"""
def __init__(
self,
language: Optional[str] = None,
temperature: Optional[float] = None,
):
self.language = language
self.temperature = temperature
@dataclass
class AudioTranscriptionResponse:
"""Response from an audio transcription request.
Attributes:
text: The transcribed text.
"""
text: str
class AudioClient:
"""OpenAI-compatible audio transcription client backed by Foundry Local Core.
Supports non-streaming and streaming transcription of audio files.
Attributes:
model_id: The ID of the loaded Whisper model variant.
settings: Tunable ``AudioSettings`` (language, temperature).
"""
def __init__(self, model_id: str, core_interop: CoreInterop):
self.model_id = model_id
self.settings = AudioSettings()
self._core_interop = core_interop
def create_live_transcription_session(self) -> LiveAudioTranscriptionSession:
"""Create a real-time streaming transcription session.
Audio data is pushed in as PCM chunks and transcription results are
returned as a synchronous generator.
Returns:
A streaming session that should be stopped when done.
Supports use as a context manager::
with audio_client.create_live_transcription_session() as session:
session.settings.sample_rate = 16000
session.start()
session.append(pcm_bytes)
for result in session.get_transcription_stream():
print(result.content[0].text)
"""
return LiveAudioTranscriptionSession(self.model_id, self._core_interop)
@staticmethod
def _validate_audio_file_path(audio_file_path: str) -> None:
"""Validate that the audio file path is a non-empty string."""
if not isinstance(audio_file_path, str) or audio_file_path.strip() == "":
raise ValueError("Audio file path must be a non-empty string.")
def _create_request_json(self, audio_file_path: str) -> str:
"""Build the JSON payload for the ``audio_transcribe`` native command."""
request: dict = {
"Model": self.model_id,
"FileName": audio_file_path,
}
metadata: dict[str, str] = {}
if self.settings.language is not None:
request["Language"] = self.settings.language
metadata["language"] = self.settings.language
if self.settings.temperature is not None:
request["Temperature"] = self.settings.temperature
metadata["temperature"] = str(self.settings.temperature)
if metadata:
request["metadata"] = metadata
return json.dumps(request)
def transcribe(self, audio_file_path: str) -> AudioTranscriptionResponse:
"""Transcribe an audio file (non-streaming).
Args:
audio_file_path: Path to the audio file to transcribe.
Returns:
An ``AudioTranscriptionResponse`` containing the transcribed text.
Raises:
ValueError: If *audio_file_path* is not a non-empty string.
FoundryLocalException: If the underlying native transcription command fails.
"""
self._validate_audio_file_path(audio_file_path)
request_json = self._create_request_json(audio_file_path)
request = InteropRequest(params={"OpenAICreateRequest": request_json})
response = self._core_interop.execute_command("audio_transcribe", request)
if response.error is not None:
raise FoundryLocalException(
f"Audio transcription failed for model '{self.model_id}': {response.error}"
)
data = json.loads(response.data)
return AudioTranscriptionResponse(text=data.get("text", ""))
def _stream_chunks(self, request_json: str) -> Generator[AudioTranscriptionResponse, None, None]:
"""Background-thread generator that yields parsed chunks from the native streaming call."""
_SENTINEL = object()
chunk_queue: queue.Queue = queue.Queue()
errors: List[Exception] = []
def _on_chunk(chunk_str: str) -> None:
chunk_data = json.loads(chunk_str)
chunk_queue.put(AudioTranscriptionResponse(text=chunk_data.get("text", "")))
def _run() -> None:
try:
resp = self._core_interop.execute_command_with_callback(
"audio_transcribe",
InteropRequest(params={"OpenAICreateRequest": request_json}),
_on_chunk,
)
if resp.error is not None:
errors.append(
FoundryLocalException(
f"Streaming audio transcription failed for model '{self.model_id}': {resp.error}"
)
)
except Exception as exc:
errors.append(exc)
finally:
chunk_queue.put(_SENTINEL)
threading.Thread(target=_run, daemon=True).start()
while (item := chunk_queue.get()) is not _SENTINEL:
yield item
if errors:
raise errors[0]
def transcribe_streaming(
self,
audio_file_path: str,
) -> Generator[AudioTranscriptionResponse, None, None]:
"""Transcribe an audio file with streaming chunks.
Consume with a standard ``for`` loop::
for chunk in audio_client.transcribe_streaming("recording.mp3"):
print(chunk.text, end="", flush=True)
Args:
audio_file_path: Path to the audio file to transcribe.
Returns:
A generator of ``AudioTranscriptionResponse`` objects.
Raises:
ValueError: If *audio_file_path* is not a non-empty string.
FoundryLocalException: If the underlying native transcription command fails.
"""
self._validate_audio_file_path(audio_file_path)
request_json = self._create_request_json(audio_file_path)
return self._stream_chunks(request_json)