Skip to content

Commit 0f5e99d

Browse files
Implement EnvVar for Containerized Images
1 parent 3bb2e8e commit 0f5e99d

File tree

5 files changed

+106
-15
lines changed

5 files changed

+106
-15
lines changed

deepgram/client.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
from .clients.onprem.client import OnPremClient
7878
from .clients.onprem.v1.async_client import AsyncOnPremClient
7979

80-
from .options import DeepgramClientOptions
80+
from .options import DeepgramClientOptions, ClientOptionsFromEnv
8181
from .errors import DeepgramApiKeyError, DeepgramModuleError
8282

8383

@@ -128,8 +128,16 @@ class DeepgramClient:
128128
"""
129129

130130
def __init__(
131-
self, api_key: str = "", config: Optional[DeepgramClientOptions] = None
131+
self,
132+
api_key: str = "",
133+
config: Optional[DeepgramClientOptions] = None,
132134
):
135+
verboselogs.install()
136+
self.logger = logging.getLogger(__name__)
137+
self.logger.addHandler(logging.StreamHandler())
138+
139+
if config is not None:
140+
api_key = config.api_key
133141
if not api_key:
134142
# Default to `None` for on-prem instances where an API key is not required
135143
api_key = os.getenv("DEEPGRAM_API_KEY", None)

deepgram/options.py

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ def __init__(
3838
self.verbose = verbose
3939
self.api_key = api_key
4040
self._update_headers(headers=headers)
41+
4142
if len(url) == 0:
4243
url = "api.deepgram.com"
4344
self.url = self._get_url(url)
45+
4446
if options is None:
4547
options = dict()
4648
self.options = options
@@ -70,18 +72,93 @@ def _update_headers(self, headers: Optional[Dict[str, str]] = None):
7072
class ClientOptionsFromEnv(DeepgramClientOptions):
7173
def __init__(
7274
self,
75+
api_key: str = "",
76+
url: str = "",
7377
verbose: int = logging.WARNING,
7478
headers: Dict[str, str] = None,
7579
options: Dict[str, str] = None,
7680
):
77-
apiKey = os.getenv("DEEPGRAM_API_KEY", None)
78-
if apiKey is None:
79-
raise DeepgramApiKeyError("Deepgram API KEY is not set")
81+
verboselogs.install()
82+
self.logger = logging.getLogger(__name__)
83+
self.logger.addHandler(logging.StreamHandler())
84+
self.logger.setLevel(logging.WARNING) # temporary set for setup
8085

81-
url = os.getenv("DEEPGRAM_URL", None)
82-
if url is None:
83-
url = "api.deepgram.com"
86+
if api_key == "":
87+
api_key = os.getenv("DEEPGRAM_API_KEY", None)
88+
if api_key is None:
89+
self.logger.critical("Deepgram API KEY is not set")
90+
raise DeepgramApiKeyError("Deepgram API KEY is not set")
91+
92+
if url == "":
93+
url = os.getenv("DEEPGRAM_HOST", "api.deepgram.com")
94+
self.logger.notice(f"Deepgram host is set to {url}")
95+
96+
if verbose == logging.WARNING:
97+
verbose = os.getenv("DEEPGRAM_LOGGING", logging.WARNING)
98+
if type(verbose) != int:
99+
match verbose:
100+
case "NOTSET":
101+
self.logger.notice("Logging level is set to NOTSET")
102+
verbose = logging.NOTSET
103+
case "SPAM":
104+
self.logger.notice("Logging level is set to SPAM")
105+
verbose = logging.SPAM
106+
case "DEBUG":
107+
self.logger.notice("Logging level is set to DEBUG")
108+
verbose = logging.DEBUG
109+
case "VERBOSE":
110+
self.logger.notice("Logging level is set to VERBOSE")
111+
verbose = logging.VERBOSE
112+
case "NOTICE":
113+
self.logger.notice("Logging level is set to NOTICE")
114+
verbose = logging.NOTICE
115+
case "WARNING":
116+
self.logger.notice("Logging level is set to WARNING")
117+
verbose = logging.WARNING
118+
case "SUCCESS":
119+
self.logger.notice("Logging level is set to SUCCESS")
120+
verbose = logging.SUCCESS
121+
case "ERROR":
122+
self.logger.notice("Logging level is set to ERROR")
123+
verbose = logging.ERROR
124+
case "CRITICAL":
125+
self.logger.notice("Logging level is set to CRITICAL")
126+
verbose = logging.CRITICAL
127+
case _:
128+
self.logger.notice("Logging level is set to WARNING")
129+
verbose = logging.WARNING
130+
self.logger.notice(f"Logging level is set to {verbose}")
131+
132+
if headers is None:
133+
headers = dict()
134+
for x in range(0, 20):
135+
header = os.getenv(f"DEEPGRAM_HEADER_{x}", None)
136+
if header is not None:
137+
headers[header] = os.getenv(f"DEEPGRAM_HEADER_VALUE_{x}", None)
138+
self.logger.debug(
139+
f"Deepgram header {header} is set with value {headers[header]}"
140+
)
141+
else:
142+
break
143+
if len(headers) == 0:
144+
self.logger.notice("Deepgram headers are not set")
145+
headers = None
146+
147+
if options is None:
148+
options = dict()
149+
for x in range(0, 20):
150+
param = os.getenv(f"DEEPGRAM_PARAM_{x}", None)
151+
if param is not None:
152+
options[param] = os.getenv(f"DEEPGRAM_PARAM_VALUE_{x}", None)
153+
self.logger.debug(
154+
f"Deepgram option {param} is set with value {options[param]}"
155+
)
156+
else:
157+
break
158+
if len(options) == 0:
159+
self.logger.notice("Deepgram options are not set")
160+
options = None
84161

85162
super().__init__(
86-
api_key=apiKey, url=url, verbose=verbose, headers=headers, options=options
163+
api_key=api_key, url=url, verbose=verbose, headers=headers, options=options
87164
)

examples/prerecorded/file/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
from datetime import datetime, timedelta
99

1010
from deepgram import (
11-
DeepgramClientOptions,
1211
DeepgramClient,
12+
DeepgramClientOptions,
1313
PrerecordedOptions,
1414
FileSource,
1515
)
@@ -22,11 +22,11 @@
2222
def main():
2323
try:
2424
# STEP 1 Create a Deepgram client using the API key in the environment variables
25-
config: DeepgramClientOptions = DeepgramClientOptions(
25+
config = DeepgramClientOptions(
2626
verbose=logging.SPAM,
2727
)
2828

29-
deepgram: DeepgramClient = DeepgramClient("", config)
29+
deepgram = DeepgramClient("", config)
3030

3131
# STEP 2 Call the transcribe_file method on the prerecorded class
3232
with open(AUDIO_FILE, "rb") as file:

examples/prerecorded/url/main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
from dotenv import load_dotenv
77
import logging, verboselogs
88

9-
from deepgram import DeepgramClient, DeepgramClientOptions, PrerecordedOptions
9+
from deepgram import (
10+
DeepgramClient,
11+
ClientOptionsFromEnv,
12+
PrerecordedOptions,
13+
)
1014

1115
load_dotenv()
1216

@@ -18,7 +22,7 @@
1822
def main():
1923
try:
2024
# STEP 1 Create a Deepgram client using the API key from environment variables
21-
deepgram = DeepgramClient()
25+
deepgram = DeepgramClient("", ClientOptionsFromEnv())
2226

2327
# STEP 2 Call the transcribe_url method on the prerecorded class
2428
options = PrerecordedOptions(

examples/streaming/async_http/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
load_dotenv()
1313

14+
API_KEY = os.getenv("DG_API_KEY")
15+
1416
options = LiveOptions(
1517
model="nova",
1618
interim_results=False,
@@ -22,7 +24,7 @@
2224

2325

2426
async def main():
25-
deepgram = DeepgramClient()
27+
deepgram = DeepgramClient(API_KEY)
2628

2729
# Create a websocket connection to Deepgram
2830
try:

0 commit comments

Comments
 (0)