Skip to content

Commit 4a2b22d

Browse files
Implement TTS
1 parent 745aebe commit 4a2b22d

File tree

33 files changed

+1000
-45
lines changed

33 files changed

+1000
-45
lines changed

deepgram/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
)
4646

4747
# read
48+
from .client import ReadClient, AsyncReadClient
4849
from .client import AnalyzeClient, AsyncAnalyzeClient
4950
from .client import (
5051
AnalyzeSource,
@@ -61,6 +62,11 @@
6162
SyncAnalyzeResponse,
6263
)
6364

65+
# speak
66+
from .client import SpeakClient, AsyncSpeakClient
67+
from .client import SpeakSource, TextSource, SpeakStreamSource, SpeakOptions
68+
from .client import SpeakResponse
69+
6470
# manage
6571
from .client import ManageClient, AsyncManageClient
6672
from .client import (

deepgram/client.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
SyncPrerecordedResponse,
4848
)
4949

50-
# analyze
50+
# read
51+
from .clients import ReadClient, AsyncReadClient
5152
from .clients import AnalyzeClient, AsyncAnalyzeClient
5253
from .clients import (
5354
AnalyzeSource,
@@ -66,6 +67,14 @@
6667
SyncAnalyzeResponse,
6768
)
6869

70+
# speak client classes/input
71+
from .clients import SpeakClient, AsyncSpeakClient
72+
from .clients import SpeakOptions
73+
from .clients import SpeakSource, TextSource, SpeakStreamSource
74+
75+
# speak client responses
76+
from .clients import SpeakResponse
77+
6978
# manage client classes/input
7079
from .clients import ManageClient, AsyncManageClient
7180
from .clients import (
@@ -184,6 +193,14 @@ def listen(self):
184193
def read(self):
185194
return Read(self.config)
186195

196+
@property
197+
def speak(self):
198+
return self.Version(self.config, "speak")
199+
200+
@property
201+
def asyncspeak(self):
202+
return self.Version(self.config, "asyncspeak")
203+
187204
@property
188205
def manage(self):
189206
return self.Version(self.config, "manage")
@@ -241,6 +258,14 @@ def v(self, version: str = ""):
241258
parent = "manage"
242259
fileName = "async_client"
243260
className = "AsyncManageClient"
261+
case "speak":
262+
parent = "speak"
263+
fileName = "client"
264+
className = "SpeakClient"
265+
case "asyncspeak":
266+
parent = "speak"
267+
fileName = "async_client"
268+
className = "AsyncSpeakClient"
244269
case "onprem":
245270
parent = "onprem"
246271
fileName = "client"

deepgram/clients/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
SyncPrerecordedResponse,
3838
)
3939

40-
# analyze
40+
# read
41+
from .analyze import ReadClient, AsyncReadClient
4142
from .analyze import AnalyzeClient, AsyncAnalyzeClient
4243
from .analyze import AnalyzeOptions
4344
from .analyze import Sentiment
@@ -54,6 +55,16 @@
5455
SyncAnalyzeResponse,
5556
)
5657

58+
# speak
59+
from .speak import SpeakClient, AsyncSpeakClient
60+
from .speak import SpeakOptions
61+
from .speak import (
62+
SpeakSource,
63+
TextSource,
64+
SpeakStreamSource,
65+
)
66+
from .speak import SpeakResponse
67+
5768
# manage
5869
from .manage import ManageClient, AsyncManageClient
5970
from .manage import (

deepgram/clients/abstract_async_client.py

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import httpx
66
import json
7-
from typing import Dict, Optional
7+
import io
8+
from typing import Dict, Optional, List
89

910
from .helpers import append_query_params
1011
from ..options import DeepgramClientOptions
@@ -41,7 +42,7 @@ async def get(
4142
options: Optional[Dict] = None,
4243
addons: Optional[Dict] = None,
4344
timeout: Optional[httpx.Timeout] = None,
44-
**kwargs
45+
**kwargs,
4546
) -> str:
4647
return await self._handle_request(
4748
"GET",
@@ -50,7 +51,27 @@ async def get(
5051
addons=addons,
5152
timeout=timeout,
5253
headers=self.config.headers,
53-
**kwargs
54+
**kwargs,
55+
)
56+
57+
async def post_file(
58+
self,
59+
url: str,
60+
options: Optional[Dict] = None,
61+
addons: Optional[Dict] = None,
62+
timeout: Optional[httpx.Timeout] = None,
63+
file_result: Optional[List] = None,
64+
**kwargs,
65+
) -> Dict:
66+
return await self._handle_request(
67+
"POST",
68+
url,
69+
file_result=file_result,
70+
params=options,
71+
addons=addons,
72+
timeout=timeout,
73+
headers=self.config.headers,
74+
**kwargs,
5475
)
5576

5677
async def post(
@@ -59,7 +80,7 @@ async def post(
5980
options: Optional[Dict] = None,
6081
addons: Optional[Dict] = None,
6182
timeout: Optional[httpx.Timeout] = None,
62-
**kwargs
83+
**kwargs,
6384
) -> str:
6485
return await self._handle_request(
6586
"POST",
@@ -68,7 +89,7 @@ async def post(
6889
addons=addons,
6990
timeout=timeout,
7091
headers=self.config.headers,
71-
**kwargs
92+
**kwargs,
7293
)
7394

7495
async def put(
@@ -77,7 +98,7 @@ async def put(
7798
options: Optional[Dict] = None,
7899
addons: Optional[Dict] = None,
79100
timeout: Optional[httpx.Timeout] = None,
80-
**kwargs
101+
**kwargs,
81102
) -> str:
82103
return await self._handle_request(
83104
"PUT",
@@ -86,7 +107,7 @@ async def put(
86107
addons=addons,
87108
timeout=timeout,
88109
headers=self.config.headers,
89-
**kwargs
110+
**kwargs,
90111
)
91112

92113
async def patch(
@@ -95,7 +116,7 @@ async def patch(
95116
options: Optional[Dict] = None,
96117
addons: Optional[Dict] = None,
97118
timeout: Optional[httpx.Timeout] = None,
98-
**kwargs
119+
**kwargs,
99120
) -> str:
100121
return await self._handle_request(
101122
"PATCH",
@@ -104,7 +125,7 @@ async def patch(
104125
addons=addons,
105126
timeout=timeout,
106127
headers=self.config.headers,
107-
**kwargs
128+
**kwargs,
108129
)
109130

110131
async def delete(
@@ -113,7 +134,7 @@ async def delete(
113134
options: Optional[Dict] = None,
114135
addons: Optional[Dict] = None,
115136
timeout: Optional[httpx.Timeout] = None,
116-
**kwargs
137+
**kwargs,
117138
) -> str:
118139
return await self._handle_request(
119140
"DELETE",
@@ -122,7 +143,7 @@ async def delete(
122143
addons=addons,
123144
timeout=timeout,
124145
headers=self.config.headers,
125-
**kwargs
146+
**kwargs,
126147
)
127148

128149
async def _handle_request(
@@ -133,8 +154,9 @@ async def _handle_request(
133154
addons: Optional[Dict] = None,
134155
timeout: Optional[httpx.Timeout] = None,
135156
headers: Optional[Dict] = None,
136-
**kwargs
137-
) -> str:
157+
file_result: Optional[List] = None,
158+
**kwargs,
159+
):
138160
new_url = url
139161
if params is not None:
140162
new_url = append_query_params(new_url, params)
@@ -150,7 +172,27 @@ async def _handle_request(
150172
method, new_url, headers=headers, **kwargs
151173
)
152174
response.raise_for_status()
175+
176+
# handle file response
177+
if file_result is not None:
178+
ret = dict()
179+
for item in file_result:
180+
if item in response.headers:
181+
ret[item] = response.headers[item]
182+
continue
183+
tmpItem = f"dg-{item}"
184+
if tmpItem in response.headers:
185+
ret[item] = response.headers[tmpItem]
186+
continue
187+
tmpItem = f"x-dg-{item}"
188+
if tmpItem in response.headers:
189+
ret[item] = response.headers[tmpItem]
190+
ret["stream"] = io.BytesIO(response.content)
191+
return ret
192+
193+
# standard response
153194
return response.text
195+
154196
except httpx._exceptions.HTTPError as e:
155197
if isinstance(e, httpx.HTTPStatusError):
156198
status_code = e.response.status_code or 500

0 commit comments

Comments
 (0)