Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 0c15211

Browse files
Fix stuff and add examples to speech
1 parent 1b9f827 commit 0c15211

File tree

5 files changed

+119
-8
lines changed

5 files changed

+119
-8
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from openrobot.api_wrapper import AsyncClient
2+
3+
client = AsyncClient(...)
4+
5+
# Perform Text To Speech
6+
7+
# From file
8+
from io import BytesIO
9+
10+
file_bytes = BytesIO(...)
11+
12+
stt = await client.speech.speech_to_text(file_bytes, language_code=...)
13+
14+
# From URL
15+
url = ...
16+
17+
stt = await client.speech.speech_to_text(url, language_code=...)
18+
19+
20+
stt.text # ...
21+
stt.duration # ...
22+
23+
24+
# Get details on Text To Speech such as supported languages, etc.
25+
26+
await client.speech.speech_to_text_support() # {'languages': ['Language Codes']}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from openrobot.api_wrapper import AsyncClient
2+
3+
client = AsyncClient(...)
4+
5+
# Perform Text to speech
6+
7+
text = 'Hello World'
8+
9+
tts = await client.speech.text_to_speech(text, 'en-US', ...) # Get it by doing details (Speech.text_to_speech_support)
10+
11+
tts_url = tts.url # https://cdn.openrobot.xyz/speech/text-to-speech/...
12+
13+
# Get details on Speech To Text such as a list of Voices from a specific language, etc.
14+
15+
tts_support = await client.speech.text_to_speech_support('en-US')
16+
17+
for language in tts_support.languages:
18+
language.name
19+
language.code
20+
21+
for voice in tts_support.voices:
22+
voice.gender
23+
voice.id
24+
voice.language.name
25+
voice.language.code
26+
voice.name
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from openrobot.api_wrapper import SyncClient
2+
3+
client = SyncClient(...)
4+
5+
# Perform Text To Speech
6+
7+
# From file
8+
from io import BytesIO
9+
10+
file_bytes = BytesIO(...)
11+
12+
stt = client.speech.speech_to_text(file_bytes, language_code=...)
13+
14+
# From URL
15+
url = ...
16+
17+
stt = client.speech.speech_to_text(url, language_code=...)
18+
19+
20+
stt.text # ...
21+
stt.duration # ...
22+
23+
24+
# Get details on Text To Speech such as supported languages, etc.
25+
26+
client.speech.speech_to_text_support() # {'languages': ['Language Codes']}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from openrobot.api_wrapper import SyncClient
2+
3+
client = SyncClient(...)
4+
5+
# Perform Text to speech
6+
7+
text = 'Hello World'
8+
9+
tts = client.speech.text_to_speech(text, 'en-US', ...) # Get it by doing details (Speech.text_to_speech_support)
10+
11+
tts_url = tts.url # https://cdn.openrobot.xyz/speech/text-to-speech/...
12+
13+
# Get details on Speech To Text such as a list of Voices from a specific language, etc.
14+
15+
tts_support = client.speech.text_to_speech_support('en-US')
16+
17+
for language in tts_support.languages:
18+
language.name
19+
language.code
20+
21+
for voice in tts_support.voices:
22+
voice.gender
23+
voice.id
24+
voice.language.name
25+
voice.language.code
26+
voice.name

openrobot/api_wrapper/speech.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def speech_to_text(self, source: typing.Union[str, io.BytesIO], language_code: s
5252
if self._is_async:
5353
async def _text_to_speech() -> TextToSpeechResult:
5454
if isinstance(source, str):
55-
js = await self._client.request('POST', '/api/speech/speech-to-text', params={'url': source})
55+
js = await self._client.request('POST', '/api/speech/speech-to-text', params={'url': source, 'language_code': language_code})
5656
elif isinstance(source, io.BytesIO):
5757
data = aiohttp.FormData()
5858
data.add_field('file', source)
@@ -66,12 +66,12 @@ async def _text_to_speech() -> TextToSpeechResult:
6666
return _text_to_speech()
6767
else:
6868
if isinstance(source, str):
69-
js = self._client.request('POST', '/api/speech/speech-to-text', params={'url': source})
69+
js = self._client.request('POST', '/api/speech/speech-to-text', params={'url': source, 'language_code': language_code})
7070
elif isinstance(source, io.BytesIO):
7171
data = aiohttp.FormData()
7272
data.add_field('file', source)
7373

74-
js = self._client.request('POST', '/api/speech/speech-to-text', files={'upload_file': getattr(source, 'getvalue', lambda: source)()})
74+
js = self._client.request('POST', '/api/speech/speech-to-text', files={'upload_file': getattr(source, 'getvalue', lambda: source)()}, params={'language_code': language_code})
7575
else:
7676
raise OpenRobotAPIError('source must be a URL or BytesIO.')
7777

@@ -112,7 +112,7 @@ async def _speech_to_text_support() -> typing.Dict[str, typing.Any]:
112112

113113
return js
114114

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]:
115+
def text_to_speech(self, text: str, language_code: str, voice_id: str, *, engine: typing.Optional[str] = 'standard') -> typing.Union[typing.Coroutine[None, None, TextToSpeechResult], TextToSpeechResult]:
116116
"""|maybecoro|
117117
118118
Text to speech.
@@ -128,7 +128,7 @@ def text_to_speech(self, text: str, language_code: str, voice_id: str, *, engine
128128
The language code of the speech.
129129
voice_id: :class:`str`
130130
The voice id of the speech.
131-
engine: :class:`str`
131+
engine: Optional[:class:`str`]
132132
The engine of the speech.
133133
134134
Raises
@@ -158,14 +158,21 @@ async def _text_to_speech() -> TextToSpeechResult:
158158

159159
return TextToSpeechResult(js)
160160

161-
def text_to_speech_support(self) -> typing.Union[typing.Coroutine[None, None, TextToSpeechSupportResult], TextToSpeechSupportResult]:
161+
def text_to_speech_support(self, language_code: str, *, engine: typing.Optional[str] = 'standard') -> typing.Union[typing.Coroutine[None, None, TextToSpeechSupportResult], TextToSpeechSupportResult]:
162162
"""|maybecoro|
163163
164164
Returns the supported details for Text To Speech.
165165
166166
This function is a coroutine if the client is an
167167
:class:`AsyncClient` object, else it would be a synchronous method.
168168
169+
Parameters
170+
----------
171+
language_code: :class:`str`
172+
The language code to get the supported details for.
173+
engine: Optional[:class:`str`]
174+
The engine of the speech.
175+
169176
Raises
170177
------
171178
:exc:`Forbidden`
@@ -183,12 +190,12 @@ def text_to_speech_support(self) -> typing.Union[typing.Coroutine[None, None, Te
183190

184191
if self._is_async:
185192
async def _text_to_speech_support() -> TextToSpeechSupportResult:
186-
js = await self._client.request('GET', '/api/speech/text-to-speech/supports')
193+
js = await self._client.request('GET', '/api/speech/text-to-speech/supports', params={'language_code': language_code, 'engine': engine})
187194

188195
return TextToSpeechSupportResult(js)
189196

190197
return _text_to_speech_support()
191198
else:
192-
js = self._client.request('GET', '/api/speech/text-to-speech/supports')
199+
js = self._client.request('GET', '/api/speech/text-to-speech/supports', params={'language_code': language_code, 'engine': engine})
193200

194201
return TextToSpeechSupportResult(js)

0 commit comments

Comments
 (0)