forked from DougDougGithub/Babagaboosh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheleven_labs.py
62 lines (52 loc) · 2.17 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from elevenlabs import generate, stream, set_api_key, voices, play, save
import time
import os
try:
set_api_key(os.getenv('ELEVENLABS_API_KEY'))
except TypeError:
exit("Ooops! You forgot to set ELEVENLABS_API_KEY in your environment!")
class ElevenLabsManager:
def __init__(self):
# CALLING voices() IS NECESSARY TO INSTANTIATE 11LABS FOR SOME FUCKING REASON
all_voices = voices()
print(f"\nAll ElevenLabs voices: \n{all_voices}\n")
# Convert text to speech, then save it to file. Returns the file path
def text_to_audio(self, input_text, voice="Doug VO Only", save_as_wave=True, subdirectory=""):
audio_saved = generate(
text=input_text,
voice=voice,
model="eleven_monolingual_v1"
)
if save_as_wave:
file_name = f"___Msg{str(hash(input_text))}.wav"
else:
file_name = f"___Msg{str(hash(input_text))}.mp3"
tts_file = os.path.join(os.path.abspath(os.curdir), subdirectory, file_name)
save(audio_saved,tts_file)
return tts_file
# Convert text to speech, then play it out loud
def text_to_audio_played(self, input_text, voice="Doug VO Only"):
audio = generate(
text=input_text,
voice=voice,
model="eleven_monolingual_v1"
)
play(audio)
# Convert text to speech, then stream it out loud (don't need to wait for full speech to finish)
def text_to_audio_streamed(self, input_text, voice="Doug VO Only"):
audio_stream = generate(
text=input_text,
voice=voice,
model="eleven_monolingual_v1",
stream=True
)
stream(audio_stream)
if __name__ == '__main__':
elevenlabs_manager = ElevenLabsManager()
elevenlabs_manager.text_to_audio_streamed("This is my streamed test audio, I'm so much cooler than played", "Doug Melina")
time.sleep(2)
elevenlabs_manager.text_to_audio_played("This is my played test audio, helo hello", "Doug Melina")
time.sleep(2)
file_path = elevenlabs_manager.text_to_audio("This is my saved test audio, please make me beautiful", "Doug Melina")
print("Finished with all tests")
time.sleep(30)