Files
Auto-GPT/scripts/speak.py
2023-04-03 13:51:36 +02:00

33 lines
966 B
Python

import os
from playsound import playsound
import requests
from config import Config
cfg = Config()
# TODO: Nicer names for these ids
voices = ["ErXwobaYiN019PkySvjV", "EXAVITQu4vr4xnSDxMaL"]
tts_headers = {
"Content-Type": "application/json",
"xi-api-key": cfg.elevenlabs_api_key
}
def say_text(text, voice_index=0):
"""Say text using ElevenLabs API"""
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)