Skip to content

Commit bd33446

Browse files
authored
Merge pull request #16 from mideind/keys-env-support
Allow reading keys from .env files (as well as environment variables)
2 parents 5dbdc80 + a10ba65 commit bd33446

File tree

1 file changed

+24
-41
lines changed

1 file changed

+24
-41
lines changed

src/icespeak/settings.py

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,13 @@ class Settings(BaseSettings):
109109
AUDIO_DIR: Optional[Path] = Field(
110110
default=None,
111111
description=(
112-
"Where to save output audio files. "
113-
"If not set, creates a directory in the platform's temporary directory."
112+
"Where to save output audio files. If not set, creates a directory in the platform's temporary directory."
114113
),
115114
)
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-
)
115+
AUDIO_CACHE_SIZE: int = Field(default=300, gt=-1, description="Max number of audio files to cache.")
116+
AUDIO_CACHE_CLEAN: bool = Field(default=True, description="If True, cleans up generated audio files upon exit.")
122117

123-
KEYS_DIR: Path = Field(
124-
default=Path("keys"), description="Where to look for API keys."
125-
)
118+
KEYS_DIR: Path = Field(default=Path("keys"), description="Where to look for API keys.")
126119
AWSPOLLY_KEY_FILENAME: str = Field(
127120
default="AWSPollyServerKey.json",
128121
description="Name of the AWS Polly API key file.",
@@ -140,6 +133,11 @@ class Settings(BaseSettings):
140133
description="Name of the OpenAI API key file.",
141134
)
142135

136+
AWSPOLLY_API_KEY: Optional[str] = Field(default=None, description="AWS Polly API key as JSON string")
137+
AZURE_API_KEY: Optional[str] = Field(default=None, description="Azure API key as JSON string")
138+
GOOGLE_API_KEY: Optional[str] = Field(default=None, description="Google API key as JSON string")
139+
OPENAI_API_KEY: Optional[str] = Field(default=None, description="OpenAI API key string")
140+
143141
def get_audio_dir(self) -> Path:
144142
"""
145143
Return directory for saving output audio files.
@@ -187,9 +185,7 @@ class Keys(BaseModel):
187185

188186
azure: Optional[AzureKey] = Field(default=None, description="Azure API key.")
189187
aws: Optional[AWSPollyKey] = Field(default=None, description="AWS Polly API key.")
190-
google: Optional[dict[str, Any]] = Field(
191-
default=None, description="Google API key."
192-
)
188+
google: Optional[dict[str, Any]] = Field(default=None, description="Google API key.")
193189
openai: Optional[OpenAIKey] = Field(default=None, description="OpenAI API key.")
194190

195191
def __hash__(self):
@@ -213,60 +209,47 @@ def __eq__(self, other: object):
213209

214210
_kd = SETTINGS.KEYS_DIR
215211
if not (_kd.exists() and _kd.is_dir()):
216-
_LOG.warning(
217-
"Keys directory missing or incorrect: %s", _kd
218-
)
212+
_LOG.warning("Keys directory missing or incorrect: %s", _kd)
219213

220214
# Load API keys, logging exceptions in level DEBUG so they aren't logged twice,
221215
# as exceptions are logged as warnings when voice modules are initialized
222216

223217
# Amazon Polly
224218
try:
225-
if key := os.getenv("ICESPEAK_AWSPOLLY_API_KEY"):
226-
API_KEYS.aws = AWSPollyKey.model_validate_json(key)
219+
if SETTINGS.AWSPOLLY_API_KEY:
220+
API_KEYS.aws = AWSPollyKey.model_validate_json(SETTINGS.AWSPOLLY_API_KEY)
227221
else:
228-
API_KEYS.aws = AWSPollyKey.model_validate_json(
229-
(_kd / SETTINGS.AWSPOLLY_KEY_FILENAME).read_text().strip()
230-
)
222+
API_KEYS.aws = AWSPollyKey.model_validate_json((_kd / SETTINGS.AWSPOLLY_KEY_FILENAME).read_text().strip())
231223
except Exception as err:
232224
_LOG.debug(
233225
"Could not load AWS Polly API key, ASR with AWS Polly will not work. Error: %s",
234226
err,
235227
)
236228
# Azure
237229
try:
238-
if key := os.getenv("ICESPEAK_AZURE_API_KEY"):
239-
API_KEYS.azure = AzureKey.model_validate_json(key)
230+
if SETTINGS.AZURE_API_KEY:
231+
API_KEYS.azure = AzureKey.model_validate_json(SETTINGS.AZURE_API_KEY)
240232
else:
241-
API_KEYS.azure = AzureKey.model_validate_json(
242-
(_kd / SETTINGS.AZURE_KEY_FILENAME).read_text().strip()
243-
)
233+
API_KEYS.azure = AzureKey.model_validate_json((_kd / SETTINGS.AZURE_KEY_FILENAME).read_text().strip())
244234
except Exception as err:
245-
_LOG.debug(
246-
"Could not load Azure API key, ASR with Azure will not work. Error: %s", err
247-
)
235+
_LOG.debug("Could not load Azure API key, ASR with Azure will not work. Error: %s", err)
248236
# Google
249237
try:
250-
if key := os.getenv("ICESPEAK_GOOGLE_API_KEY"):
251-
API_KEYS.google = json.loads(key)
238+
if SETTINGS.GOOGLE_API_KEY:
239+
API_KEYS.google = json.loads(SETTINGS.GOOGLE_API_KEY)
252240
else:
253-
API_KEYS.google = json.loads(
254-
(_kd / SETTINGS.GOOGLE_KEY_FILENAME).read_text().strip()
255-
)
241+
API_KEYS.google = json.loads((_kd / SETTINGS.GOOGLE_KEY_FILENAME).read_text().strip())
256242
except Exception as err:
257243
_LOG.debug(
258244
"Could not load Google API key, ASR with Google will not work. Error: %s",
259245
err,
260246
)
261247
# OpenAI
262248
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))
249+
if SETTINGS.OPENAI_API_KEY:
250+
API_KEYS.openai = OpenAIKey(api_key=SecretStr(SETTINGS.OPENAI_API_KEY))
266251
else:
267-
API_KEYS.openai = OpenAIKey.model_validate_json(
268-
(_kd / SETTINGS.OPENAI_KEY_FILENAME).read_text().strip()
269-
)
252+
API_KEYS.openai = OpenAIKey.model_validate_json((_kd / SETTINGS.OPENAI_KEY_FILENAME).read_text().strip())
270253
except Exception as err:
271254
_LOG.debug(
272255
"Could not load OpenAI API key, ASR with OpenAI will not work. Error: %s",

0 commit comments

Comments
 (0)