Skip to content

Commit

Permalink
Merge pull request #337 from dvonthenen/implement-tts
Browse files Browse the repository at this point in the history
Implement TTS
  • Loading branch information
davidvonthenen authored Mar 11, 2024
2 parents 745aebe + 4a2b22d commit c44eb48
Show file tree
Hide file tree
Showing 33 changed files with 1,000 additions and 45 deletions.
6 changes: 6 additions & 0 deletions deepgram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
)

# read
from .client import ReadClient, AsyncReadClient
from .client import AnalyzeClient, AsyncAnalyzeClient
from .client import (
AnalyzeSource,
Expand All @@ -61,6 +62,11 @@
SyncAnalyzeResponse,
)

# speak
from .client import SpeakClient, AsyncSpeakClient
from .client import SpeakSource, TextSource, SpeakStreamSource, SpeakOptions
from .client import SpeakResponse

# manage
from .client import ManageClient, AsyncManageClient
from .client import (
Expand Down
27 changes: 26 additions & 1 deletion deepgram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
SyncPrerecordedResponse,
)

# analyze
# read
from .clients import ReadClient, AsyncReadClient
from .clients import AnalyzeClient, AsyncAnalyzeClient
from .clients import (
AnalyzeSource,
Expand All @@ -66,6 +67,14 @@
SyncAnalyzeResponse,
)

# speak client classes/input
from .clients import SpeakClient, AsyncSpeakClient
from .clients import SpeakOptions
from .clients import SpeakSource, TextSource, SpeakStreamSource

# speak client responses
from .clients import SpeakResponse

# manage client classes/input
from .clients import ManageClient, AsyncManageClient
from .clients import (
Expand Down Expand Up @@ -184,6 +193,14 @@ def listen(self):
def read(self):
return Read(self.config)

@property
def speak(self):
return self.Version(self.config, "speak")

@property
def asyncspeak(self):
return self.Version(self.config, "asyncspeak")

@property
def manage(self):
return self.Version(self.config, "manage")
Expand Down Expand Up @@ -241,6 +258,14 @@ def v(self, version: str = ""):
parent = "manage"
fileName = "async_client"
className = "AsyncManageClient"
case "speak":
parent = "speak"
fileName = "client"
className = "SpeakClient"
case "asyncspeak":
parent = "speak"
fileName = "async_client"
className = "AsyncSpeakClient"
case "onprem":
parent = "onprem"
fileName = "client"
Expand Down
13 changes: 12 additions & 1 deletion deepgram/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
SyncPrerecordedResponse,
)

# analyze
# read
from .analyze import ReadClient, AsyncReadClient
from .analyze import AnalyzeClient, AsyncAnalyzeClient
from .analyze import AnalyzeOptions
from .analyze import Sentiment
Expand All @@ -54,6 +55,16 @@
SyncAnalyzeResponse,
)

# speak
from .speak import SpeakClient, AsyncSpeakClient
from .speak import SpeakOptions
from .speak import (
SpeakSource,
TextSource,
SpeakStreamSource,
)
from .speak import SpeakResponse

# manage
from .manage import ManageClient, AsyncManageClient
from .manage import (
Expand Down
68 changes: 55 additions & 13 deletions deepgram/clients/abstract_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import httpx
import json
from typing import Dict, Optional
import io
from typing import Dict, Optional, List

from .helpers import append_query_params
from ..options import DeepgramClientOptions
Expand Down Expand Up @@ -41,7 +42,7 @@ async def get(
options: Optional[Dict] = None,
addons: Optional[Dict] = None,
timeout: Optional[httpx.Timeout] = None,
**kwargs
**kwargs,
) -> str:
return await self._handle_request(
"GET",
Expand All @@ -50,7 +51,27 @@ async def get(
addons=addons,
timeout=timeout,
headers=self.config.headers,
**kwargs
**kwargs,
)

async def post_file(
self,
url: str,
options: Optional[Dict] = None,
addons: Optional[Dict] = None,
timeout: Optional[httpx.Timeout] = None,
file_result: Optional[List] = None,
**kwargs,
) -> Dict:
return await self._handle_request(
"POST",
url,
file_result=file_result,
params=options,
addons=addons,
timeout=timeout,
headers=self.config.headers,
**kwargs,
)

async def post(
Expand All @@ -59,7 +80,7 @@ async def post(
options: Optional[Dict] = None,
addons: Optional[Dict] = None,
timeout: Optional[httpx.Timeout] = None,
**kwargs
**kwargs,
) -> str:
return await self._handle_request(
"POST",
Expand All @@ -68,7 +89,7 @@ async def post(
addons=addons,
timeout=timeout,
headers=self.config.headers,
**kwargs
**kwargs,
)

async def put(
Expand All @@ -77,7 +98,7 @@ async def put(
options: Optional[Dict] = None,
addons: Optional[Dict] = None,
timeout: Optional[httpx.Timeout] = None,
**kwargs
**kwargs,
) -> str:
return await self._handle_request(
"PUT",
Expand All @@ -86,7 +107,7 @@ async def put(
addons=addons,
timeout=timeout,
headers=self.config.headers,
**kwargs
**kwargs,
)

async def patch(
Expand All @@ -95,7 +116,7 @@ async def patch(
options: Optional[Dict] = None,
addons: Optional[Dict] = None,
timeout: Optional[httpx.Timeout] = None,
**kwargs
**kwargs,
) -> str:
return await self._handle_request(
"PATCH",
Expand All @@ -104,7 +125,7 @@ async def patch(
addons=addons,
timeout=timeout,
headers=self.config.headers,
**kwargs
**kwargs,
)

async def delete(
Expand All @@ -113,7 +134,7 @@ async def delete(
options: Optional[Dict] = None,
addons: Optional[Dict] = None,
timeout: Optional[httpx.Timeout] = None,
**kwargs
**kwargs,
) -> str:
return await self._handle_request(
"DELETE",
Expand All @@ -122,7 +143,7 @@ async def delete(
addons=addons,
timeout=timeout,
headers=self.config.headers,
**kwargs
**kwargs,
)

async def _handle_request(
Expand All @@ -133,8 +154,9 @@ async def _handle_request(
addons: Optional[Dict] = None,
timeout: Optional[httpx.Timeout] = None,
headers: Optional[Dict] = None,
**kwargs
) -> str:
file_result: Optional[List] = None,
**kwargs,
):
new_url = url
if params is not None:
new_url = append_query_params(new_url, params)
Expand All @@ -150,7 +172,27 @@ async def _handle_request(
method, new_url, headers=headers, **kwargs
)
response.raise_for_status()

# handle file response
if file_result is not None:
ret = dict()
for item in file_result:
if item in response.headers:
ret[item] = response.headers[item]
continue
tmpItem = f"dg-{item}"
if tmpItem in response.headers:
ret[item] = response.headers[tmpItem]
continue
tmpItem = f"x-dg-{item}"
if tmpItem in response.headers:
ret[item] = response.headers[tmpItem]
ret["stream"] = io.BytesIO(response.content)
return ret

# standard response
return response.text

except httpx._exceptions.HTTPError as e:
if isinstance(e, httpx.HTTPStatusError):
status_code = e.response.status_code or 500
Expand Down
Loading

0 comments on commit c44eb48

Please sign in to comment.