Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions deepgram/clients/abstract_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import httpx
import json

from .helpers import append_query_params
from ..options import DeepgramClientOptions
from .errors import DeepgramError, DeepgramApiError, DeepgramUnknownApiError
from .helpers import append_query_params
Expand All @@ -20,6 +21,8 @@ class AbstractAsyncRestClient:
Args:
url (Dict[str, str]): The base URL for the RESTful API, including any path segments.
headers (Optional[Dict[str, Any]]): Optional HTTP headers to include in requests.
params (Optional[Dict[str, Any]]): Optional query parameters to include in requests.
timeout (Optional[httpx.Timeout]): Optional timeout configuration for requests.

Exceptions:
DeepgramApiError: Raised for known API errors.
Expand All @@ -29,70 +32,80 @@ class AbstractAsyncRestClient:
def __init__(self, config: DeepgramClientOptions):
if config is None:
raise DeepgramError("Config are required")

self.config = config
self.client = httpx.AsyncClient()

async def get(self, url: str, options=None, addons=None, **kwargs):
async def get(self, url: str, options=None, addons=None, timeout=None, **kwargs):
return await self._handle_request(
"GET",
url,
params=options,
addons=addons,
timeout=timeout,
headers=self.config.headers,
**kwargs
)

async def post(self, url: str, options=None, addons=None, **kwargs):
async def post(self, url: str, options=None, addons=None, timeout=None, **kwargs):
return await self._handle_request(
"POST",
url,
params=options,
addons=addons,
timeout=timeout,
headers=self.config.headers,
**kwargs
)

async def put(self, url: str, options=None, addons=None, **kwargs):
async def put(self, url: str, options=None, addons=None, timeout=None, **kwargs):
return await self._handle_request(
"PUT",
url,
params=options,
addons=addons,
timeout=timeout,
headers=self.config.headers,
**kwargs
)

async def patch(self, url: str, options=None, addons=None, **kwargs):
async def patch(self, url: str, options=None, addons=None, timeout=None, **kwargs):
return await self._handle_request(
"PATCH",
url,
params=options,
addons=addons,
timeout=timeout,
headers=self.config.headers,
**kwargs
)

async def delete(self, url: str, options=None, addons=None, **kwargs):
async def delete(self, url: str, options=None, addons=None, timeout=None, **kwargs):
return await self._handle_request(
"DELETE",
url,
params=options,
addons=addons,
timeout=timeout,
headers=self.config.headers,
**kwargs
)

async def _handle_request(self, method, url, params, addons, headers, **kwargs):
async def _handle_request(
self, method, url, params, addons, timeout, headers, **kwargs
):
new_url = url
if params is not None:
new_url = append_query_params(new_url, params)
if addons is not None:
new_url = append_query_params(new_url, addons)

if timeout is None:
timeout = httpx.Timeout(30.0, connect=10.0)

try:
with httpx.Client() as client:
response = client.request(method, new_url, headers=headers, **kwargs)
async with httpx.AsyncClient(timeout=timeout) as client:
response = await client.request(
method, new_url, headers=headers, **kwargs
)
response.raise_for_status()
return response.text
except httpx._exceptions.HTTPError as e:
Expand Down
3 changes: 1 addition & 2 deletions deepgram/clients/abstract_sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class AbstractSyncRestClient:
def __init__(self, config: DeepgramClientOptions):
if config is None:
raise DeepgramError("Config are required")

self.config = config

def get(self, url: str, options=None, addons=None, timeout=None, **kwargs):
Expand Down Expand Up @@ -97,7 +96,7 @@ def _handle_request(self, method, url, params, addons, headers, timeout, **kwarg
new_url = append_query_params(new_url, addons)

if timeout is None:
timeout = httpx.Timeout(10.0, connect=10.0)
timeout = httpx.Timeout(30.0, connect=10.0)

try:
with httpx.Client(timeout=timeout) as client:
Expand Down
26 changes: 20 additions & 6 deletions deepgram/clients/prerecorded/v1/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
# SPDX-License-Identifier: MIT

import httpx
import logging, verboselogs
import json
import logging, verboselogs

Expand Down Expand Up @@ -47,14 +49,15 @@ async def transcribe_url(
source: UrlSource,
options: PrerecordedOptions = None,
addons: dict = None,
timeout: httpx.Timeout = None,
endpoint: str = "v1/listen",
) -> PrerecordedResponse:
self.logger.debug("PreRecordedClient.transcribe_url ENTER")

if options is not None and options.callback is not None:
self.logger.debug("PreRecordedClient.transcribe_url LEAVE")
return await self.transcribe_url_callback(
source, options["callback"], options, addons, endpoint
source, options["callback"], options, addons, timeout, endpoint
)

url = f"{self.config.url}/{endpoint}"
Expand All @@ -72,7 +75,9 @@ async def transcribe_url(
options = json.loads(options.to_json())
self.logger.info("options: %s", options)
self.logger.info("addons: %s", addons)
result = await self.post(url, options=options, addons=addons, json=body)
result = await self.post(
url, options=options, addons=addons, json=body, timeout=timeout
)
self.logger.info("json: %s", result)
res = PrerecordedResponse.from_json(result)
self.logger.verbose("result: %s", res)
Expand Down Expand Up @@ -102,6 +107,7 @@ async def transcribe_url_callback(
callback: str,
options: PrerecordedOptions = None,
addons: dict = None,
timeout: httpx.Timeout = None,
endpoint: str = "v1/listen",
) -> AsyncPrerecordedResponse:
self.logger.debug("PreRecordedClient.transcribe_url_callback ENTER")
Expand All @@ -124,7 +130,9 @@ async def transcribe_url_callback(
options = json.loads(options.to_json())
self.logger.info("options: %s", options)
self.logger.info("addons: %s", addons)
result = await self.post(url, options=options, addons=addons, json=body)
result = await self.post(
url, options=options, addons=addons, json=body, timeout=timeout
)
self.logger.info("json: %s", result)
res = AsyncPrerecordedResponse.from_json(result)
self.logger.verbose("result: %s", res)
Expand Down Expand Up @@ -152,14 +160,15 @@ async def transcribe_file(
source: FileSource,
options: PrerecordedOptions = None,
addons: dict = None,
timeout: httpx.Timeout = None,
endpoint: str = "v1/listen",
) -> PrerecordedResponse:
self.logger.debug("PreRecordedClient.transcribe_file ENTER")

if options is not None and options.callback is not None:
self.logger.debug("PreRecordedClient.transcribe_file LEAVE")
return await self.transcribe_file_callback(
source, options["callback"], options, addons, endpoint
source, options["callback"], options, addons, timeout, endpoint
)

url = f"{self.config.url}/{endpoint}"
Expand All @@ -178,7 +187,9 @@ async def transcribe_file(
options = json.loads(options.to_json())
self.logger.info("options: %s", options)
self.logger.info("addons: %s", addons)
result = await self.post(url, options=options, addons=addons, content=body)
result = await self.post(
url, options=options, addons=addons, content=body, timeout=timeout
)
self.logger.info("json: %s", result)
res = PrerecordedResponse.from_json(result)
self.logger.verbose("result: %s", res)
Expand Down Expand Up @@ -208,6 +219,7 @@ async def transcribe_file_callback(
callback: str,
options: PrerecordedOptions = None,
addons: dict = None,
timeout: httpx.Timeout = None,
endpoint: str = "v1/listen",
) -> AsyncPrerecordedResponse:
self.logger.debug("PreRecordedClient.transcribe_file_callback ENTER")
Expand All @@ -231,7 +243,9 @@ async def transcribe_file_callback(
options = json.loads(options.to_json())
self.logger.info("options: %s", options)
self.logger.info("addons: %s", addons)
result = await self.post(url, options=options, addons=addons, json=body)
result = await self.post(
url, options=options, addons=addons, json=body, timeout=timeout
)
self.logger.info("json: %s", result)
res = AsyncPrerecordedResponse.from_json(result)
self.logger.verbose("result: %s", res)
Expand Down
4 changes: 2 additions & 2 deletions deepgram/clients/prerecorded/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def transcribe_url(
if options is not None and options.callback is not None:
self.logger.debug("PreRecordedClient.transcribe_url LEAVE")
return self.transcribe_url_callback(
source, options["callback"], options, addons, endpoint
source, options["callback"], options, addons, timeout, endpoint
)

url = f"{self.config.url}/{endpoint}"
Expand Down Expand Up @@ -167,7 +167,7 @@ def transcribe_file(
if options is not None and options.callback is not None:
self.logger.debug("PreRecordedClient.transcribe_file LEAVE")
return self.transcribe_file_callback(
source, options["callback"], options, addons, endpoint
source, options["callback"], options, addons, timeout, endpoint
)

url = f"{self.config.url}/{endpoint}"
Expand Down
8 changes: 8 additions & 0 deletions examples/prerecorded/file/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
from dotenv import load_dotenv
import logging, verboselogs
from datetime import datetime, timedelta

from deepgram import (
DeepgramClientOptions,
Expand Down Expand Up @@ -42,8 +43,15 @@ def main():
punctuate=True,
diarize=True,
)

before = datetime.now()
response = deepgram.listen.prerecorded.v("1").transcribe_file(payload, options)
after = datetime.now()

print(response.to_json(indent=4))
print("")
difference = after - before
print(f"time: {difference.seconds}")

except Exception as e:
print(f"Exception: {e}")
Expand Down