forked from bigbluebutton/bbb-livekit-stt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
244 lines (202 loc) · 8.38 KB
/
config.py
File metadata and controls
244 lines (202 loc) · 8.38 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import json
import os
from dataclasses import asdict, dataclass, field
from typing import Any, Dict, List, Literal
DEFAULT_TRANSLATION_LANG_MAP = "de:de-DE,en:en-US,es:es-ES,fr:fr-FR,hi:hi-IN,it:it-IT,ja:ja-JP,pt:pt-BR,ru:ru-RU,zh:zh-CN"
REDACTED_CONFIG_KEYS = {"api_key", "password", "secret", "token"}
def _get_float_env(key: str, default: float) -> float:
val = os.getenv(key)
if val is None:
return default
return float(val)
def _get_bool_env(key: str, default: bool | None) -> bool | None:
val = os.getenv(key)
if val is None:
return default
return val.lower() in ("true", "1", "t")
def _get_list_env(key: str, default: List[str] | None) -> List[str] | None:
val = os.getenv(key)
if val is None:
return default
if not val:
return []
return [item.strip() for item in val.split(",")]
def _get_json_env(key: str) -> Any | None:
val = os.getenv(key)
if not val:
return None
try:
return json.loads(val)
except json.JSONDecodeError as e:
print(f"Warning: Could not parse {key} as JSON: {e}")
return None
def _get_map_env(key: str, default_str: str = "") -> Dict[str, str]:
val = os.getenv(key, default_str)
if not val:
return {}
lang_map = {}
try:
pairs = val.split(",")
for pair in pairs:
if ":" in pair:
lang, bbb_locale = pair.split(":", 1)
lang_map[lang.strip()] = bbb_locale.strip()
except Exception as e:
print(f"Warning: Could not parse {key}: {e}")
return lang_map
@dataclass
class RedisConfig:
host: str = field(default_factory=lambda: os.getenv("REDIS_HOST", "127.0.0.1"))
port: int = field(default_factory=lambda: int(os.getenv("REDIS_PORT", 6379)))
password: str = field(default_factory=lambda: os.getenv("REDIS_PASSWORD", ""))
redis_config = RedisConfig()
# This is a mapping of the Gladia STT plugin configuration options to environment variables.
# The plugin uses the defaults if the environment variables are not set.
# See https://github.com/livekit/livekit-agents/blob/main/livekit/plugins/gladia/stt.py
@dataclass
class GladiaConfig:
api_key: str | None = field(default_factory=lambda: os.getenv("GLADIA_API_KEY"))
min_confidence_interim: float = field(
default_factory=lambda: _get_float_env(
"GLADIA_MIN_CONFIDENCE_INTERIM",
_get_float_env("GLADIA_MIN_CONFIDENCE", 0.1),
)
)
min_confidence_final: float = field(
default_factory=lambda: _get_float_env(
"GLADIA_MIN_CONFIDENCE_FINAL",
_get_float_env("GLADIA_MIN_CONFIDENCE", 0.1),
)
)
model: str | None = field(default_factory=lambda: os.getenv("GLADIA_MODEL", None))
base_url: str | None = field(
default_factory=lambda: os.getenv("GLADIA_BASE_URL", None)
)
interim_results: bool | None = field(
default_factory=lambda: _get_bool_env("GLADIA_INTERIM_RESULTS", None)
)
languages: List[str] | None = field(
default_factory=lambda: _get_list_env("GLADIA_LANGUAGES", None)
)
code_switching: bool | None = field(
default_factory=lambda: _get_bool_env("GLADIA_CODE_SWITCHING", False)
)
sample_rate: int = field(
default_factory=lambda: int(os.getenv("GLADIA_SAMPLE_RATE", 16000))
)
bit_depth: int = field(
default_factory=lambda: int(os.getenv("GLADIA_BIT_DEPTH", 16))
)
channels: int = field(default_factory=lambda: int(os.getenv("GLADIA_CHANNELS", 1)))
encoding: Literal["wav/pcm", "wav/alaw", "wav/ulaw"] = field(
default_factory=lambda: os.getenv("GLADIA_ENCODING", "wav/pcm")
)
endpointing: float | None = field(
default_factory=lambda: _get_float_env("GLADIA_ENDPOINTING", 0.01)
)
maximum_duration_without_endpointing: float | None = field(
default_factory=lambda: _get_float_env(
"GLADIA_MAXIMUM_DURATION_WITHOUT_ENDPOINTING", None
)
)
region: str | None = field(default_factory=lambda: os.getenv("GLADIA_REGION", None))
energy_filter: bool | None = field(
default_factory=lambda: _get_bool_env("GLADIA_ENERGY_FILTER", None)
)
translation_enabled: bool | None = field(
default_factory=lambda: _get_bool_env("GLADIA_TRANSLATION_ENABLED", None)
)
translation_target_languages: List[str] | None = field(
default_factory=lambda: _get_list_env(
"GLADIA_TRANSLATION_TARGET_LANGUAGES", None
)
)
translation_model: str | None = field(
default_factory=lambda: os.getenv("GLADIA_TRANSLATION_MODEL", None)
)
translation_match_original_utterances: bool | None = field(
default_factory=lambda: _get_bool_env(
"GLADIA_TRANSLATION_MATCH_ORIGINAL_UTTERANCES", None
)
)
translation_lipsync: bool | None = field(
default_factory=lambda: _get_bool_env("GLADIA_TRANSLATION_LIPSYNC", None)
)
translation_context_adaptation: bool | None = field(
default_factory=lambda: _get_bool_env(
"GLADIA_TRANSLATION_CONTEXT_ADAPTATION", None
)
)
translation_context: str | None = field(
default_factory=lambda: os.getenv("GLADIA_TRANSLATION_CONTEXT", None)
)
translation_informal: bool | None = field(
default_factory=lambda: _get_bool_env("GLADIA_TRANSLATION_INFORMAL", None)
)
translation_lang_map: Dict[str, str] = field(
default_factory=lambda: _get_map_env(
"GLADIA_TRANSLATION_LANG_MAP", DEFAULT_TRANSLATION_LANG_MAP
)
)
custom_vocabulary: List[Any] | None = field(
default_factory=lambda: _get_json_env("GLADIA_CUSTOM_VOCABULARY")
)
custom_spelling: Dict[str, List[str]] | None = field(
default_factory=lambda: _get_json_env("GLADIA_CUSTOM_SPELLING")
)
pre_processing_audio_enhancer: bool = field(
default_factory=lambda: _get_bool_env(
"GLADIA_PRE_PROCESSING_AUDIO_ENHANCER", True
)
)
pre_processing_speech_threshold: float | None = field(
default_factory=lambda: _get_float_env(
"GLADIA_PRE_PROCESSING_SPEECH_THRESHOLD", 0.7
)
)
def to_dict(self):
# Exclude None values so defaults are used by the plugin
data = {
"api_key": self.api_key,
"model": self.model,
"base_url": self.base_url,
"interim_results": self.interim_results,
"languages": self.languages,
"code_switching": self.code_switching,
"sample_rate": self.sample_rate,
"bit_depth": self.bit_depth,
"channels": self.channels,
"encoding": self.encoding,
"endpointing": self.endpointing,
"maximum_duration_without_endpointing": self.maximum_duration_without_endpointing,
"region": self.region,
"energy_filter": self.energy_filter,
"translation_enabled": self.translation_enabled,
"translation_target_languages": self.translation_target_languages,
"translation_model": self.translation_model,
"translation_match_original_utterances": self.translation_match_original_utterances,
"translation_lipsync": self.translation_lipsync,
"translation_context_adaptation": self.translation_context_adaptation,
"translation_context": self.translation_context,
"translation_informal": self.translation_informal,
"custom_vocabulary": self.custom_vocabulary,
"custom_spelling": self.custom_spelling,
"pre_processing_audio_enhancer": self.pre_processing_audio_enhancer,
"pre_processing_speech_threshold": self.pre_processing_speech_threshold,
}
return {k: v for k, v in data.items() if v is not None}
gladia_config = GladiaConfig()
def redact_config_values(value: object, key: str | None = None) -> object:
if key and key.lower() in REDACTED_CONFIG_KEYS:
return "***REDACTED***" if value not in (None, "") else value
if isinstance(value, dict):
return {k: redact_config_values(v, k) for k, v in value.items()}
if isinstance(value, list):
return [redact_config_values(item) for item in value]
return value
def get_redacted_app_config() -> Dict[str, Any]:
config_payload = {
"redis": asdict(redis_config),
"gladia": asdict(gladia_config),
}
return redact_config_values(config_payload)