-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
tts_elevenlabs.py
305 lines (269 loc) · 9.17 KB
/
tts_elevenlabs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import requests
class TTSElevenlabs():
"""
Class for text2audio conversion using elevenlabs api
"""
def __init__(self, api_key: str) -> None:
"""
Initiate the class
Parameters:
api_key (str): api key for elevenlabs api
"""
self.headers = {
'accept': 'application/json',
'xi-api-key': api_key,
}
self.endpoint_prefix = 'https://api.elevenlabs.io/v1'
self.endpoints = {
'get_subscription_info': '/user/subscription',
'get_user_info': '/user',
'get_voices': '/voices',
'get_default_voice_settings': '/default',
'get_voice_settings': '/voices/{voice_id}/settings',
'get_voice': '/voices/{voice_id}',
'delete_voice': '/voices/{voice_id}',
'edit_voice_settings': '/voices/{voice_id}/settings/edit',
'add_voice': '/voices/add',
'edit_voice': '/voices/{voice_id}/edit',
'text_to_speech': '/text-to-speech/{voice_id}',
'text_to_speech_stream': '/text-to-speech/{voice_id}/stream',
'delete sample': '/voices/{voice_id}/samples/{sample_id}',
'get_sample': '/voices/{voice_id}/samples/{sample_id}/audio',
'get_history': '/history',
'get_audio_from_history': '/history/{history_id}/audio',
'delete_history': '/history/{history_id}',
'download_history': '/history/download',
}
self.active_voice = None
def get_subscription_info(self) -> dict:
"""
Get subscription info
Returns:
dict: subscription info
"""
response = requests.get(
self.endpoint_prefix + self.endpoints['get_subscription_info'],
headers=self.headers
)
return response.json()
def get_user_info(self) -> dict:
"""
Get user info
Returns:
dict: user info
"""
response = requests.get(
self.endpoint_prefix + self.endpoints['get_user_info'],
headers=self.headers
)
return response.json()
def get_voices(self) -> dict:
"""
Get voices
Returns:
dict: voices
"""
response = requests.get(
self.endpoint_prefix + self.endpoints['get_voices'],
headers=self.headers
)
return response.json()
def get_default_voice_settings(self) -> dict:
"""
Get default voice settings
Returns:
dict: default voice settings
"""
response = requests.get(
self.endpoint_prefix + self.endpoints['get_default_voice_settings'],
headers=self.headers
)
return response.json()
def get_voice_settings(self, voice_id: str = None) -> dict:
"""
Get voice settings
Parameters:
voice_id (str): voice id (if None, active voice will be used)
Returns:
dict: voice settings
"""
if voice_id is None:
voice_id = self.active_voice
response = requests.get(
self.endpoint_prefix + self.endpoints['get_voice_settings'].format(voice_id=voice_id),
headers=self.headers
)
return response.json()
def get_voice(self, voice_id: str = None) -> dict:
"""
Get voice
Parameters:
voice_id (str): voice id (if None, active voice will be used)
"""
if voice_id is None:
voice_id = self.active_voice
response = requests.get(
self.endpoint_prefix + self.endpoints['get_voice'].format(voice_id=voice_id),
headers=self.headers
)
return response.json()
def delete_voice(self, voice_id: str) -> dict:
"""
Delete voice
Parameters:
voice_id (str): voice id
Returns:
dict: response
"""
response = requests.delete(
self.endpoint_prefix + self.endpoints['delete_voice'].format(voice_id=voice_id),
headers=self.headers
)
return response.json()
def edit_voice_settings(self, voice_id: str = None, data: dict = {}) -> dict:
"""
Edit voice settings
Parameters:
voice_id (str): voice id (if None, active voice will be used)
data (dict): data to be sent
Returns:
dict: response
"""
if voice_id is None:
voice_id = self.active_voice
response = requests.post(
self.endpoint_prefix + self.endpoints['edit_voice_settings'].format(voice_id=voice_id),
headers=self.headers,
json=data
)
return response.json()
def add_voice(self, data: dict) -> dict:
"""
Add voice
Parameters:
data (dict): data to be sent
Returns:
dict: response
"""
response = requests.post(
self.endpoint_prefix + self.endpoints['add_voice'],
headers=self.headers,
json=data
)
return response.json()
def edit_voice(self, voice_id: str = None, data: dict = {}) -> dict:
"""
Edit voice
"""
if voice_id is None:
voice_id = self.active_voice
response = requests.post(
self.endpoint_prefix + self.endpoints['edit_voice'].format(voice_id=voice_id),
headers=self.headers,
json=data
)
return response.json()
def text_to_speech(self, text: str, voice_id: str = None) -> dict:
"""
Text to speech
"""
if voice_id is None:
voice_id = self.active_voice
response = requests.post(
self.endpoint_prefix + self.endpoints['text_to_speech'].format(voice_id=voice_id),
headers=self.headers,
json={'text': text}
)
return response
def text_to_speech_stream(self, text: str, voice_id: str = None) -> dict:
"""
Text to speech stream
"""
if voice_id is None:
voice_id = self.active_voice
response = requests.post(
self.endpoint_prefix + self.endpoints['text_to_speech_stream'].format(voice_id=voice_id),
headers=self.headers,
json={'text': text}
)
return response
def delete_sample(self, sample_id: str, voice_id: str = None) -> dict:
"""
Delete sample
"""
if voice_id is None:
voice_id = self.active_voice
response = requests.delete(
self.endpoint_prefix + self.endpoints['delete sample'].format(voice_id=voice_id, sample_id=sample_id),
headers=self.headers
)
return response.json()
def get_sample(self, sample_id: str, voice_id: str = None) -> dict:
"""
Get sample
"""
if voice_id is None:
voice_id = self.active_voice
response = requests.get(
self.endpoint_prefix + self.endpoints['get_sample'].format(voice_id=voice_id, sample_id=sample_id),
headers=self.headers
)
return response.json()
def get_history(self) -> dict:
"""
Get history
"""
response = requests.get(
self.endpoint_prefix + self.endpoints['get_history'],
headers=self.headers
)
return response.json()
def get_audio_from_history(self, history_id: str) -> dict:
"""
Get audio from history
"""
response = requests.get(
self.endpoint_prefix + self.endpoints['get_audio_from_history'].format(history_id=history_id),
headers=self.headers
)
return response.json()
def delete_history(self, history_id: str) -> dict:
"""
Delete history
"""
response = requests.delete(
self.endpoint_prefix + self.endpoints['delete_history'].format(history_id=history_id),
headers=self.headers
)
return response.json()
def download_history(self, data: dict) -> dict:
"""
Download history
"""
response = requests.post(
self.endpoint_prefix + self.endpoints['download_history'],
headers=self.headers,
json=data
)
return response.json()
def set_active_voice(self, voice_id: str = None, voice_name: str = None) -> None:
"""
Set active voice using voice ID (if not provided, will fallback to using voice name)
Parameters:
voice_id (str): Voice ID
voice_name (str): Voice name
"""
if voice_id != None:
self.active_voice = voice_id
elif voice_name != None:
voices = self.get_voices()
found_voice = False
for voice in voices['voices']:
if voice['name'] == voice_name:
self.active_voice = voice['voice_id']
found_voice = True
break
if found_voice == False:
raise Exception('Voice name not found')
else:
raise Exception('No voice ID or name provided')