This repository was archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathibm.py
89 lines (77 loc) · 3.31 KB
/
ibm.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
# Takes advantage of IBM's wattson tts demo allowing for free medium quality tts
# This program is free software; you can redistribute it and/or modify it under the terms
# of the GNU General Public License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program;
# if not, see http://www.gnu.org/licenses/gpl-3.0
import requests, string, random
from enum import Enum
s_pattern = string.ascii_lowercase + string.digits
s_chunck = lambda r: "".join([random.choice(s_pattern) for _ in range(r)])
def createId():
return f"{s_chunck(8)}-{s_chunck(4)}-{s_chunck(4)}-{s_chunck(4)}-{s_chunck(12)}"
def wrap(x):
return f'<prosody pitch="0%" rate="-0%">{x}</prosody>'
class Voices(Enum):
allison_expressive = "en-US_AllisonExpressive"
emma_expressive = "en-US_EmmaExpressive"
lisa_expressive = "en-US_LisaExpressive"
michael_expressive = "en-US_MichaelExpressive"
allison = "en-US_AllisonV3Voice"
emily ="en-US_EmilyV3Voice"
henry ="en-US_HenryV3Voice"
kevin ="en-US_KevinV3Voice"
lisa ="en-US_LisaV3Voice"
michael = "en-US_MichaelV3Voice"
olivia ="en-US_OliviaV3Voice"
class Imbtts:
def __init__(self, proxy=None):
self.prox = proxy
self.ses = requests.session()
if proxy:
self.ses.proxies.update(proxy)
h = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
self.ses.get("https://www.ibm.com/demos/live/tts-demo/self-service/home", headers=h)
def chunck(self, ssml):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.5',
'Content-Type': 'application/json;charset=utf-8',
'Origin': 'https://www.ibm.com',
'Connection': 'keep-alive',
'Referer': 'https://www.ibm.com/demos/live/tts-demo/self-service/home',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
}
idd = createId()
json_data = {
'ssmlText': ssml,
'sessionID': idd,
}
r =self.ses.post('https://www.ibm.com/demos/live/tts-demo/api/tts/store', headers=headers, json=json_data)
return idd
def download(self, idd, loc, voice = Voices.michael_expressive):
headers = {
'authority': 'www.ibm.com',
'accept': '*/*',
'accept-language': 'en-US,en;q=0.5',
'range': 'bytes=0-',
'referer': 'https://www.ibm.com/demos/live/tts-demo/self-service/home',
'sec-fetch-dest': 'audio',
'sec-fetch-mode': 'no-cors',
'sec-fetch-site': 'same-origin',
'sec-gpc': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
}
f = open(loc, "wb")
r = self.ses.get("https://www.ibm.com/demos/live/tts-demo/api/tts/newSynthesize?voice="+voice.value+"&id="+idd, headers=headers, timeout=60*7)
c = r.content
f.write(c)
f.close()
return len(c)