MCPcopy
hub / github.com/RayVentura/ShortGPT / ElevenLabsAPI

Class ElevenLabsAPI

shortGPT/api_utils/eleven_api.py:6–52  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4
5
6class ElevenLabsAPI:
7
8 def __init__(self, api_key):
9 self.api_key = api_key
10 self.url_base = 'https://api.elevenlabs.io/v1/'
11 self.get_voices()
12
13 def get_voices(self):
14 '''Get the list of voices available'''
15 url = self.url_base + 'voices'
16 headers = {'accept': 'application/json'}
17 if self.api_key:
18 headers['xi-api-key'] = self.api_key
19 response = requests.get(url, headers=headers)
20 self.voices = {voice['name']: voice['voice_id'] for voice in response.json()['voices']}
21 return self.voices
22
23 def get_remaining_characters(self):
24 '''Get the number of characters remaining'''
25 url = self.url_base + 'user'
26 headers = {'accept': '*/*', 'xi-api-key': self.api_key, 'Content-Type': 'application/json'}
27 response = requests.get(url, headers=headers)
28
29 if response.status_code == 200:
30 sub = response.json()['subscription']
31 return sub['character_limit'] - sub['character_count']
32 else:
33 raise Exception(response.json()['detail']['message'])
34
35 def generate_voice(self, text, character, filename, stability=0.2, clarity=0.1):
36 '''Generate a voice'''
37 if character not in self.voices:
38 print(character, 'is not in the array of characters: ', list(self.voices.keys()))
39
40 voice_id = self.voices[character]
41 url = f'{self.url_base}text-to-speech/{voice_id}/stream'
42 headers = {'accept': '*/*', 'xi-api-key': self.api_key, 'Content-Type': 'application/json'}
43 data = json.dumps({"model_id": "eleven_multilingual_v2", "text": text, "stability": stability, "similarity_boost": clarity})
44 response = requests.post(url, headers=headers, data=data)
45
46 if response.status_code == 200:
47 with open(filename, 'wb') as f:
48 f.write(response.content)
49 return filename
50 else:
51 message = response.text
52 raise Exception(f'Error in response, {response.status_code} , message: {message}')

Callers 4

__init__Method · 0.90
__init__Method · 0.90
verify_eleven_keyMethod · 0.90
getElevenlabsVoicesMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected