deepgram.clients.speak.v1.rest.async_client
1# Copyright 2024 Deepgram SDK contributors. All Rights Reserved. 2# Use of this source code is governed by a MIT license that can be found in the LICENSE file. 3# SPDX-License-Identifier: MIT 4 5import logging 6from typing import Dict, Union, Optional, cast 7import io 8import aiofiles 9 10import httpx 11 12import deprecation # type: ignore 13from ..... import __version__ 14 15from .....utils import verboselogs 16from .....options import DeepgramClientOptions 17from ....common import AbstractAsyncRestClient 18from ....common import DeepgramError, DeepgramTypeError 19 20from .helpers import is_text_source 21from .options import SpeakRESTOptions, FileSource 22from .response import SpeakRESTResponse 23 24 25class AsyncSpeakRESTClient(AbstractAsyncRestClient): 26 """ 27 A client class for doing Text-to-Speech. 28 Provides methods for speaking from text. 29 """ 30 31 _logger: verboselogs.VerboseLogger 32 _config: DeepgramClientOptions 33 34 def __init__(self, config: DeepgramClientOptions): 35 self._logger = verboselogs.VerboseLogger(__name__) 36 self._logger.addHandler(logging.StreamHandler()) 37 self._logger.setLevel(config.verbose) 38 self._config = config 39 super().__init__(config) 40 41 # pylint: disable=too-many-positional-arguments 42 43 async def stream_raw( 44 self, 45 source: FileSource, 46 options: Optional[Union[Dict, SpeakRESTOptions]] = None, 47 addons: Optional[Dict] = None, 48 headers: Optional[Dict] = None, 49 timeout: Optional[httpx.Timeout] = None, 50 endpoint: str = "v1/speak", 51 **kwargs, 52 ) -> httpx.Response: 53 """ 54 Speak from a text source and store as a Iterator[byte]. 55 56 Args: 57 source (TextSource): The text source to speak. 58 options (SpeakRESTOptions): Additional options for the ingest (default is None). 59 addons (Dict): Additional options for the request (default is None). 60 headers (Dict): Additional headers for the request (default is None). 61 timeout (httpx.Timeout): The timeout for the request (default is None). 62 endpoint (str): The endpoint to use for the request (default is "v1/speak"). 63 64 Returns: 65 httpx.Response: The direct httpx.Response object from the speak request. 66 For more information, see https://www.python-httpx.org/api/#response 67 68 IMPORTANT: The response object's `close()` method should be called when done 69 in order to prevent connection leaks. 70 71 Raises: 72 DeepgramTypeError: Raised for known API errors. 73 """ 74 self._logger.debug("AsyncSpeakClient.stream ENTER") 75 76 url = f"{self._config.url}/{endpoint}" 77 if is_text_source(source): 78 body = source 79 else: 80 self._logger.error("Unknown speak source type") 81 self._logger.debug("AsyncSpeakClient.stream LEAVE") 82 raise DeepgramTypeError("Unknown speak source type") 83 84 if isinstance(options, SpeakRESTOptions) and not options.check(): 85 self._logger.error("options.check failed") 86 self._logger.debug("AsyncSpeakClient.stream LEAVE") 87 raise DeepgramError("Fatal speak options error") 88 89 self._logger.info("url: %s", url) 90 self._logger.info("source: %s", source) 91 if isinstance(options, SpeakRESTOptions): 92 self._logger.info("SpeakRESTOptions switching class -> dict") 93 options = options.to_dict() 94 self._logger.info("options: %s", options) 95 self._logger.info("addons: %s", addons) 96 self._logger.info("headers: %s", headers) 97 98 result = await self.post_raw( 99 url, 100 options=options, 101 addons=addons, 102 headers=headers, 103 json=body, 104 timeout=timeout, 105 **kwargs, 106 ) 107 108 self._logger.info("result: %s", str(result)) 109 self._logger.notice("speak succeeded") 110 self._logger.debug("AsyncSpeakClient.stream LEAVE") 111 return result 112 113 async def stream_memory( 114 self, 115 source: FileSource, 116 options: Optional[Union[Dict, SpeakRESTOptions]] = None, 117 addons: Optional[Dict] = None, 118 headers: Optional[Dict] = None, 119 timeout: Optional[httpx.Timeout] = None, 120 endpoint: str = "v1/speak", 121 **kwargs, 122 ) -> SpeakRESTResponse: 123 """ 124 Speak from a text source and store in memory. 125 126 Args: 127 source (TextSource): The text source to speak. 128 options (SpeakRESTOptions): Additional options for the ingest (default is None). 129 addons (Dict): Additional options for the request (default is None). 130 headers (Dict): Additional headers for the request (default is None). 131 timeout (httpx.Timeout): The timeout for the request (default is None). 132 endpoint (str): The endpoint to use for the request (default is "v1/speak"). 133 134 Returns: 135 SpeakRESTResponse: The response from the speak request. 136 137 Raises: 138 DeepgramTypeError: Raised for known API errors. 139 """ 140 self._logger.debug("AsyncSpeakClient.stream ENTER") 141 142 url = f"{self._config.url}/{endpoint}" 143 if is_text_source(source): 144 body = source 145 else: 146 self._logger.error("Unknown speak source type") 147 self._logger.debug("AsyncSpeakClient.stream LEAVE") 148 raise DeepgramTypeError("Unknown speak source type") 149 150 if isinstance(options, SpeakRESTOptions) and not options.check(): 151 self._logger.error("options.check failed") 152 self._logger.debug("AsyncSpeakClient.stream LEAVE") 153 raise DeepgramError("Fatal speak options error") 154 155 self._logger.info("url: %s", url) 156 self._logger.info("source: %s", source) 157 if isinstance(options, SpeakRESTOptions): 158 self._logger.info("SpeakRESTOptions switching class -> dict") 159 options = options.to_dict() 160 self._logger.info("options: %s", options) 161 self._logger.info("addons: %s", addons) 162 self._logger.info("headers: %s", headers) 163 164 return_vals = [ 165 "content-type", 166 "request-id", 167 "model-uuid", 168 "model-name", 169 "char-count", 170 "transfer-encoding", 171 "date", 172 ] 173 result = await self.post_memory( 174 url, 175 options=options, 176 addons=addons, 177 headers=headers, 178 json=body, 179 timeout=timeout, 180 file_result=return_vals, 181 **kwargs, 182 ) 183 self._logger.info("result: %s", result) 184 resp = SpeakRESTResponse( 185 content_type=str(result["content-type"]), 186 request_id=str(result["request-id"]), 187 model_uuid=str(result["model-uuid"]), 188 model_name=str(result["model-name"]), 189 characters=int(str(result["char-count"])), 190 transfer_encoding=str(result["transfer-encoding"]), 191 date=str(result["date"]), 192 stream=cast(io.BytesIO, result["stream"]), 193 stream_memory=cast(io.BytesIO, result["stream"]), 194 ) 195 self._logger.verbose("resp Object: %s", str(resp)) 196 self._logger.notice("speak succeeded") 197 self._logger.debug("AsyncSpeakClient.stream LEAVE") 198 return resp 199 200 @deprecation.deprecated( 201 deprecated_in="3.4.0", 202 removed_in="4.0.0", 203 current_version=__version__, 204 details="SpeakRESTClient.stream is deprecated. Use SpeakRESTClient.stream_memory instead.", 205 ) 206 async def stream( 207 self, 208 source: FileSource, 209 options: Optional[Union[Dict, SpeakRESTOptions]] = None, 210 addons: Optional[Dict] = None, 211 headers: Optional[Dict] = None, 212 timeout: Optional[httpx.Timeout] = None, 213 endpoint: str = "v1/speak", 214 **kwargs, 215 ) -> SpeakRESTResponse: 216 """ 217 DEPRECATED: stream() is deprecated. Use stream_memory() instead. 218 """ 219 return await self.stream_memory( 220 source, 221 options=options, 222 addons=addons, 223 headers=headers, 224 timeout=timeout, 225 endpoint=endpoint, 226 **kwargs, 227 ) 228 229 async def file( 230 self, 231 filename: str, 232 source: FileSource, 233 options: Optional[Union[Dict, SpeakRESTOptions]] = None, 234 addons: Optional[Dict] = None, 235 timeout: Optional[httpx.Timeout] = None, 236 endpoint: str = "v1/speak", 237 **kwargs, 238 ) -> SpeakRESTResponse: 239 """ 240 Speak from a text source and save to a file. 241 """ 242 return await self.save( 243 filename, 244 source, 245 options=options, 246 addons=addons, 247 timeout=timeout, 248 endpoint=endpoint, 249 **kwargs, 250 ) 251 252 async def save( 253 self, 254 filename: str, 255 source: FileSource, 256 options: Optional[Union[Dict, SpeakRESTOptions]] = None, 257 addons: Optional[Dict] = None, 258 headers: Optional[Dict] = None, 259 timeout: Optional[httpx.Timeout] = None, 260 endpoint: str = "v1/speak", 261 **kwargs, 262 ) -> SpeakRESTResponse: 263 """ 264 Speak from a text source and save to a file. 265 266 Args: 267 source (TextSource): The text source to speak. 268 options (SpeakRESTOptions): Additional options for the ingest (default is None). 269 addons (Dict): Additional options for the request (default is None). 270 headers (Dict): Additional headers for the request (default is None). 271 timeout (httpx.Timeout): The timeout for the request (default is None). 272 endpoint (str): The endpoint to use for the request (default is "v1/speak"). 273 274 Returns: 275 SpeakRESTResponse: The response from the speak request. 276 277 Raises: 278 DeepgramTypeError: Raised for known API errors. 279 """ 280 self._logger.debug("AsyncSpeakClient.save ENTER") 281 282 res = await self.stream_memory( 283 source, 284 options=options, 285 addons=addons, 286 headers=headers, 287 timeout=timeout, 288 endpoint=endpoint, 289 **kwargs, 290 ) 291 292 if res.stream is None: 293 self._logger.error("stream is None") 294 self._logger.debug("AsyncSpeakClient.save LEAVE") 295 raise DeepgramError("BytesIO stream is None") 296 297 # save to file 298 async with aiofiles.open(filename, "wb") as out: 299 await out.write(res.stream.getbuffer()) 300 await out.flush() 301 302 # add filename to response 303 res.stream = None 304 res.filename = filename 305 306 self._logger.debug("AsyncSpeakClient.save LEAVE") 307 return res 308 309 # pylint: enable=too-many-positional-arguments
26class AsyncSpeakRESTClient(AbstractAsyncRestClient): 27 """ 28 A client class for doing Text-to-Speech. 29 Provides methods for speaking from text. 30 """ 31 32 _logger: verboselogs.VerboseLogger 33 _config: DeepgramClientOptions 34 35 def __init__(self, config: DeepgramClientOptions): 36 self._logger = verboselogs.VerboseLogger(__name__) 37 self._logger.addHandler(logging.StreamHandler()) 38 self._logger.setLevel(config.verbose) 39 self._config = config 40 super().__init__(config) 41 42 # pylint: disable=too-many-positional-arguments 43 44 async def stream_raw( 45 self, 46 source: FileSource, 47 options: Optional[Union[Dict, SpeakRESTOptions]] = None, 48 addons: Optional[Dict] = None, 49 headers: Optional[Dict] = None, 50 timeout: Optional[httpx.Timeout] = None, 51 endpoint: str = "v1/speak", 52 **kwargs, 53 ) -> httpx.Response: 54 """ 55 Speak from a text source and store as a Iterator[byte]. 56 57 Args: 58 source (TextSource): The text source to speak. 59 options (SpeakRESTOptions): Additional options for the ingest (default is None). 60 addons (Dict): Additional options for the request (default is None). 61 headers (Dict): Additional headers for the request (default is None). 62 timeout (httpx.Timeout): The timeout for the request (default is None). 63 endpoint (str): The endpoint to use for the request (default is "v1/speak"). 64 65 Returns: 66 httpx.Response: The direct httpx.Response object from the speak request. 67 For more information, see https://www.python-httpx.org/api/#response 68 69 IMPORTANT: The response object's `close()` method should be called when done 70 in order to prevent connection leaks. 71 72 Raises: 73 DeepgramTypeError: Raised for known API errors. 74 """ 75 self._logger.debug("AsyncSpeakClient.stream ENTER") 76 77 url = f"{self._config.url}/{endpoint}" 78 if is_text_source(source): 79 body = source 80 else: 81 self._logger.error("Unknown speak source type") 82 self._logger.debug("AsyncSpeakClient.stream LEAVE") 83 raise DeepgramTypeError("Unknown speak source type") 84 85 if isinstance(options, SpeakRESTOptions) and not options.check(): 86 self._logger.error("options.check failed") 87 self._logger.debug("AsyncSpeakClient.stream LEAVE") 88 raise DeepgramError("Fatal speak options error") 89 90 self._logger.info("url: %s", url) 91 self._logger.info("source: %s", source) 92 if isinstance(options, SpeakRESTOptions): 93 self._logger.info("SpeakRESTOptions switching class -> dict") 94 options = options.to_dict() 95 self._logger.info("options: %s", options) 96 self._logger.info("addons: %s", addons) 97 self._logger.info("headers: %s", headers) 98 99 result = await self.post_raw( 100 url, 101 options=options, 102 addons=addons, 103 headers=headers, 104 json=body, 105 timeout=timeout, 106 **kwargs, 107 ) 108 109 self._logger.info("result: %s", str(result)) 110 self._logger.notice("speak succeeded") 111 self._logger.debug("AsyncSpeakClient.stream LEAVE") 112 return result 113 114 async def stream_memory( 115 self, 116 source: FileSource, 117 options: Optional[Union[Dict, SpeakRESTOptions]] = None, 118 addons: Optional[Dict] = None, 119 headers: Optional[Dict] = None, 120 timeout: Optional[httpx.Timeout] = None, 121 endpoint: str = "v1/speak", 122 **kwargs, 123 ) -> SpeakRESTResponse: 124 """ 125 Speak from a text source and store in memory. 126 127 Args: 128 source (TextSource): The text source to speak. 129 options (SpeakRESTOptions): Additional options for the ingest (default is None). 130 addons (Dict): Additional options for the request (default is None). 131 headers (Dict): Additional headers for the request (default is None). 132 timeout (httpx.Timeout): The timeout for the request (default is None). 133 endpoint (str): The endpoint to use for the request (default is "v1/speak"). 134 135 Returns: 136 SpeakRESTResponse: The response from the speak request. 137 138 Raises: 139 DeepgramTypeError: Raised for known API errors. 140 """ 141 self._logger.debug("AsyncSpeakClient.stream ENTER") 142 143 url = f"{self._config.url}/{endpoint}" 144 if is_text_source(source): 145 body = source 146 else: 147 self._logger.error("Unknown speak source type") 148 self._logger.debug("AsyncSpeakClient.stream LEAVE") 149 raise DeepgramTypeError("Unknown speak source type") 150 151 if isinstance(options, SpeakRESTOptions) and not options.check(): 152 self._logger.error("options.check failed") 153 self._logger.debug("AsyncSpeakClient.stream LEAVE") 154 raise DeepgramError("Fatal speak options error") 155 156 self._logger.info("url: %s", url) 157 self._logger.info("source: %s", source) 158 if isinstance(options, SpeakRESTOptions): 159 self._logger.info("SpeakRESTOptions switching class -> dict") 160 options = options.to_dict() 161 self._logger.info("options: %s", options) 162 self._logger.info("addons: %s", addons) 163 self._logger.info("headers: %s", headers) 164 165 return_vals = [ 166 "content-type", 167 "request-id", 168 "model-uuid", 169 "model-name", 170 "char-count", 171 "transfer-encoding", 172 "date", 173 ] 174 result = await self.post_memory( 175 url, 176 options=options, 177 addons=addons, 178 headers=headers, 179 json=body, 180 timeout=timeout, 181 file_result=return_vals, 182 **kwargs, 183 ) 184 self._logger.info("result: %s", result) 185 resp = SpeakRESTResponse( 186 content_type=str(result["content-type"]), 187 request_id=str(result["request-id"]), 188 model_uuid=str(result["model-uuid"]), 189 model_name=str(result["model-name"]), 190 characters=int(str(result["char-count"])), 191 transfer_encoding=str(result["transfer-encoding"]), 192 date=str(result["date"]), 193 stream=cast(io.BytesIO, result["stream"]), 194 stream_memory=cast(io.BytesIO, result["stream"]), 195 ) 196 self._logger.verbose("resp Object: %s", str(resp)) 197 self._logger.notice("speak succeeded") 198 self._logger.debug("AsyncSpeakClient.stream LEAVE") 199 return resp 200 201 @deprecation.deprecated( 202 deprecated_in="3.4.0", 203 removed_in="4.0.0", 204 current_version=__version__, 205 details="SpeakRESTClient.stream is deprecated. Use SpeakRESTClient.stream_memory instead.", 206 ) 207 async def stream( 208 self, 209 source: FileSource, 210 options: Optional[Union[Dict, SpeakRESTOptions]] = None, 211 addons: Optional[Dict] = None, 212 headers: Optional[Dict] = None, 213 timeout: Optional[httpx.Timeout] = None, 214 endpoint: str = "v1/speak", 215 **kwargs, 216 ) -> SpeakRESTResponse: 217 """ 218 DEPRECATED: stream() is deprecated. Use stream_memory() instead. 219 """ 220 return await self.stream_memory( 221 source, 222 options=options, 223 addons=addons, 224 headers=headers, 225 timeout=timeout, 226 endpoint=endpoint, 227 **kwargs, 228 ) 229 230 async def file( 231 self, 232 filename: str, 233 source: FileSource, 234 options: Optional[Union[Dict, SpeakRESTOptions]] = None, 235 addons: Optional[Dict] = None, 236 timeout: Optional[httpx.Timeout] = None, 237 endpoint: str = "v1/speak", 238 **kwargs, 239 ) -> SpeakRESTResponse: 240 """ 241 Speak from a text source and save to a file. 242 """ 243 return await self.save( 244 filename, 245 source, 246 options=options, 247 addons=addons, 248 timeout=timeout, 249 endpoint=endpoint, 250 **kwargs, 251 ) 252 253 async def save( 254 self, 255 filename: str, 256 source: FileSource, 257 options: Optional[Union[Dict, SpeakRESTOptions]] = None, 258 addons: Optional[Dict] = None, 259 headers: Optional[Dict] = None, 260 timeout: Optional[httpx.Timeout] = None, 261 endpoint: str = "v1/speak", 262 **kwargs, 263 ) -> SpeakRESTResponse: 264 """ 265 Speak from a text source and save to a file. 266 267 Args: 268 source (TextSource): The text source to speak. 269 options (SpeakRESTOptions): Additional options for the ingest (default is None). 270 addons (Dict): Additional options for the request (default is None). 271 headers (Dict): Additional headers for the request (default is None). 272 timeout (httpx.Timeout): The timeout for the request (default is None). 273 endpoint (str): The endpoint to use for the request (default is "v1/speak"). 274 275 Returns: 276 SpeakRESTResponse: The response from the speak request. 277 278 Raises: 279 DeepgramTypeError: Raised for known API errors. 280 """ 281 self._logger.debug("AsyncSpeakClient.save ENTER") 282 283 res = await self.stream_memory( 284 source, 285 options=options, 286 addons=addons, 287 headers=headers, 288 timeout=timeout, 289 endpoint=endpoint, 290 **kwargs, 291 ) 292 293 if res.stream is None: 294 self._logger.error("stream is None") 295 self._logger.debug("AsyncSpeakClient.save LEAVE") 296 raise DeepgramError("BytesIO stream is None") 297 298 # save to file 299 async with aiofiles.open(filename, "wb") as out: 300 await out.write(res.stream.getbuffer()) 301 await out.flush() 302 303 # add filename to response 304 res.stream = None 305 res.filename = filename 306 307 self._logger.debug("AsyncSpeakClient.save LEAVE") 308 return res 309 310 # pylint: enable=too-many-positional-arguments
A client class for doing Text-to-Speech. Provides methods for speaking from text.
44 async def stream_raw( 45 self, 46 source: FileSource, 47 options: Optional[Union[Dict, SpeakRESTOptions]] = None, 48 addons: Optional[Dict] = None, 49 headers: Optional[Dict] = None, 50 timeout: Optional[httpx.Timeout] = None, 51 endpoint: str = "v1/speak", 52 **kwargs, 53 ) -> httpx.Response: 54 """ 55 Speak from a text source and store as a Iterator[byte]. 56 57 Args: 58 source (TextSource): The text source to speak. 59 options (SpeakRESTOptions): Additional options for the ingest (default is None). 60 addons (Dict): Additional options for the request (default is None). 61 headers (Dict): Additional headers for the request (default is None). 62 timeout (httpx.Timeout): The timeout for the request (default is None). 63 endpoint (str): The endpoint to use for the request (default is "v1/speak"). 64 65 Returns: 66 httpx.Response: The direct httpx.Response object from the speak request. 67 For more information, see https://www.python-httpx.org/api/#response 68 69 IMPORTANT: The response object's `close()` method should be called when done 70 in order to prevent connection leaks. 71 72 Raises: 73 DeepgramTypeError: Raised for known API errors. 74 """ 75 self._logger.debug("AsyncSpeakClient.stream ENTER") 76 77 url = f"{self._config.url}/{endpoint}" 78 if is_text_source(source): 79 body = source 80 else: 81 self._logger.error("Unknown speak source type") 82 self._logger.debug("AsyncSpeakClient.stream LEAVE") 83 raise DeepgramTypeError("Unknown speak source type") 84 85 if isinstance(options, SpeakRESTOptions) and not options.check(): 86 self._logger.error("options.check failed") 87 self._logger.debug("AsyncSpeakClient.stream LEAVE") 88 raise DeepgramError("Fatal speak options error") 89 90 self._logger.info("url: %s", url) 91 self._logger.info("source: %s", source) 92 if isinstance(options, SpeakRESTOptions): 93 self._logger.info("SpeakRESTOptions switching class -> dict") 94 options = options.to_dict() 95 self._logger.info("options: %s", options) 96 self._logger.info("addons: %s", addons) 97 self._logger.info("headers: %s", headers) 98 99 result = await self.post_raw( 100 url, 101 options=options, 102 addons=addons, 103 headers=headers, 104 json=body, 105 timeout=timeout, 106 **kwargs, 107 ) 108 109 self._logger.info("result: %s", str(result)) 110 self._logger.notice("speak succeeded") 111 self._logger.debug("AsyncSpeakClient.stream LEAVE") 112 return result
Speak from a text source and store as a Iterator[byte].
Args: source (TextSource): The text source to speak. options (SpeakRESTOptions): Additional options for the ingest (default is None). addons (Dict): Additional options for the request (default is None). headers (Dict): Additional headers for the request (default is None). timeout (httpx.Timeout): The timeout for the request (default is None). endpoint (str): The endpoint to use for the request (default is "v1/speak").
Returns: httpx.Response: The direct httpx.Response object from the speak request. For more information, see https://www.python-httpx.org/api/#response
IMPORTANT: The response object's `close()` method should be called when done
in order to prevent connection leaks.
Raises: DeepgramTypeError: Raised for known API errors.
114 async def stream_memory( 115 self, 116 source: FileSource, 117 options: Optional[Union[Dict, SpeakRESTOptions]] = None, 118 addons: Optional[Dict] = None, 119 headers: Optional[Dict] = None, 120 timeout: Optional[httpx.Timeout] = None, 121 endpoint: str = "v1/speak", 122 **kwargs, 123 ) -> SpeakRESTResponse: 124 """ 125 Speak from a text source and store in memory. 126 127 Args: 128 source (TextSource): The text source to speak. 129 options (SpeakRESTOptions): Additional options for the ingest (default is None). 130 addons (Dict): Additional options for the request (default is None). 131 headers (Dict): Additional headers for the request (default is None). 132 timeout (httpx.Timeout): The timeout for the request (default is None). 133 endpoint (str): The endpoint to use for the request (default is "v1/speak"). 134 135 Returns: 136 SpeakRESTResponse: The response from the speak request. 137 138 Raises: 139 DeepgramTypeError: Raised for known API errors. 140 """ 141 self._logger.debug("AsyncSpeakClient.stream ENTER") 142 143 url = f"{self._config.url}/{endpoint}" 144 if is_text_source(source): 145 body = source 146 else: 147 self._logger.error("Unknown speak source type") 148 self._logger.debug("AsyncSpeakClient.stream LEAVE") 149 raise DeepgramTypeError("Unknown speak source type") 150 151 if isinstance(options, SpeakRESTOptions) and not options.check(): 152 self._logger.error("options.check failed") 153 self._logger.debug("AsyncSpeakClient.stream LEAVE") 154 raise DeepgramError("Fatal speak options error") 155 156 self._logger.info("url: %s", url) 157 self._logger.info("source: %s", source) 158 if isinstance(options, SpeakRESTOptions): 159 self._logger.info("SpeakRESTOptions switching class -> dict") 160 options = options.to_dict() 161 self._logger.info("options: %s", options) 162 self._logger.info("addons: %s", addons) 163 self._logger.info("headers: %s", headers) 164 165 return_vals = [ 166 "content-type", 167 "request-id", 168 "model-uuid", 169 "model-name", 170 "char-count", 171 "transfer-encoding", 172 "date", 173 ] 174 result = await self.post_memory( 175 url, 176 options=options, 177 addons=addons, 178 headers=headers, 179 json=body, 180 timeout=timeout, 181 file_result=return_vals, 182 **kwargs, 183 ) 184 self._logger.info("result: %s", result) 185 resp = SpeakRESTResponse( 186 content_type=str(result["content-type"]), 187 request_id=str(result["request-id"]), 188 model_uuid=str(result["model-uuid"]), 189 model_name=str(result["model-name"]), 190 characters=int(str(result["char-count"])), 191 transfer_encoding=str(result["transfer-encoding"]), 192 date=str(result["date"]), 193 stream=cast(io.BytesIO, result["stream"]), 194 stream_memory=cast(io.BytesIO, result["stream"]), 195 ) 196 self._logger.verbose("resp Object: %s", str(resp)) 197 self._logger.notice("speak succeeded") 198 self._logger.debug("AsyncSpeakClient.stream LEAVE") 199 return resp
Speak from a text source and store in memory.
Args: source (TextSource): The text source to speak. options (SpeakRESTOptions): Additional options for the ingest (default is None). addons (Dict): Additional options for the request (default is None). headers (Dict): Additional headers for the request (default is None). timeout (httpx.Timeout): The timeout for the request (default is None). endpoint (str): The endpoint to use for the request (default is "v1/speak").
Returns: SpeakRESTResponse: The response from the speak request.
Raises: DeepgramTypeError: Raised for known API errors.
201 @deprecation.deprecated( 202 deprecated_in="3.4.0", 203 removed_in="4.0.0", 204 current_version=__version__, 205 details="SpeakRESTClient.stream is deprecated. Use SpeakRESTClient.stream_memory instead.", 206 ) 207 async def stream( 208 self, 209 source: FileSource, 210 options: Optional[Union[Dict, SpeakRESTOptions]] = None, 211 addons: Optional[Dict] = None, 212 headers: Optional[Dict] = None, 213 timeout: Optional[httpx.Timeout] = None, 214 endpoint: str = "v1/speak", 215 **kwargs, 216 ) -> SpeakRESTResponse: 217 """ 218 DEPRECATED: stream() is deprecated. Use stream_memory() instead. 219 """ 220 return await self.stream_memory( 221 source, 222 options=options, 223 addons=addons, 224 headers=headers, 225 timeout=timeout, 226 endpoint=endpoint, 227 **kwargs, 228 )
DEPRECATED: stream() is deprecated. Use stream_memory() instead.
230 async def file( 231 self, 232 filename: str, 233 source: FileSource, 234 options: Optional[Union[Dict, SpeakRESTOptions]] = None, 235 addons: Optional[Dict] = None, 236 timeout: Optional[httpx.Timeout] = None, 237 endpoint: str = "v1/speak", 238 **kwargs, 239 ) -> SpeakRESTResponse: 240 """ 241 Speak from a text source and save to a file. 242 """ 243 return await self.save( 244 filename, 245 source, 246 options=options, 247 addons=addons, 248 timeout=timeout, 249 endpoint=endpoint, 250 **kwargs, 251 )
Speak from a text source and save to a file.
253 async def save( 254 self, 255 filename: str, 256 source: FileSource, 257 options: Optional[Union[Dict, SpeakRESTOptions]] = None, 258 addons: Optional[Dict] = None, 259 headers: Optional[Dict] = None, 260 timeout: Optional[httpx.Timeout] = None, 261 endpoint: str = "v1/speak", 262 **kwargs, 263 ) -> SpeakRESTResponse: 264 """ 265 Speak from a text source and save to a file. 266 267 Args: 268 source (TextSource): The text source to speak. 269 options (SpeakRESTOptions): Additional options for the ingest (default is None). 270 addons (Dict): Additional options for the request (default is None). 271 headers (Dict): Additional headers for the request (default is None). 272 timeout (httpx.Timeout): The timeout for the request (default is None). 273 endpoint (str): The endpoint to use for the request (default is "v1/speak"). 274 275 Returns: 276 SpeakRESTResponse: The response from the speak request. 277 278 Raises: 279 DeepgramTypeError: Raised for known API errors. 280 """ 281 self._logger.debug("AsyncSpeakClient.save ENTER") 282 283 res = await self.stream_memory( 284 source, 285 options=options, 286 addons=addons, 287 headers=headers, 288 timeout=timeout, 289 endpoint=endpoint, 290 **kwargs, 291 ) 292 293 if res.stream is None: 294 self._logger.error("stream is None") 295 self._logger.debug("AsyncSpeakClient.save LEAVE") 296 raise DeepgramError("BytesIO stream is None") 297 298 # save to file 299 async with aiofiles.open(filename, "wb") as out: 300 await out.write(res.stream.getbuffer()) 301 await out.flush() 302 303 # add filename to response 304 res.stream = None 305 res.filename = filename 306 307 self._logger.debug("AsyncSpeakClient.save LEAVE") 308 return res
Speak from a text source and save to a file.
Args: source (TextSource): The text source to speak. options (SpeakRESTOptions): Additional options for the ingest (default is None). addons (Dict): Additional options for the request (default is None). headers (Dict): Additional headers for the request (default is None). timeout (httpx.Timeout): The timeout for the request (default is None). endpoint (str): The endpoint to use for the request (default is "v1/speak").
Returns: SpeakRESTResponse: The response from the speak request.
Raises: DeepgramTypeError: Raised for known API errors.