1+ import typing
2+ import io
3+ import aiohttp
4+ from .results import SpeechToTextResult , TextToSpeechResult , TextToSpeechSupportResult
5+ from .error import OpenRobotAPIError
6+
7+ class Speech :
8+ """
9+ The speech client.
10+ """
11+
12+ def __init__ (self , client , is_async : bool ):
13+ self ._client = client
14+
15+ self ._is_async = is_async
16+
17+ def speech_to_text (self , source : typing .Union [str , io .BytesIO ], language_code : str ) -> typing .Union [typing .Coroutine [None , None , SpeechToTextResult ], SpeechToTextResult ]:
18+ """|maybecoro|
19+
20+ Speech to text.
21+
22+ This function is a coroutine if the client is an
23+ :class:`AsyncClient` object, else it would be a synchronous method.
24+
25+ Parameters
26+ ----------
27+ source: Union[:class:`str`, :class:`io.BytesIO`]
28+ The source of the speech. This can be either a URL or a
29+ :class:`io.BytesIO` object.
30+ language_code: :class:`str`
31+ The language code of the speech.
32+ voice_id: :class:`str`
33+ The voice id of the speech.
34+ engine: :class:`str`
35+ The engine of the speech.
36+
37+ Raises
38+ ------
39+ :exc:`Forbidden`
40+ API Returned a 403 HTTP Status Code.
41+ :exc:`BadRequest`
42+ API Returned a 400 HTTP Status Code.
43+ :exc:`InternalServerError`
44+ API Returned a 500 HTTP Status Code.
45+
46+ Returns
47+ -------
48+ Union[Coroutine[None, None, :class:`SpeechToTextResult`], :class:`SpeechToTextResult`]
49+ The result of the text to speech.
50+ """
51+
52+ if self ._is_async :
53+ async def _text_to_speech () -> TextToSpeechResult :
54+ if isinstance (source , str ):
55+ js = await self ._client .request ('POST' , '/api/speech/speech-to-text' , params = {'url' : source })
56+ elif isinstance (source , io .BytesIO ):
57+ data = aiohttp .FormData ()
58+ data .add_field ('file' , source )
59+
60+ js = await self ._client .request ('POST' , '/api/speech/speech-to-text' , data = data )
61+ else :
62+ raise OpenRobotAPIError ('source must be a URL or BytesIO.' )
63+
64+ return SpeechToTextResult (js )
65+
66+ return _text_to_speech ()
67+ else :
68+ if isinstance (source , str ):
69+ js = self ._client .request ('POST' , '/api/speech/speech-to-text' , params = {'url' : source })
70+ elif isinstance (source , io .BytesIO ):
71+ data = aiohttp .FormData ()
72+ data .add_field ('file' , source )
73+
74+ js = self ._client .request ('POST' , '/api/speech/speech-to-text' , files = {'upload_file' : getattr (source , 'getvalue' , lambda : source )()})
75+ else :
76+ raise OpenRobotAPIError ('source must be a URL or BytesIO.' )
77+
78+ return SpeechToTextResult (js )
79+
80+ def speech_to_text_support (self ) -> typing .Union [typing .Coroutine [None , None , typing .Dict [str , typing .Any ]], typing .Dict [str , typing .Any ]]:
81+ """|maybecoro|
82+
83+ Returns the supported details for Speech To Text.
84+
85+ This function is a coroutine if the client is an
86+ :class:`AsyncClient` object, else it would be a synchronous method.
87+
88+ Raises
89+ ------
90+ :exc:`Forbidden`
91+ API Returned a 403 HTTP Status Code.
92+ :exc:`BadRequest`
93+ API Returned a 400 HTTP Status Code.
94+ :exc:`InternalServerError`
95+ API Returned a 500 HTTP Status Code.
96+
97+ Returns
98+ -------
99+ Union[Coroutine[None, None, typing.Dict[:class:`str`, :class:`typing.Any`]], typing.Dict[:class:`str`, :class:`typing.Any`]]
100+ The supported details for Speech To Text.
101+ """
102+
103+ if self ._is_async :
104+ async def _speech_to_text_support () -> typing .Dict [str , typing .Any ]:
105+ js = await self ._client .request ('GET' , '/api/speech/speech-to-text/supports' )
106+
107+ return js
108+
109+ return _speech_to_text_support ()
110+ else :
111+ js = self ._client .request ('GET' , '/api/speech/speech-to-text/supports' )
112+
113+ return js
114+
115+ def text_to_speech (self , text : str , language_code : str , voice_id : str , * , engine : str = 'standard' ) -> typing .Union [typing .Coroutine [None , None , TextToSpeechResult ], TextToSpeechResult ]:
116+ """|maybecoro|
117+
118+ Text to speech.
119+
120+ This function is a coroutine if the client is an
121+ :class:`AsyncClient` object, else it would be a synchronous method.
122+
123+ Parameters
124+ ----------
125+ text: :class:`str`
126+ The text to be speeched.
127+ language_code: :class:`str`
128+ The language code of the speech.
129+ voice_id: :class:`str`
130+ The voice id of the speech.
131+ engine: :class:`str`
132+ The engine of the speech.
133+
134+ Raises
135+ ------
136+ :exc:`Forbidden`
137+ API Returned a 403 HTTP Status Code.
138+ :exc:`BadRequest`
139+ API Returned a 400 HTTP Status Code.
140+ :exc:`InternalServerError`
141+ API Returned a 500 HTTP Status Code.
142+
143+ Returns
144+ -------
145+ Union[Coroutine[None, None, :class:`TextToSpeechResult`], :class:`TextToSpeechResult`]
146+ The result of the text to speech.
147+ """
148+
149+ if self ._is_async :
150+ async def _text_to_speech () -> TextToSpeechResult :
151+ js = await self ._client .request ('GET' , '/api/speech/text-to-speech' , params = {'text' : text , 'language_code' : language_code , 'voice_id' : voice_id , 'engine' : engine })
152+
153+ return TextToSpeechResult (js )
154+
155+ return _text_to_speech ()
156+ else :
157+ js = self ._client .request ('GET' , '/api/speech/text-to-speech' , params = {'text' : text , 'language_code' : language_code , 'voice_id' : voice_id , 'engine' : engine })
158+
159+ return TextToSpeechResult (js )
160+
161+ def text_to_speech_support (self ) -> typing .Union [typing .Coroutine [None , None , TextToSpeechSupportResult ], TextToSpeechSupportResult ]:
162+ """|maybecoro|
163+
164+ Returns the supported details for Text To Speech.
165+
166+ This function is a coroutine if the client is an
167+ :class:`AsyncClient` object, else it would be a synchronous method.
168+
169+ Raises
170+ ------
171+ :exc:`Forbidden`
172+ API Returned a 403 HTTP Status Code.
173+ :exc:`BadRequest`
174+ API Returned a 400 HTTP Status Code.
175+ :exc:`InternalServerError`
176+ API Returned a 500 HTTP Status Code.
177+
178+ Returns
179+ -------
180+ Union[Coroutine[None, None, :class:`TextToSpeechSupportResult`], :class:`TextToSpeechSupportResult`]
181+ The supported details for Text To Speech.
182+ """
183+
184+ if self ._is_async :
185+ async def _text_to_speech_support () -> TextToSpeechSupportResult :
186+ js = await self ._client .request ('GET' , '/api/speech/text-to-speech/supports' )
187+
188+ return TextToSpeechSupportResult (js )
189+
190+ return _text_to_speech_support ()
191+ else :
192+ js = self ._client .request ('GET' , '/api/speech/text-to-speech/supports' )
193+
194+ return TextToSpeechSupportResult (js )
0 commit comments