-
Notifications
You must be signed in to change notification settings - Fork 4
/
speaker.py
37 lines (30 loc) · 1.26 KB
/
speaker.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
from deepgram import DeepgramClient, SpeakOptions
import os
def text_to_speech(text):
if os.environ.get("DEEPGRAM_API_KEY") is None:
print("Please set the DEEPGRAM_API_KEY environment variable to enable text to speech.")
return
else:
DEEPGRAM_API_KEY = os.environ.get("DEEPGRAM_API_KEY")
FILENAME = "combined_audio.mp3"
try:
deepgram = DeepgramClient(DEEPGRAM_API_KEY)
options = SpeakOptions(
model="aura-asteria-en",
)
if len(text) > 1999:
chunks = [text[i:i + 1800] for i in range(0, len(text), 1800)]
else:
chunks = [text]
with open(FILENAME, "wb") as combined_audio:
for i, chunk in enumerate(chunks):
chunk_filename = f"audio_chunk_{i}.mp3"
response = deepgram.speak.v("1").save(chunk_filename, {"text": chunk}, options)
with open(chunk_filename, "rb") as chunk_file:
combined_audio.write(chunk_file.read())
os.remove(chunk_filename) # Remove chunk file after processing
print(f"Audio saved as {FILENAME}")
return FILENAME
except Exception as e:
print(f"Exception: {e}")
return f"Error converting text to speech: {str(e)}"