forked from vishalsubbiah/en-passant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheleven_labs.py
40 lines (34 loc) · 1.18 KB
/
eleven_labs.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
import requests
import os
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env.
from playsound import playsound
class ElevenVoice:
def __init__(self):
self.CHUNK_SIZE = 1024
key = os.environ["ELEVEN_LABS_API"]
self.headers = {
"Accept": "audio/mpeg",
"Content-Type": "application/json",
"xi-api-key": key
}
def generate_and_play_audio(self, content, voice_id):
data = {
"text": content,
"model_id": "eleven_turbo_v2",
"voice_settings": {
"stability": 0.5,
"similarity_boost": 0.75
}
}
url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}"
response = requests.post(url, json=data, headers=self.headers)
with open('output.mp3', 'wb') as f:
for chunk in response.iter_content(chunk_size=self.CHUNK_SIZE):
if chunk:
f.write(chunk)
playsound('output.mp3')
if __name__ == "__main__":
gordon_voice_id = os.environ["GORDON_VOICE_ID"]
voice = ElevenVoice()
voice.generate_and_play_audio("Does this work?", gordon_voice_id)