2626from .types .body_text_to_speech_streaming_with_timestamps_v_1_text_to_speech_voice_id_stream_with_timestamps_post_apply_text_normalization import (
2727 BodyTextToSpeechStreamingWithTimestampsV1TextToSpeechVoiceIdStreamWithTimestampsPostApplyTextNormalization ,
2828)
29+ from .types .text_to_speech_stream_with_timestamps_response import TextToSpeechStreamWithTimestampsResponse
30+ import json
2931from ..core .client_wrapper import AsyncClientWrapper
3032
3133# this is used as the default value for optional parameters
@@ -538,7 +540,7 @@ def stream_with_timestamps(
538540 BodyTextToSpeechStreamingWithTimestampsV1TextToSpeechVoiceIdStreamWithTimestampsPostApplyTextNormalization
539541 ] = OMIT ,
540542 request_options : typing .Optional [RequestOptions ] = None ,
541- ) -> None :
543+ ) -> typing . Iterator [ TextToSpeechStreamWithTimestampsResponse ] :
542544 """
543545 Converts text into speech using a voice of your choice and returns a stream of JSONs containing audio as a base64 encoded string together with information on when which character was spoken.
544546
@@ -595,9 +597,10 @@ def stream_with_timestamps(
595597 request_options : typing.Optional[RequestOptions]
596598 Request-specific configuration.
597599
598- Returns
599- -------
600- None
600+ Yields
601+ ------
602+ typing.Iterator[TextToSpeechStreamWithTimestampsResponse]
603+ Stream of JSON objects containing audio chunks and character timing information
601604
602605 Examples
603606 --------
@@ -606,12 +609,14 @@ def stream_with_timestamps(
606609 client = ElevenLabs(
607610 api_key="YOUR_API_KEY",
608611 )
609- client.text_to_speech.stream_with_timestamps(
612+ response = client.text_to_speech.stream_with_timestamps(
610613 voice_id="21m00Tcm4TlvDq8ikWAM",
611614 text="text",
612615 )
616+ for chunk in response:
617+ yield chunk
613618 """
614- _response = self ._client_wrapper .httpx_client .request (
619+ with self ._client_wrapper .httpx_client .stream (
615620 f"v1/text-to-speech/{ jsonable_encoder (voice_id )} /stream/with-timestamps" ,
616621 method = "POST" ,
617622 params = {
@@ -644,24 +649,38 @@ def stream_with_timestamps(
644649 },
645650 request_options = request_options ,
646651 omit = OMIT ,
647- )
648- try :
649- if 200 <= _response .status_code < 300 :
650- return
651- if _response .status_code == 422 :
652- raise UnprocessableEntityError (
653- typing .cast (
654- HttpValidationError ,
655- construct_type (
656- type_ = HttpValidationError , # type: ignore
657- object_ = _response .json (),
658- ),
652+ ) as _response :
653+ try :
654+ if 200 <= _response .status_code < 300 :
655+ for _text in _response .iter_lines ():
656+ try :
657+ if len (_text ) == 0 :
658+ continue
659+ yield typing .cast (
660+ TextToSpeechStreamWithTimestampsResponse ,
661+ construct_type (
662+ type_ = TextToSpeechStreamWithTimestampsResponse , # type: ignore
663+ object_ = json .loads (_text ),
664+ ),
665+ )
666+ except :
667+ pass
668+ return
669+ _response .read ()
670+ if _response .status_code == 422 :
671+ raise UnprocessableEntityError (
672+ typing .cast (
673+ HttpValidationError ,
674+ construct_type (
675+ type_ = HttpValidationError , # type: ignore
676+ object_ = _response .json (),
677+ ),
678+ )
659679 )
660- )
661- _response_json = _response .json ()
662- except JSONDecodeError :
663- raise ApiError (status_code = _response .status_code , body = _response .text )
664- raise ApiError (status_code = _response .status_code , body = _response_json )
680+ _response_json = _response .json ()
681+ except JSONDecodeError :
682+ raise ApiError (status_code = _response .status_code , body = _response .text )
683+ raise ApiError (status_code = _response .status_code , body = _response_json )
665684
666685
667686class AsyncTextToSpeechClient :
@@ -1194,7 +1213,7 @@ async def stream_with_timestamps(
11941213 BodyTextToSpeechStreamingWithTimestampsV1TextToSpeechVoiceIdStreamWithTimestampsPostApplyTextNormalization
11951214 ] = OMIT ,
11961215 request_options : typing .Optional [RequestOptions ] = None ,
1197- ) -> None :
1216+ ) -> typing . AsyncIterator [ TextToSpeechStreamWithTimestampsResponse ] :
11981217 """
11991218 Converts text into speech using a voice of your choice and returns a stream of JSONs containing audio as a base64 encoded string together with information on when which character was spoken.
12001219
@@ -1251,9 +1270,10 @@ async def stream_with_timestamps(
12511270 request_options : typing.Optional[RequestOptions]
12521271 Request-specific configuration.
12531272
1254- Returns
1255- -------
1256- None
1273+ Yields
1274+ ------
1275+ typing.AsyncIterator[TextToSpeechStreamWithTimestampsResponse]
1276+ Stream of JSON objects containing audio chunks and character timing information
12571277
12581278 Examples
12591279 --------
@@ -1267,15 +1287,17 @@ async def stream_with_timestamps(
12671287
12681288
12691289 async def main() -> None:
1270- await client.text_to_speech.stream_with_timestamps(
1290+ response = await client.text_to_speech.stream_with_timestamps(
12711291 voice_id="21m00Tcm4TlvDq8ikWAM",
12721292 text="text",
12731293 )
1294+ async for chunk in response:
1295+ yield chunk
12741296
12751297
12761298 asyncio.run(main())
12771299 """
1278- _response = await self ._client_wrapper .httpx_client .request (
1300+ async with self ._client_wrapper .httpx_client .stream (
12791301 f"v1/text-to-speech/{ jsonable_encoder (voice_id )} /stream/with-timestamps" ,
12801302 method = "POST" ,
12811303 params = {
@@ -1308,21 +1330,35 @@ async def main() -> None:
13081330 },
13091331 request_options = request_options ,
13101332 omit = OMIT ,
1311- )
1312- try :
1313- if 200 <= _response .status_code < 300 :
1314- return
1315- if _response .status_code == 422 :
1316- raise UnprocessableEntityError (
1317- typing .cast (
1318- HttpValidationError ,
1319- construct_type (
1320- type_ = HttpValidationError , # type: ignore
1321- object_ = _response .json (),
1322- ),
1333+ ) as _response :
1334+ try :
1335+ if 200 <= _response .status_code < 300 :
1336+ async for _text in _response .aiter_lines ():
1337+ try :
1338+ if len (_text ) == 0 :
1339+ continue
1340+ yield typing .cast (
1341+ TextToSpeechStreamWithTimestampsResponse ,
1342+ construct_type (
1343+ type_ = TextToSpeechStreamWithTimestampsResponse , # type: ignore
1344+ object_ = json .loads (_text ),
1345+ ),
1346+ )
1347+ except :
1348+ pass
1349+ return
1350+ await _response .aread ()
1351+ if _response .status_code == 422 :
1352+ raise UnprocessableEntityError (
1353+ typing .cast (
1354+ HttpValidationError ,
1355+ construct_type (
1356+ type_ = HttpValidationError , # type: ignore
1357+ object_ = _response .json (),
1358+ ),
1359+ )
13231360 )
1324- )
1325- _response_json = _response .json ()
1326- except JSONDecodeError :
1327- raise ApiError (status_code = _response .status_code , body = _response .text )
1328- raise ApiError (status_code = _response .status_code , body = _response_json )
1361+ _response_json = _response .json ()
1362+ except JSONDecodeError :
1363+ raise ApiError (status_code = _response .status_code , body = _response .text )
1364+ raise ApiError (status_code = _response .status_code , body = _response_json )
0 commit comments