Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
elebumm
GitHub Repository: elebumm/RedditVideoMakerBot
Path: blob/master/TTS/streamlabs_polly.py
327 views
1
import random
2
3
import requests
4
from requests.exceptions import JSONDecodeError
5
6
from utils import settings
7
from utils.voice import check_ratelimit
8
9
voices = [
10
"Brian",
11
"Emma",
12
"Russell",
13
"Joey",
14
"Matthew",
15
"Joanna",
16
"Kimberly",
17
"Amy",
18
"Geraint",
19
"Nicole",
20
"Justin",
21
"Ivy",
22
"Kendra",
23
"Salli",
24
"Raveena",
25
]
26
27
28
# valid voices https://lazypy.ro/tts/
29
30
31
class StreamlabsPolly:
32
def __init__(self):
33
self.url = "https://streamlabs.com/polly/speak"
34
self.max_chars = 550
35
self.voices = voices
36
37
def run(self, text, filepath, random_voice: bool = False):
38
if random_voice:
39
voice = self.randomvoice()
40
else:
41
if not settings.config["settings"]["tts"]["streamlabs_polly_voice"]:
42
raise ValueError(
43
f"Please set the config variable STREAMLABS_POLLY_VOICE to a valid voice. options are: {voices}"
44
)
45
voice = str(settings.config["settings"]["tts"]["streamlabs_polly_voice"]).capitalize()
46
47
body = {"voice": voice, "text": text, "service": "polly"}
48
headers = {"Referer": "https://streamlabs.com/"}
49
response = requests.post(self.url, headers=headers, data=body)
50
51
if not check_ratelimit(response):
52
self.run(text, filepath, random_voice)
53
54
else:
55
try:
56
voice_data = requests.get(response.json()["speak_url"])
57
with open(filepath, "wb") as f:
58
f.write(voice_data.content)
59
except (KeyError, JSONDecodeError):
60
try:
61
if response.json()["error"] == "No text specified!":
62
raise ValueError("Please specify a text to convert to speech.")
63
except (KeyError, JSONDecodeError):
64
print("Error occurred calling Streamlabs Polly")
65
66
def randomvoice(self):
67
return random.choice(self.voices)
68
69