Skip to content

Commit

Permalink
small changes based on Julias PR recomendations
Browse files Browse the repository at this point in the history
  • Loading branch information
SandraRodgers committed Oct 30, 2023
1 parent 843bcbd commit 33a26b9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
2 changes: 0 additions & 2 deletions deepgram/clients/abstract_restful_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ async def delete(self, url: str):
return await self._handle_request('DELETE', url, headers=headers)

async def _handle_request(self, method, url, **kwargs):
# for key, value in kwargs.items():
# print(f"Request Argument - {key}: {value}")
try:
with httpx.Client() as client:
response = client.request(method, url, **kwargs)
Expand Down
1 change: 0 additions & 1 deletion deepgram/clients/listen_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def __init__(self, url: str, api_key: str, headers: Optional[Dict[str, Any]]):
self.url = url
self.api_key = api_key
self.headers = headers
print('hey')

@property
def prerecorded(self):
Expand Down
30 changes: 17 additions & 13 deletions deepgram/clients/prerecorded_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from ..types.prerecorded_response import AsyncPrerecordedResponse
from ..types.prerecorded_response import SyncPrerecordedResponse


class PreRecordedClient(AbstractRestfulClient):
"""
A client class for handling pre-recorded audio data. Provides methods for transcribing audio from URLs and files.
"""

def __init__(self, url, headers):
"""
Initializes a new instance of the PreRecordedClient.
Expand All @@ -21,9 +23,9 @@ def __init__(self, url, headers):
self.url = url
self.headers = headers
super().__init__(url, headers)

async def transcribe_url(
self, source: UrlSource, options: PrerecordedOptions = None, endpoint: str="v1/listen"
self, source: UrlSource, options: PrerecordedOptions = None, endpoint: str = "v1/listen"
) -> SyncPrerecordedResponse:
"""
Transcribes audio from a URL source.
Expand All @@ -38,6 +40,7 @@ async def transcribe_url(
Raises:
DeepgramError: If the "callback" option is provided for a synchronous transcription.
DeepgramError: If the source type is unknown.
DeepgramApiError: Raised for known API errors.
DeepgramUnknownApiError: Raised for unknown API errors.
DeepgramUnknownError: Raised for unexpected errors not specific to the API.
Expand All @@ -46,14 +49,15 @@ async def transcribe_url(

url = f"{self.url}/{endpoint}"
if options is not None and "callback" in options:
raise DeepgramError("Callback cannot be provided as an option to a synchronous transcription. Use `transcribe_url_callback` instead.")
raise DeepgramError(
"Callback cannot be provided as an option to a synchronous transcription. Use `transcribe_url_callback` instead.")
if is_url_source(source):
body = source
else:
raise DeepgramError("Unknown transcription source type")
return await self.post(url, options, json=body)
async def transcribe_url_callback( self, source: UrlSource, callback:str, options: PrerecordedOptions = None, endpoint: str="v1/listen") -> AsyncPrerecordedResponse:

async def transcribe_url_callback(self, source: UrlSource, callback: str, options: PrerecordedOptions = None, endpoint: str = "v1/listen") -> AsyncPrerecordedResponse:
"""
Transcribes audio from a URL source and sends the result to a callback URL.
Expand All @@ -67,7 +71,6 @@ async def transcribe_url_callback( self, source: UrlSource, callback:str, option
AsyncPrerecordedResponse: An object containing the request_id or an error message.
Raises:
DeepgramError: If the "callback" option is provided for a synchronous transcription.
DeepgramApiError: Raised for known API errors.
DeepgramUnknownApiError: Raised for unknown API errors.
DeepgramUnknownError: Raised for unexpected errors not specific to the API.
Expand All @@ -83,8 +86,7 @@ async def transcribe_url_callback( self, source: UrlSource, callback:str, option
raise DeepgramError("Unknown transcription source type")
return await self.post(url, options, json=body)


async def transcribe_file(self, source: FileSource, options: PrerecordedOptions=None, endpoint: str = "v1/listen") -> SyncPrerecordedResponse:
async def transcribe_file(self, source: FileSource, options: PrerecordedOptions = None, endpoint: str = "v1/listen") -> SyncPrerecordedResponse:
"""
Transcribes audio from a local file source.
Expand All @@ -98,13 +100,14 @@ async def transcribe_file(self, source: FileSource, options: PrerecordedOptions=
Raises:
DeepgramError: If the "callback" option is provided for a synchronous transcription.
DeepgramError: If the source type is unknown.
DeepgramApiError: Raised for known API errors.
DeepgramUnknownApiError: Raised for unknown API errors.
DeepgramUnknownError: Raised for unexpected errors not specific to the API.
Exception: For any other unexpected exceptions.
"""

url = f"{self.url}/{endpoint}"
url = f"{self.url}/{endpoint}"
await self._set_file_mimetype_headers(source)
if is_buffer_source(source):
body = source["buffer"]
Expand All @@ -114,7 +117,7 @@ async def transcribe_file(self, source: FileSource, options: PrerecordedOptions=
raise DeepgramError("Unknown transcription source type")
return await self.post(url, options, content=body)

async def transcribe_file_callback(self, source: FileSource, callback:str, options: PrerecordedOptions = None, endpoint: str="v1/listen") -> AsyncPrerecordedResponse:
async def transcribe_file_callback(self, source: FileSource, callback: str, options: PrerecordedOptions = None, endpoint: str = "v1/listen") -> AsyncPrerecordedResponse:
"""
Transcribes audio from a local file source and sends the result to a callback URL.
Expand All @@ -128,7 +131,7 @@ async def transcribe_file_callback(self, source: FileSource, callback:str, optio
AsyncPrerecordedResponse: An object containing the request_id or an error message.
Raises:
DeepgramError: If the "callback" option is provided for a synchronous transcription.
DeepgramError: If the source type is unknown.
DeepgramApiError: Raised for known API errors.
DeepgramUnknownApiError: Raised for unknown API errors.
DeepgramUnknownError: Raised for unexpected errors not specific to the API.
Expand All @@ -150,5 +153,6 @@ async def transcribe_file_callback(self, source: FileSource, callback:str, optio

async def _set_file_mimetype_headers(self, source: FileSource) -> None:
if "mimetype" not in source or not source["mimetype"]:
raise DeepgramError("Mimetype must be provided if the source is a Buffer or a Readable")
self.headers["Content-Type"] = source["mimetype"]
raise DeepgramError(
"Mimetype must be provided if the source is a Buffer or a Readable")
self.headers["Content-Type"] = source["mimetype"]
File renamed without changes.
31 changes: 16 additions & 15 deletions examples/demo_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@
API_KEY = os.getenv('DG_API_KEY_MANAGE')
PROJECT_ID = os.getenv('DG_PROJECT_ID')
UPDATE_PROJECT_OPTIONS = {
"name": "sandras_project",
"company": "deepgram"
}
"name": "example_project_name",
"company": "deepgram"
}
CREATE_KEY_OPTIONS = {
"comment": "this is a comment", #requred
"scopes": ["keys:read", "owners:read"],
"tags": ["my_name", "sdk_test"],
"testkey": "test value",
"time_to_live_in_seconds": 60000,
}
"comment": "this is a comment", # requred
"scopes": ["keys:read", "owners:read"],
"tags": ["my_name", "sdk_test"],
"testkey": "test value",
"time_to_live_in_seconds": 60000,
}

# Create a Deepgram client using the API key
deepgram: DeepgramClient = create_client(API_KEY)


async def main():
response = await deepgram.manage.get_projects()
# response = await deepgram.manage.get_project(PROJECT_ID)
# response = await deepgram.manage.update_project(PROJECT_ID, UPDATE_PROJECT_OPTIONS)
# response = await deepgram.manage.create_project_key(PROJECT_ID, CREATE_KEY_OPTIONS)
response = await deepgram.manage.get_projects()
# response = await deepgram.manage.get_project(PROJECT_ID)
# response = await deepgram.manage.update_project(PROJECT_ID, UPDATE_PROJECT_OPTIONS)
# response = await deepgram.manage.create_project_key(PROJECT_ID, CREATE_KEY_OPTIONS)

print(response)
print(response)

if __name__ == "__main__":
asyncio.run(main())
asyncio.run(main())

0 comments on commit 33a26b9

Please sign in to comment.