24
24
# We dont import annotations from __future__ here
25
25
# due to pydantic
26
26
from typing import Any , Optional
27
- from typing_extensions import Literal
28
27
29
28
import json
30
29
import os
@@ -114,10 +113,16 @@ class Settings(BaseSettings):
114
113
"If not set, creates a directory in the platform's temporary directory."
115
114
),
116
115
)
117
- AUDIO_CACHE_SIZE : int = Field (default = 300 , gt = 0 , description = "Max number of audio files to cache." )
118
- AUDIO_CACHE_CLEAN : bool = Field (default = True , description = "If True, cleans up generated audio files upon exit." )
116
+ AUDIO_CACHE_SIZE : int = Field (
117
+ default = 300 , gt = - 1 , description = "Max number of audio files to cache."
118
+ )
119
+ AUDIO_CACHE_CLEAN : bool = Field (
120
+ default = True , description = "If True, cleans up generated audio files upon exit."
121
+ )
119
122
120
- KEYS_DIR : Path = Field (default = Path ("keys" ), description = "Where to look for API keys." )
123
+ KEYS_DIR : Path = Field (
124
+ default = Path ("keys" ), description = "Where to look for API keys."
125
+ )
121
126
AWSPOLLY_KEY_FILENAME : str = Field (
122
127
default = "AWSPollyServerKey.json" ,
123
128
description = "Name of the AWS Polly API key file." ,
@@ -182,26 +187,24 @@ class Keys(BaseModel):
182
187
183
188
azure : Optional [AzureKey ] = Field (default = None , description = "Azure API key." )
184
189
aws : Optional [AWSPollyKey ] = Field (default = None , description = "AWS Polly API key." )
185
- google : Optional [dict [str , Any ]] = Field (default = None , description = "Google API key." )
186
- # TODO: Re-implement TTS with Tiro
187
- tiro : Literal [ None ] = Field ( default = None )
190
+ google : Optional [dict [str , Any ]] = Field (
191
+ default = None , description = "Google API key."
192
+ )
188
193
openai : Optional [OpenAIKey ] = Field (default = None , description = "OpenAI API key." )
189
194
190
195
def __hash__ (self ):
191
- return hash ((self .azure , self .aws , self .google , self .tiro , self . openai ))
196
+ return hash ((self .azure , self .aws , self .google , self .openai ))
192
197
193
198
def __eq__ (self , other : object ):
194
199
return isinstance (other , Keys ) and (
195
200
self .azure ,
196
201
self .aws ,
197
202
self .google ,
198
- self .tiro ,
199
203
self .openai ,
200
204
) == (
201
205
other .azure ,
202
206
other .aws ,
203
207
other .google ,
204
- other .tiro ,
205
208
other .openai ,
206
209
)
207
210
@@ -210,36 +213,62 @@ def __eq__(self, other: object):
210
213
211
214
_kd = SETTINGS .KEYS_DIR
212
215
if not (_kd .exists () and _kd .is_dir ()):
213
- _LOG .warning ("Keys directory missing or incorrect, TTS will not work! Set to: %s" , _kd )
214
- else :
215
- # Load API keys, logging exceptions in level DEBUG so they aren't logged twice,
216
- # as exceptions are logged as warnings when voice modules are initialized
217
- try :
218
- API_KEYS .aws = AWSPollyKey .model_validate_json ((_kd / SETTINGS .AWSPOLLY_KEY_FILENAME ).read_text ().strip ())
219
- except Exception as err :
220
- _LOG .debug (
221
- "Could not load AWS Polly API key, ASR with AWS Polly will not work. Error: %s" ,
222
- err ,
216
+ _LOG .warning (
217
+ "Keys directory missing or incorrect: %s" , _kd
218
+ )
219
+
220
+ # Load API keys, logging exceptions in level DEBUG so they aren't logged twice,
221
+ # as exceptions are logged as warnings when voice modules are initialized
222
+
223
+ # Amazon Polly
224
+ try :
225
+ if key := os .getenv ("ICESPEAK_AWSPOLLY_API_KEY" ):
226
+ API_KEYS .aws = AWSPollyKey .model_validate_json (key )
227
+ else :
228
+ API_KEYS .aws = AWSPollyKey .model_validate_json (
229
+ (_kd / SETTINGS .AWSPOLLY_KEY_FILENAME ).read_text ().strip ()
230
+ )
231
+ except Exception as err :
232
+ _LOG .debug (
233
+ "Could not load AWS Polly API key, ASR with AWS Polly will not work. Error: %s" ,
234
+ err ,
235
+ )
236
+ # Azure
237
+ try :
238
+ if key := os .getenv ("ICESPEAK_AZURE_API_KEY" ):
239
+ API_KEYS .azure = AzureKey .model_validate_json (key )
240
+ else :
241
+ API_KEYS .azure = AzureKey .model_validate_json (
242
+ (_kd / SETTINGS .AZURE_KEY_FILENAME ).read_text ().strip ()
223
243
)
224
- try :
225
- API_KEYS .azure = AzureKey .model_validate_json ((_kd / SETTINGS .AZURE_KEY_FILENAME ).read_text ().strip ())
226
- except Exception as err :
227
- _LOG .debug ("Could not load Azure API key, ASR with Azure will not work. Error: %s" , err )
228
- try :
229
- API_KEYS .google = json .loads ((_kd / SETTINGS .GOOGLE_KEY_FILENAME ).read_text ().strip ())
230
- except Exception as err :
231
- _LOG .debug (
232
- "Could not load Google API key, ASR with Google will not work. Error: %s" ,
233
- err ,
244
+ except Exception as err :
245
+ _LOG .debug (
246
+ "Could not load Azure API key, ASR with Azure will not work. Error: %s" , err
247
+ )
248
+ # Google
249
+ try :
250
+ if key := os .getenv ("ICESPEAK_GOOGLE_API_KEY" ):
251
+ API_KEYS .google = json .loads (key )
252
+ else :
253
+ API_KEYS .google = json .loads (
254
+ (_kd / SETTINGS .GOOGLE_KEY_FILENAME ).read_text ().strip ()
234
255
)
235
- try :
236
- # First try to load the key from environment variable OPENAI_API_KEY
237
- if key := os .getenv ("OPENAI_API_KEY" ):
238
- API_KEYS .openai = OpenAIKey (api_key = SecretStr (key ))
239
- else :
240
- API_KEYS .openai = OpenAIKey .model_validate_json ((_kd / SETTINGS .OPENAI_KEY_FILENAME ).read_text ().strip ())
241
- except Exception as err :
242
- _LOG .debug (
243
- "Could not load OpenAI API key, ASR with OpenAI will not work. Error: %s" ,
244
- err ,
256
+ except Exception as err :
257
+ _LOG .debug (
258
+ "Could not load Google API key, ASR with Google will not work. Error: %s" ,
259
+ err ,
260
+ )
261
+ # OpenAI
262
+ try :
263
+ # First try to load the key from environment variable OPENAI_API_KEY
264
+ if key := os .getenv ("ICESPEAK_OPENAI_API_KEY" ):
265
+ API_KEYS .openai = OpenAIKey (api_key = SecretStr (key ))
266
+ else :
267
+ API_KEYS .openai = OpenAIKey .model_validate_json (
268
+ (_kd / SETTINGS .OPENAI_KEY_FILENAME ).read_text ().strip ()
245
269
)
270
+ except Exception as err :
271
+ _LOG .debug (
272
+ "Could not load OpenAI API key, ASR with OpenAI will not work. Error: %s" ,
273
+ err ,
274
+ )
0 commit comments