-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathtts_functions.py
137 lines (108 loc) · 3.69 KB
/
tts_functions.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import pyttsx3
import requests
import gtts
from gtts import gTTS
import os
from playsound import playsound
from datetime import datetime
from langdetect import detect
from general_functions import *
import json
from elevenlabs import generate, play, save
gtts_languages = set(gtts.lang.tts_langs().keys())
def robospeak(text: str):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
def tts11Multi(key: str, text: str, path: str, voice_id: str = 'EXAVITQu4vr4xnSDxMaL'):
audio = generate(
text=text,
voice=voice_id,
model='eleven_multilingual_v1',
api_key=key
)
save(audio, filename=path)
def tts11AI(key: str, text: str, path: str, voice_id: str = 'EXAVITQu4vr4xnSDxMaL') -> bool:
"""
This uses ElevenLab's AI to generate text to speech.
:param key: This is your 11.ai key
:param text: What you want spoken
:param path: Where you want your file saved
"""
# create a session object
s = requests.Session()
# set the headers
headers = {
"accept": "audio/mpeg",
"xi-api-key": key,
"Content-Type": "application/json",
}
# set the payload
payload = {
"text": text
}
# make the post request
url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}"
try:
r = s.post(url, data=json.dumps(payload), headers=headers, timeout=60)
if r.status_code != 200:
return False
# save the response content
with open(path, 'wb') as f:
f.write(r.content)
except requests.exceptions.Timeout:
return False
except Exception as e:
info(f'Unexpected error: {e}', 'bad')
return False
return True
def google_tts(text: str, path: str, show_text:bool = True):
language = detect(text)
if show_text: info(f'DETECTED LANGUAGE: {language}')
if language in gtts_languages: # Pronounce correctly if possible
tts = gTTS(text, lang=language)
tts.save(path)
elif language == 'zh-cn':
tts = gTTS(text, lang='zh-CN')
tts.save(path)
elif language == 'zh-tw':
tts = gTTS(text, lang='zh-TW')
tts.save(path)
else: # Otherwise just use English pronounciation
tts = gTTS(text)
tts.save(path)
def talk(text: str, name: str, use11: bool = False, key11: str = '',
show_text:bool = True, eleven_voice_id: str = 'EXAVITQu4vr4xnSDxMaL') -> bool:
"""
This will provide a sound file for what ever you enter, then
play it using playsound. Saves an mp3 file.
:param text: This is what you want to be converted to speech
:param name: This is what you want the mp3 file to be called
"""
tts11_okay = False
# 1. Set up name
now = datetime.now()
today = f'{now.month}-{now.day}-{now.year}'
file = f'./messages/{today}/{name}'
if not os.path.isdir(f'messages'): # Make primary dir if not there
os.mkdir(f'messages')
if not os.path.isdir(f'./messages/{today}'):
os.mkdir(f'./messages/{today}')
if os.path.isfile(file + '.mpeg'): # Delete file if it's already there
os.remove(file + '.mpeg')
elif os.path.isfile(file + '.mp3'):
os.remove(file + '.mp3')
# 2. Have gtts create file
try:
if use11:
tts11Multi(text=text, key=key11, voice_id=eleven_voice_id, path=f'{file}.mpeg')
playsound(f'{file}.mpeg')
return True
else:
google_tts(text, f'{file}.mp3', show_text=show_text)
playsound(file + '.mp3')
return tts11_okay
except Exception as e:
google_tts(text, f'{file}.mp3', show_text=show_text)
playsound(file + '.mp3')
return tts11_okay