-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtts.py
61 lines (54 loc) · 2.17 KB
/
tts.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
import threading
import requests
from playsound import playsound
from time import sleep
import re
from params import session
import os
def remove_emojis(text: str):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
u"\U00002500-\U00002BEF" # chinese char
u"\U00002702-\U000027B0"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
u"\U0001f926-\U0001f937"
u"\U00010000-\U0010ffff"
u"\u2640-\u2642"
u"\u2600-\u2B55"
u"\u200d"
u"\u23cf"
u"\u23e9"
u"\u231a"
u"\ufe0f" # dingbats
u"\u3030"
"]+", flags=re.UNICODE)
return emoji_pattern.sub(r'', text)
def text_to_speech(text):
api = os.getenv("MANDRILL")
headers = {
'Authorization': f'Bearer {api}'
}
data = {
"text": text,
"voice_id": session.VOICE_ID
}
response = requests.post("https://api.mandrillai.tech/v1/audio/tts",
json=data,
headers=headers)
print(response)
# Save the file
if os.path.exists(os.path.join(os.path.dirname(__file__), "audio.mp3")):
os.remove(os.path.join(os.path.dirname(__file__), "audio.mp3"))
try:
with open(os.path.join(os.path.dirname(__file__), "audio.mp3"), "wb") as f:
f.write(response.content)
sleep(0.3)
f.close()
sleep(0.2)
threading.Thread(target=playsound, args=(os.path.join(os.path.dirname(__file__), "audio.mp3"),)).start()
except:
print("Ошибочка :_")