forked from Significant-Gravitas/AutoGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeak.py
34 lines (26 loc) · 952 Bytes
/
speak.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
import os
from playsound import playsound
import requests
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
voices = ["ErXwobaYiN019PkySvjV", "EXAVITQu4vr4xnSDxMaL"]
tts_headers = {
"Content-Type": "application/json",
"xi-api-key": os.getenv("ELEVENLABS_API_KEY")
}
def say_text(text, voice_index=0):
tts_url = "https://api.elevenlabs.io/v1/text-to-speech/{voice_id}".format(
voice_id=voices[voice_index])
formatted_message = {"text": text}
response = requests.post(
tts_url, headers=tts_headers, json=formatted_message)
if response.status_code == 200:
with open("speech.mpeg", "wb") as f:
f.write(response.content)
playsound("speech.mpeg")
# Delete audio file
os.remove("speech.mpeg")
else:
print("Request failed with status code:", response.status_code)
print("Response content:", response.content)